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

**URL:** <https://forum.ansible.com/t/run-dynamic-inventory-script-in-a-play-to-pass-as-to-a-second-play-as-inventory/10631>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [November 1, 2024, 3:03pm UTC](https://forum.ansible.com/t/run-dynamic-inventory-script-in-a-play-to-pass-as-to-a-second-play-as-inventory/10631 "2024-11-01T15:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slekkus](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@slekkus](https://forum.ansible.com/u/slekkus)\
**Post date:** [November 1, 2024, 3:03pm UTC](https://forum.ansible.com/t/run-dynamic-inventory-script-in-a-play-to-pass-as-to-a-second-play-as-inventory/10631/1 "2024-11-01T15:03:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [November 1, 2024, 4:51pm UTC](https://forum.ansible.com/t/run-dynamic-inventory-script-in-a-play-to-pass-as-to-a-second-play-as-inventory/10631/2 "2024-11-01T16:51:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [November 1, 2024, 6:22pm UTC](https://forum.ansible.com/t/run-dynamic-inventory-script-in-a-play-to-pass-as-to-a-second-play-as-inventory/10631/3 "2024-11-01T18:22:55Z")

</div>

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)
