# populate /etc/hosts from dynamic inventory

**URL:** <https://forum.ansible.com/t/populate-etc-hosts-from-dynamic-inventory/28189>\
**Category:** Ansible Project\
**Created:** [July 30, 2018, 9:59am UTC](https://forum.ansible.com/t/populate-etc-hosts-from-dynamic-inventory/28189 "2018-07-30T09:59:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [July 30, 2018, 9:59am UTC](https://forum.ansible.com/t/populate-etc-hosts-from-dynamic-inventory/28189/1 "2018-07-30T09:59:20Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 30, 2018, 3:34pm UTC](https://forum.ansible.com/t/populate-etc-hosts-from-dynamic-inventory/28189/2 "2018-07-30T15:34:34Z")

</div>

> 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  
> {  
> &nbsp;&nbsp;"\_meta": {  
> &nbsp;&nbsp;&nbsp;&nbsp;"hostvars": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"AIXACODBT": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "172.17.9.68",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_lparname": "AIXACODBT"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"AIXAMTEST1DB": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "172.17.14.56",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_lparname": "AIXAMTEST1DB"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"AIXAPPL1EDUC": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "172.17.9.173",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_lparname": "AIXAPPL1EDUC"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"AIXAPPL1ITU5": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "172.17.9.192",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_lparname": "AIXAPPL1ITU5"  
> Enter code here...

This look wrong.  
AIXACODBT, AIXAMTEST1DB, AIXAPPL1EDUC and AIXAPPL1ITU5 is inventory\_hostname you can not set it like this.  
Your inventory\_hostname should probably be rename to ansible\_host.

> 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...

You have a dot instead of a underscore in inventory\_lparname,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 31, 2018, 8:12pm UTC](https://forum.ansible.com/t/populate-etc-hosts-from-dynamic-inventory/28189/3 "2018-07-31T20:12:03Z")

</div>

You are almost there, just made 1 mistake referencing the variable:

> {{ hostvars[host].inventory\_hostname }} {{ hostvars[host].inventory.lparname }

{{ hostvars[host].inventory\_hostname }} {{ hostvars[host].inventory\_lparname }
