# update hots file - the default inventory file

**URL:** <https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 3, 2021, 4:52am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711 "2021-03-03T04:52:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [March 3, 2021, 4:52am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711/1 "2021-03-03T04:52:51Z")

</div>

Can you provide output of lineinfile ? Mine look like

changed: [localhost] =\> (item=a) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: true, “item”: “a”, “msg”: “line added”}  
changed: [localhost] =\> (item=b) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: true, “item”: “b”, “msg”: “line added”}

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [March 3, 2021, 7:08am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711/2 "2021-03-03T07:08:50Z")

</div>

Thanks for the reply Abhi

What i noticed is it does update the file if there is no entry like blow is the o/p

TASK [Update the Inventory] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> (item=xx)

but if the entry already present in more than 1 group say … then it does not update the file & the output is

TASK [Update the Inventory] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=xxx)

So if a fresh box it is ok ( ie never had been provisioned )

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [March 3, 2021, 7:11am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711/3 "2021-03-03T07:11:27Z")

</div>

for eg

[group1]  
server1  
server2  
[group2]  
server1  
server2

if you remove from group one & run does not update … So lineinfile should have someway to say the position exactly below [group1] add is missing …

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 3, 2021, 9:01am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711/4 "2021-03-03T09:01:40Z")

</div>

Try "ini\_file", e.g.  
[https://docs.ansible.com/ansible/latest/collections/community/general/ini\_file\_module.html](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html)

&nbsp;&nbsp;&nbsp;&nbsp;- ini\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/ansible/hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section: MyServers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allow\_no\_value: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ servers }}"

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [March 4, 2021, 5:18am UTC](https://forum.ansible.com/t/update-hots-file-the-default-inventory-file/34711/5 "2021-03-04T05:18:20Z")

</div>

Thanks Vladimir Botka
