Last active
January 11, 2020 08:53
-
-
Save aliasmee/abad0916b97a36b8f10466aba0133d2f to your computer and use it in GitHub Desktop.
modify /etc/hosts file hostname from inventory file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ansible playbook inventory file