# Updating Ansible hosts inventory dynamically

**URL:** <https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596>\
**Category:** Ansible Project\
**Created:** [February 1, 2019, 7:09pm UTC](https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596 "2019-02-01T19:09:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subhi\_Andrews](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Subhi\_Andrews](https://forum.ansible.com/u/Subhi_Andrews)\
**Post date:** [February 1, 2019, 7:09pm UTC](https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596/1 "2019-02-01T19:09:41Z")

</div>

We have our hosts inventory organized by environments. We tend to have alpha versions of software deployed in our test environment. Are there any thoughts on keeping the inventory dynamically updated if we decide to move a host between environment - say from test to dev or from dev to production etc. Are there any best practices and such around it?

Thank you.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 1, 2019, 7:26pm UTC](https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596/2 "2019-02-01T19:26:22Z")

</div>

You have information in the documentation about dynamic inventory  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_dynamic\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html)

Some plugins is created  
[https://docs.ansible.com/ansible/latest/plugins/inventory.html](https://docs.ansible.com/ansible/latest/plugins/inventory.html)

If non of them suite your need you probably need to create your own.

---

<div class="post-metadata">

**Author:** ![Subhi\_Andrews](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@Subhi\_Andrews](https://forum.ansible.com/u/Subhi_Andrews)\
**Post date:** [February 1, 2019, 8:13pm UTC](https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596/3 "2019-02-01T20:13:55Z")

</div>

Thank you, Kai. Will check it out.

So if we can write a script that generates inventory in json format, we can directly pass the script to the ansible-playbook command? Did I understand that correctly?

Regards,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 1, 2019, 8:51pm UTC](https://forum.ansible.com/t/updating-ansible-hosts-inventory-dynamically/29596/4 "2019-02-01T20:51:46Z")

</div>

That is correct, if a inventory file doesn't have the execution bit set Ansible will read the content, if the execution bit is set Ansible will run the file.

The script need to return json and the format is described here together with how to develop a plugin  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_inventory.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html)
