Skip to content

Instantly share code, notes, and snippets.

@nitanka
Created August 14, 2017 06:44
Show Gist options
  • Save nitanka/5976ef7fd51c8795db91c4c35f290a44 to your computer and use it in GitHub Desktop.
Save nitanka/5976ef7fd51c8795db91c4c35f290a44 to your computer and use it in GitHub Desktop.
Ansible: adding hosts to /etc/hosts from inventory file
name: host mapping /etc/hosts of server
hosts: <hostgroup>
become: true
tasks:
- name: adding hostname mapping /etc/hosts
lineinfile:
dest: /etc/hosts
insertbefore: '^127.0.0.1'
line: '{{ item.0 }} {{ item.1 }}'
with_together:
- "{{ groups['hostname'] | map('extract', hostvars, ['ansible_default_ipv4', 'address'])| list }}"
- "{{ groups['hostname'] | map('extract', hostvars, ['ansible_nodename']) | list }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment