• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

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

Failover Test

June 24, 2018

How to failover a website without actually doing a failover? It’s actually easier than you think. The key is to trick your computer that it’s pointing to a failover website. You can easily do this by editing your hosts file and adding a DNS entry. In both Linux and MacOS, you can edit the /etc/hosts file and add the failover site like the following below.

# /etc/hosts
# failover site
# xxx.xxx.xxx.xxx   yourdomain.com

# /etc/hosts # failover site # xxx.xxx.xxx.xxx yourdomain.com

xxx.xxx.xxx.xxx is the IP address of your failover site.
If you need to test the failover site, just uncomment the IP address.
Replace a comment if you’re done testing.

Filed Under: Cloud, Linux Tagged With: dns, hosts

Mac OS Hosts File

July 28, 2013

Adding host entries in your desktop can be beneficial if you run several servers or hosts on your internal network. Instead of typing and remembering long IP addresses, you simply type the name of each host to make things easier for you. Let’s say you have several servers named “one”, “two” and “three” in your internal network. Each hosts will have their own assigned static IP address. By editing your hosts file and adding host entries to it, you can then use the hostnames instead of IP addresses.

In the end, you can ping each hostname like this:

$ ping one
$ ping two
$ ping three

$ ping one $ ping two $ ping three

In addition, you can also use the hosts “one”, “two” or “three” on your browser URL if your host has a web interface. If there are no host entries, the ping command, as well as you browser will simply timeout. To make this all possible in your Mac OS, you’ll need to edit the /private/etc/hosts file, by opening the Terminal and typing:

$ sudo nano /private/etc/hosts

$ sudo nano /private/etc/hosts

You need to add each host to the file in this format: ip_address hostname. See below:

192.168.1.22 one
192.168.5.56 two
192.168.1.33 three

192.168.1.22 one 192.168.5.56 two 192.168.1.33 three

Save file and exit. Ctrl-o and Ctrl-x.

Flush the DNS by typing:

$ dscacheutil -flushcache

$ dscacheutil -flushcache

You can now ping and use the hostname anyway you want.

The only limitation with this approach is, you’ll have to edit the hosts file for each desktop or laptop that is on your network. You can avoid this by take the next step, by running your own internal DNS server.

Filed Under: Linux Tagged With: hosts, mac os

  • Home
  • About
  • Archives

Copyright © 2023