# playbook to compare and update hostvars in host file

**URL:** <https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 3, 2020, 5:43am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733 "2020-09-03T05:43:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harsh\_chawda](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@harsh\_chawda](https://forum.ansible.com/u/harsh_chawda)\
**Post date:** [September 3, 2020, 5:43am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733/1 "2020-09-03T05:43:42Z")

</div>

Hello All,

I am trying to update /etc/hosts file if there is any changes in the host extra variables by comparing it from a csv file

[windows]  
[Server01.test.com](http://Server01.test.com) ansible\_user=“admin” ansible\_password=“password”  
[Server02.test.com](http://Server02.test.com) ansible\_user=“admin” ansible\_password=“Welcome”

So suppose the password is changed for the systems .

**password.csv**  
[Server01.test.com](http://Server01.test.com) passwordNew  
[Server02.test.com](http://Server02.test.com) WelcomeNew

So, how to compare and change the values in the existing host file.

Please if anyone having any idea or script for this will be a great help for me.

Thanks in advance

**Regards**  
**Harsh**

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 3, 2020, 10:02am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733/2 "2020-09-03T10:02:26Z")

</div>

You now appear to have two sources of truth. I would try to have one. Removing the data from the inventory seems the easiest.

---

<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:** [September 3, 2020, 11:49am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733/3 "2020-09-03T11:49:57Z")

</div>

Use "read\_csv" to read the data. For example

&nbsp;&nbsp;- read\_csv:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: password.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delimiter: ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldnames: [host, passwd]  
&nbsp;&nbsp;&nbsp;&nbsp;register: my\_db  
&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_db.list

There are more options to replace the passwords. Edit the host file  
(1,2), create new host file from a template 93), or create new group  
dynamically (4).

1) Module "replace", which might the first choice, seems to have an  
issue #47917. The parameter "after' doesn't work as expected.  
[https://github.com/ansible/ansible/issues/47917#issuecomment-686339762](https://github.com/ansible/ansible/issues/47917#issuecomment-686339762)

For example, the parameter "after" doesn't match the section  
"[windows]" in the task below. If you remove the "after" parameter  
the task works fine. If there are more sections (groups) with the  
same hosts all passwords will be replaced not only the passwords in  
the section [windows]

&nbsp;&nbsp;- replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;after: '^\[windows\]$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^{{ item.host }}(.\*)ansible\_password=(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: '{{ item.host }}\1ansible\_password="{{ item.passwd  
&nbsp;&nbsp;&nbsp;&nbsp;}}"'  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_db.list }}"

2) Module "lineinfile" works as expected, but also here it's not  
possible to select a section of the file

&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;path: hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^{{ item.host }}(.\*)ansible\_password=(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '{{ item.host }}\1ansible\_password="{{ item.passwd }}"'  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_db.list }}"

3) The only save option seems to be "template", but you'll have to  
keep all data of the hosts in a data structure. Either a list or a  
dictionary.

4) There is one more option. Create new group of hosts in the first  
play dynamically and use it in the second play. For example, the  
playbook below

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read\_csv:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: password.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delimiter: ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldnames: [host, passwd]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: my\_db  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- my\_windows  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ item.host }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_user: admin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_password: "{{ item.passwd }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_db.list }}"  
&nbsp;&nbsp;- hosts: my\_windows  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_password

gives (abridged)

&nbsp;&nbsp;TASK [debug] \*\*\*\*  
&nbsp;&nbsp;ok: [Server01.test.com] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_password": "passwordNew"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [Server02.test.com] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_password": "WelcomeNew"  
&nbsp;&nbsp;}

---

<div class="post-metadata">

**Author:** ![harsh\_chawda](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@harsh\_chawda](https://forum.ansible.com/u/harsh_chawda)\
**Post date:** [September 4, 2020, 4:51am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733/4 "2020-09-04T04:51:18Z")

</div>

Hello Visser,

Thanks for your reply. Can you give an example on what you explained like **Removing the data from the inventory seems the easiest**.How to achieve this.

Thanks in advance.

**Regards**  
**Harsh**

---

<div class="post-metadata">

**Author:** ![harsh\_chawda](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@harsh\_chawda](https://forum.ansible.com/u/harsh_chawda)\
**Post date:** [September 4, 2020, 7:59am UTC](https://forum.ansible.com/t/playbook-to-compare-and-update-hostvars-in-host-file/33733/5 "2020-09-04T07:59:04Z")

</div>

Hello Vladimir,

I am following your 4th option ie **Create new group of hosts in the first play dynamically and use it in the second play**.

It is executing successfully , but inside /etc/hosts no entry is found for the host name and password.

Please help me on this.

Thanks,

**Regards**  
**Harsh**
