Add multiple hosts into inventory

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
host2.domain.com

host3.domain.com

host4.domain.com

host5.domain.com

host6.domain.com

host7.domain.com

host8.domain.com

host9.domain.com

host10.domain.com

Any help is appreciated!!!

Regards,
Sammi

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’.format(i) for i in range(11)]

r = {‘_meta’: {‘hostvars’: {}}, ‘all’: {‘hosts’: host_list}}

json.dumps(r)

Good luck!

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

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.

Thanks Chris,

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

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

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