Skip to content

Instantly share code, notes, and snippets.

@aliasmee
Last active January 11, 2020 08:53
Show Gist options
  • Save aliasmee/abad0916b97a36b8f10466aba0133d2f to your computer and use it in GitHub Desktop.
Save aliasmee/abad0916b97a36b8f10466aba0133d2f to your computer and use it in GitHub Desktop.
modify /etc/hosts file hostname from inventory file
REF: https://gist.github.com/phips/11233502
- name: Ensure hostname set from inventory file
tags: etchostsupdate
hostname: name={{ inventory_hostname }}
when: not inventory_hostname|match('(\d{1,3}\.){3}\d{1,3}')
- name: Update the /etc/hosts file with node name
tags: etchostsupdate
become: yes
become_user: root
lineinfile:
path: "/etc/hosts"
regexp: "{{ hostvars[item]['ansible_env'].SSH_CONNECTION.split(' ')[2] }}\t{{ hostvars[item]['ansible_fqdn']}}\t{{ hostvars[item]['ansible_hostname']}}"
line: "{{ hostvars[item]['ansible_env'].SSH_CONNECTION.split(' ')[2] }}\t{{ hostvars[item]['ansible_fqdn']}}\t{{ hostvars[item]['ansible_hostname']}}"
state: present
backup: yes
register: etchostsupdate
when: ansible_hostname != "{{ item }}" or ansible_hostname == "{{ item }}"
with_items: "{{ansible_play_hosts}}"
@aliasmee
Copy link
Author

aliasmee commented Jan 11, 2020

ansible playbook inventory file
[test-hadoop-cluster]
hadoop-m1.example.com ansible_user=user ansible_host=172.16.193.16
hadoop-n1.example.com ansible_user=user ansible_host=172.16.193.181
hadoop-n2.example.com ansible_user=user ansible_host=172.16.193.40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment