Run dynamic inventory (script) in a play to pass as to a second play as inventory

Hi, I am relatively new to Ansible and struggle to grasp how one should use dynamic inventories within playbooks or tools that call ansible-core. I have a python script that returns a valid json list of hostgroups and members which works with
ansible-inventory -i script.py -e hostgroup=webservers playbook.yml.
Is there any way to run this dynamic inventory within a play and pass it to a second play?

The only option I see now, is to run the inventory script with cmd module and use add_host for the subsequent play.

Rgrds.

I never have to invoke my inventory - as long as your ansible.cfg points to the file/directory (inventory = /opt/ansible/inventory/dynamic), it should automatically use it.

Many ways, use ansible-playbook -i script.py playbook.yml. The ansible-inventory is there to test/verify your inventory is working, but the rest of ansible CLI tools that execute against hosts are able to take -i also (adhoc, console, playbook)

3 Likes