# Update inventory file

**URL:** <https://forum.ansible.com/t/update-inventory-file/18843>\
**Category:** Ansible Project\
**Created:** [July 24, 2015, 3:20pm UTC](https://forum.ansible.com/t/update-inventory-file/18843 "2015-07-24T15:20:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgardo\_Vega](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@Edgardo\_Vega](https://forum.ansible.com/u/Edgardo_Vega)\
**Post date:** [July 24, 2015, 3:20pm UTC](https://forum.ansible.com/t/update-inventory-file/18843/1 "2015-07-24T15:20:14Z")

</div>

I have a playbook with is creating vms and then i want to add the ip address to the inventory file to when it comes time to provision i can just call the updated inventory file. lineinfile will only add to the found lines at the end of the file. replace will update all the matches. What is the recommended way to do this?

===playbook===

---

<div class="post-metadata">

**Author:** ![rmullinnix](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@rmullinnix](https://forum.ansible.com/u/rmullinnix)\
**Post date:** [July 24, 2015, 10:21pm UTC](https://forum.ansible.com/t/update-inventory-file/18843/2 "2015-07-24T22:21:19Z")

</div>

One way is to create a new file with the host name and the ip address, then use this to update the hosts file as opposed to updating it in place. You can use the post\_tasks: to run the update at the end of the playbook.

You can do this with sed by creating a sed script file with lines of – s/hostname/hostname ansible\_ssh\_host=ipaddress/g. You can use lineinfile - which will add to the EOF. call it hosts.update

Then you would have a task that would update the hosts file  
post\_tasks:

- name: update hosts file with ip address  
shell: sed -i -f hosts.update hosts

I’m sure there is probably a better way to do this
