• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

hosts

GCP Fix Hosts

December 7, 2021

This is a script that fixes /etc/hosts in GCP.

#!/bin/bash
 
# VARIABLES
FILE='/etc/hosts'
 
#BACKUP /etc/hosts before changes
cp -ipHR $FILE $FILE_$(date +%Y-%m-%d_%H-%M-%S)
 
# GET IP ADDRESS
IP=$(/sbin/ifconfig | grep 'inet' | grep -v 'inet6' | grep -v '127.0' | cut -d: -f2 | awk '{print $2}' | head -1)
 
# GET HOSTNAME
HN=$(/bin/hostname)
 
# GET PROJECT ID
echo "169.254.169.254 metadata.google.internal  # Added by Google" >> $FILE
PR=$(curl -f -s http://metadata.google.internal/computeMetadata/v1/project/project-id -H "Metadata-Flavor: Google")
 
# ADD HOSTNAME ENTRY TO /etc/hosts
STRING="$IP $HN.mydomain.com"
 
if grep -q "$STRING" "$FILE"
then
  echo "Valid host entry."
else
  echo "Adding host entry."
  echo $STRING $HN >> $FILE
fi
 
# DELETE GOOGLE ENTRIES.
sed -i '/# Added by Google/d' $FILE
 
# ADD BACK GOOGLE ENTRIES.
echo $IP $HN".c."$PR".internal" $HN"  # Added by Google" >> $FILE
echo "169.254.169.254 metadata.google.internal  # Added by Google" >> $FILE
 
# SET PERMISSIONS.
chmod 644 $FILE
 
# DISPLAY /etc/hosts
cat $FILE

#!/bin/bash # VARIABLES FILE='/etc/hosts' #BACKUP /etc/hosts before changes cp -ipHR $FILE $FILE_$(date +%Y-%m-%d_%H-%M-%S) # GET IP ADDRESS IP=$(/sbin/ifconfig | grep 'inet' | grep -v 'inet6' | grep -v '127.0' | cut -d: -f2 | awk '{print $2}' | head -1) # GET HOSTNAME HN=$(/bin/hostname) # GET PROJECT ID echo "169.254.169.254 metadata.google.internal # Added by Google" >> $FILE PR=$(curl -f -s http://metadata.google.internal/computeMetadata/v1/project/project-id -H "Metadata-Flavor: Google") # ADD HOSTNAME ENTRY TO /etc/hosts STRING="$IP $HN.mydomain.com" if grep -q "$STRING" "$FILE" then echo "Valid host entry." else echo "Adding host entry." echo $STRING $HN >> $FILE fi # DELETE GOOGLE ENTRIES. sed -i '/# Added by Google/d' $FILE # ADD BACK GOOGLE ENTRIES. echo $IP $HN".c."$PR".internal" $HN" # Added by Google" >> $FILE echo "169.254.169.254 metadata.google.internal # Added by Google" >> $FILE # SET PERMISSIONS. chmod 644 $FILE # DISPLAY /etc/hosts cat $FILE

Result

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
 
10.128.0.30 test.mydomain.com test
10.128.0.30 test.c.project-id.internal test  # Added by Google
169.254.169.254 metadata.google.internal  # Added by Google

127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 10.128.0.30 test.mydomain.com test 10.128.0.30 test.c.project-id.internal test # Added by Google 169.254.169.254 metadata.google.internal # Added by Google

It adds a proper host entry before the Google entries. /etc/hosts has top to bottom precedence.

Filed Under: Cloud Tagged With: file, fix, gcp, hosts

Ansible Update Playbook

August 1, 2020

I have an Ansible playbook that will patch all Ansible clients defined in the Ansible hosts file. The following are contents of my hosts file, and the update playbook.

File: /etc/ansible/hosts

[all:vars]
ansible_user='ubuntu'
ansible_become=yes
ansible_become_method=sudo
ansible_python_interpreter='/usr/bin/env python3'
[servers]
server1
server2
server3
[servers:vars]
ansible_python_interpreter=/usr/bin/python3

[all:vars] ansible_user='ubuntu' ansible_become=yes ansible_become_method=sudo ansible_python_interpreter='/usr/bin/env python3' [servers] server1 server2 server3 [servers:vars] ansible_python_interpreter=/usr/bin/python3

File: /etc/ansible/update.yml

---
- hosts: servers
  become: true
  become_user: root
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no

    - name: Reboot the box if kernel updated
      reboot:
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exist

--- - hosts: servers become: true become_user: root tasks: - name: Update apt repo and cache on all Debian/Ubuntu boxes apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 - name: Upgrade all packages on servers apt: upgrade=dist force_apt_get=yes - name: Check if a reboot is needed on all servers register: reboot_required_file stat: path=/var/run/reboot-required get_md5=no - name: Reboot the box if kernel updated reboot: msg: "Reboot initiated by Ansible for kernel updates" connect_timeout: 5 reboot_timeout: 300 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime when: reboot_required_file.stat.exist

Here’s how I run my Ansible update playbook.

ansible-playbook -i /etc/ansible/hosts /etc/ansible/update.yml

ansible-playbook -i /etc/ansible/hosts /etc/ansible/update.yml

The advantage of using Ansible is, I can run a single playbook to update dozens of servers. It’s also a great tool for rolling out software as well as executing commands to a group of servers.

Filed Under: Linux Tagged With: ansible, hosts, patch, playbook, update

Hostname

January 14, 2019

If you need to change your system’s hostname, use hostname or hostnamectl commands.

hostname servername
hostnamectl set-hostname servername

hostname servername hostnamectl set-hostname servername

You can add the hostname to these 2 files and reboot.

vi /etc/hostname
vi /etc/hosts
reboot

vi /etc/hostname vi /etc/hosts reboot

Without a reboot. Add the hostname to these 2 files.

vi /etc/hostname
vi /etc/hosts

vi /etc/hostname vi /etc/hosts

And add a hostname entry to 127.0.0.1 in /etc/hosts.

127.0.0.1 servername

127.0.0.1 servername

That’s all she wrote.

Filed Under: Linux Tagged With: hostname, hostnamectl, hosts

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023