hey all,
i need to populate the /etc/hosts file on the ansible server (localhost only) from dynamic inventory, alas it does not work, as the hosts/ip’s from inventory are not handled. any idea whats wrong?
inventory format:
root@lpgaixmgmtlx01:/etc/ansible/aix/dyninv>./inventory.py --list { "_meta": { "hostvars": { "AIXACODBT": { "inventory_hostname": "172.17.9.68", "inventory_lparname": "AIXACODBT" }, "AIXAMTEST1DB": { "inventory_hostname": "172.17.14.56", "inventory_lparname": "AIXAMTEST1DB" }, "AIXAPPL1EDUC": { "inventory_hostname": "172.17.9.173", "inventory_lparname": "AIXAPPL1EDUC" }, "AIXAPPL1ITU5": { "inventory_hostname": "172.17.9.192", "inventory_lparname": "AIXAPPL1ITU5" Enter code here...
template:
`
root@lpgaixmgmtlx01:/etc/ansible/local>cat etc/hosts.j2
{{ ansible_managed }}
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.17.8.1 nimmsag_a
172.17.9.140 lpgaixmgmtlx01_a
{% for host in play_hosts %}
{{ hostvars[host].inventory_hostname }} {{ hostvars[host].inventory.lparname }}
{% endfor %}
Enter code here…
`
playbook:
`
root@lpgaixmgmtlx01:/etc/ansible/local>cat prepare_local_hostsfile.yml
- hosts: localhost
tasks: - name: edit hosts file on ansible control machine
local_action: template src=etc/hosts.j2 dest=/etc/hosts
Enter code here…
`
outcome:
`
root@lpgaixmgmtlx01:/etc/ansible/local>cat /etc/hosts
Ansible managed
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.17.8.1 nimmsag_a
172.17.9.140 lpgaixmgmtlx01_a
`