# Is there a way to add hosts from a file to inventory\_hosts at the runtime of the playbook?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-add-hosts-from-a-file-to-inventory-hosts-at-the-runtime-of-the-playbook/17133>\
**Category:** Ansible Project\
**Created:** [December 23, 2014, 6:37am UTC](https://forum.ansible.com/t/is-there-a-way-to-add-hosts-from-a-file-to-inventory-hosts-at-the-runtime-of-the-playbook/17133 "2014-12-23T06:37:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fazal-e-Rehman\_Khan](https://avatars.discourse-cdn.com/v4/letter/f/9de053/32.png) [@Fazal-e-Rehman\_Khan](https://forum.ansible.com/u/Fazal-e-Rehman_Khan)\
**Post date:** [December 23, 2014, 6:37am UTC](https://forum.ansible.com/t/is-there-a-way-to-add-hosts-from-a-file-to-inventory-hosts-at-the-runtime-of-the-playbook/17133/1 "2014-12-23T06:37:29Z")

</div>

I have a script that I run at the start of my first play like this.

**- hosts: localhost**  
**local\_action: command python runScript.py**

It gnerates me a file in **roles/abc/vars/main.yml** which looks like this.

**newHosts:**  
**- hostIP: 192.168.1.23**  
**filename: file1**  
**- hostIP: 192.168.1.24**  
**filename: file2**

Then I call the role by

**roles:**  
**- { role: abc }**

in that role, I’m trying to add those hosts to inventory\_host as well as in the group by doing this:

**add\_host: hostname= {{ item.hostIP }} groups=newHosts filename= {{ item.filename }}**  
**with\_items: newHosts**

The problem is that the first time when I run this play; the script runs and generates the file and when the role is called it gives me the error

**fatal: [127.0.0.1] =\> One or more undefined variables: ‘unicode object’ has no attribute ‘hostIP’**

**FATAL: all hosts have already failed – aborting**

which is logical because the file **roles/abc/var/main.yml** didn’t exist in the beginning. The second time when I run the playbook. it runs without any problem.

So, can you guide me how can I add the hosts from a file which is generated at the run-time and use it in the same play?

Thanking you in anticipation.

Warm Regards,  
Fazal-e-Rehman Khan

---

<div class="post-metadata">

**Author:** ![jorginator74](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@jorginator74](https://forum.ansible.com/u/jorginator74)\
**Post date:** [December 23, 2014, 8:49am UTC](https://forum.ansible.com/t/is-there-a-way-to-add-hosts-from-a-file-to-inventory-hosts-at-the-runtime-of-the-playbook/17133/2 "2014-12-23T08:49:50Z")

</div>

Hi

> I have a script that I run at the start of my first play like this.
> 
> - hosts: localhost  
> &nbsp;&nbsp;local\_action: command python runScript.py
> 
> It gnerates me a file in roles/abc/vars/main.yml which looks like this.
> 
> newHosts:  
> &nbsp;&nbsp;&nbsp;- hostIP: 192.168.1.23  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename: file1  
> &nbsp;&nbsp;&nbsp;- hostIP: 192.168.1.24  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename: file2
> 
> Then I call the role by
> 
> roles:  
> &nbsp;&nbsp;&nbsp;- { role: abc }
> 
> in that role, I'm trying to add those hosts to inventory\_host as well as in the  
> group by doing this:
> 
> add\_host: hostname= {{ item.hostIP }} groups=newHosts filename= {{  
> item.filename }}  
> with\_items: newHosts
> 
> The problem is that the first time when I run this play; the script runs and  
> generates the file and when the role is called it gives me the error
> 
> fatal: [127.0.0.1] =\> One or more undefined variables: 'unicode object' has no  
> attribute 'hostIP'
> 
> FATAL: all hosts have already failed -- aborting
> 
> which is logical because the file roles/abc/var/main.yml didn't exist in the  
> beginning. The second time when I run the playbook. it runs without any  
> problem.

Well - vars\_files are read very early on during execution of  
ansible-playbook. So that's why it fares better second time around.

> So, can you guide me how can I add the hosts from a file which is generated at  
> the run-time and use it in the same play?

It sounds like you will be better off with a dynamic inventory - i.e.  
have a look at [http://docs.ansible.com/developing\_inventory.html](http://docs.ansible.com/developing_inventory.html)

By the sound of it you have most of what you need already, you just  
need to make sure that your python script outputs an inventory in JSON format

Hope this helps
