# Add multiple hosts into inventory

**URL:** <https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [December 20, 2017, 9:45pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350 "2017-12-20T21:45:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sammic30](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Sammic30](https://forum.ansible.com/u/Sammic30)\
**Post date:** [December 20, 2017, 9:45pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/1 "2017-12-20T21:45:46Z")

</div>

Hi,

I’m still learning AWX and have this question. Is there any way to add multiple hosts into inventory rather adding 1 at a time from UI?

Lets say I want to add below 10 hosts and want to run ansible playbook against them.

[host1.domain.com](http://host1.domain.com)  
[host2.domain.com](http://host2.domain.com)

[host3.domain.com](http://host3.domain.com)

[host4.domain.com](http://host4.domain.com)

[host5.domain.com](http://host5.domain.com)

[host6.domain.com](http://host6.domain.com)

[host7.domain.com](http://host7.domain.com)

[host8.domain.com](http://host8.domain.com)

[host9.domain.com](http://host9.domain.com)

[host10.domain.com](http://host10.domain.com)

Any help is appreciated!!!

Regards,  
Sammi

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [December 20, 2017, 10:08pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/2 "2017-12-20T22:08:25Z")

</div>

You can use tower-cli if you want to keep your logic inside of your text editor.

Alternatively, you could just write a custom script in the UI, add that as a source to your inventory, and then sync it.

Mental code for that:

#!/usr/bin/python

import json

host\_list = [‘host{}.[domain.com](http://domain.com)’.format(i) for i in range(11)]

r = {‘\_meta’: {‘hostvars’: {}}, ‘all’: {‘hosts’: host\_list}}

json.dumps(r)

Good luck!

---

<div class="post-metadata">

**Author:** ![Sammic30](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Sammic30](https://forum.ansible.com/u/Sammic30)\
**Post date:** [January 9, 2018, 10:47pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/3 "2018-01-09T22:47:03Z")

</div>

Thanks Alan but I’m getting below error while syncing.

stderr: ERROR! Could not parse inventory source /tmp/awx\_inventory\_xxxqxc/tmptERboz with available plugins: Plugin script failed: failed to parse executable inventory script results from /tmp/awx\_inventory\_xxxqxc/tmptERboz: needs to be a json dict Plugin yaml failed: Syntax Error while loading YAML.

exception type: \<class ‘yaml.scanner.ScannerError’\> exception: mapping values are not allowed in this context

AND

2018-01-09 22:39:36,609 INFO awx.main.commands.inventory\_import Reading Ansible inventory source: /tmp/awx\_inventory\_aJYV3F/tmpYicPAz Traceback (most recent call last): File “/opt/awx/bin/awx-manage”, line 11, in sys.exit(manage()) File “/opt/awx/embedded/lib/python2.7/site-packages/awx/ **init**.py”, line 109, in manage execute\_from\_command\_line(sys.argv) File “/opt/awx/embedded/lib64/python2.7/site-packages/django/core/management/ **init**.py”, line 364, in execute\_from\_command\_line utility.execute() File “/opt/awx/embedded/lib64/python2.7/site-packages/django/core/management/ **init**.py”, line 356, in execute self.fetch\_command(subcommand).run\_from\_argv(self.argv) File “/opt/awx/embedded/lib64/python2.7/site-packages/django/core/management/base.py”, line 283, in run\_from\_argv self.execute(\*args, \*\*cmd\_options) File “/opt/awx/embedded/lib64/python2.7/site-packages/django/core/management/base.py”, line 330, in execute output = self.handle(\*args, \*\*options) File “/opt/awx/embedded/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 993, in handle self.is\_custom) File “/opt/awx/embedded/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 242, in load\_inventory\_source is\_custom=is\_custom).load() File “/opt/awx/embedded/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 177, in load data = self.command\_to\_json(base\_args + [‘–list’]) File “/opt/awx/embedded/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 160, in command\_to\_json self.method, proc.returncode, stdout, stderr)) RuntimeError: ansible-inventory failed (rc=4) with stdout: stderr: ERROR! Could not parse inventory source /tmp/awx\_inventory\_aJYV3F/tmpYicPAz with available plugins: Plugin script failed: failed to parse executable inventory script results from /tmp/awx\_inventory\_aJYV3F/tmpYicPAz: needs to be a json dict Plugin yaml failed: YAML inventory has invalid structure, it should be a dictionary, got: \<class ‘ansible.parsing.yaml.objects.AnsibleUnicode’\> Plugin ini failed: /tmp/awx\_inventory\_aJYV3F/tmpYicPAz:3: Expected key=value host variable assignment, got: json

Do you have any example handy for tower-cli? I’ll try to work with that as well…

Regards,  
Sammi

---

<div class="post-metadata">

**Author:** ![Chris\_Church](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@Chris\_Church](https://forum.ansible.com/u/Chris_Church)\
**Post date:** [January 10, 2018, 6:03am UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/4 "2018-01-10T06:03:49Z")

</div>

HI Sammi,

Could you provide your custom inventory script that results in the errors below?

Alan’s mental code should use `print json.dumps(r)` for the last line. Without the print there would be no output, which would of course fail to be parsed as either JSON or YAML.

---

<div class="post-metadata">

**Author:** ![Sammic30](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Sammic30](https://forum.ansible.com/u/Sammic30)\
**Post date:** [January 10, 2018, 9:35pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/5 "2018-01-10T21:35:05Z")

</div>

Thanks Chris,

---

<div class="post-metadata">

**Author:** ![Chris\_Church](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@Chris\_Church](https://forum.ansible.com/u/Chris_Church)\
**Post date:** [January 10, 2018, 11:31pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/6 "2018-01-10T23:31:46Z")

</div>

Not quite. The output needs to be a JSON dict in the format described here: [http://docs.ansible.com/ansible/latest/dev\_guide/developing\_inventory.html](http://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html)

Try replacing the last line in your script with the following:

---

<div class="post-metadata">

**Author:** ![Sammic30](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Sammic30](https://forum.ansible.com/u/Sammic30)\
**Post date:** [January 11, 2018, 6:13pm UTC](https://forum.ansible.com/t/add-multiple-hosts-into-inventory/26350/7 "2018-01-11T18:13:52Z")

</div>

Thanks Chris,

This time the sync didn’t fail but I’m not seeing new hosts added in the inventory.

Sync logs:  
2018-01-11 18:09:09,795 INFO awx.main.commands.inventory\_import Updating inventory 5: Dict-A 2018-01-11 18:09:09,809 DEBUG awx.main.commands.inventory\_import Using system install of ansible-inventory CLI: /bin/ansible-inventory 2018-01-11 18:09:09,810 INFO awx.main.commands.inventory\_import Reading Ansible inventory source: /tmp/awx\_inventory\_EbTlRj/tmp\_\_ReXr 2018-01-11 18:09:11,243 INFO awx.main.commands.inventory\_import Processing JSON output… 2018-01-11 18:09:11,243 DEBUG awx.main.commands.inventory\_import Loaded group: all 2018-01-11 18:09:11,243 DEBUG awx.main.commands.inventory\_import Finished loading from source: /tmp/awx\_inventory\_EbTlRj/tmp\_\_ReXr 2018-01-11 18:09:11,243 INFO awx.main.commands.inventory\_import Loaded 0 groups, 0 hosts 2018-01-11 18:09:11,247 INFO awx.main.commands.inventory\_import Inventory variables unmodified 2018-01-11 18:09:11,251 DEBUG awx.main.models.inventory Going to update inventory computed fields 2018-01-11 18:09:11,300 DEBUG awx.main.models.inventory Finished updating inventory computed fields 2018-01-11 18:09:11,301 INFO awx.main.commands.inventory\_import Inventory import completed for Dict-A in 1.5s

[![](https://lh3.googleusercontent.com/-Wy4G6Q9pxN8/WlepU7x0X_I/AAAAAAAANm4/koePbAgE2qI3-lzz0FryrIwpCSV5AVZLQCLcBGAs/s320/Screen%2BShot%2B2018-01-11%2Bat%2B10.12.23%2BAM.png)](https://lh3.googleusercontent.com/-Wy4G6Q9pxN8/WlepU7x0X_I/AAAAAAAANm4/koePbAgE2qI3-lzz0FryrIwpCSV5AVZLQCLcBGAs/s1600/Screen%2BShot%2B2018-01-11%2Bat%2B10.12.23%2BAM.png)
