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* On Thursday, September 3, 2020 at 5:20:26 PM UTC+5:30, Vladimir Botka wrote: > > On Wed, 2 Sep 2020 22:43:42 -0700 (PDT) > harsh chawda <[email protected] <javascript:>> wrote: > > > *hosts* > > [windows] > > Server01.test.com ansible_user="admin" ansible_password="password" > > Server02.test.com ansible_user="admin" ansible_password="Welcome" > > > > *password.csv* > > Server01.test.com passwordNew > > Server02.test.com WelcomeNew > > > > how to compare and change the values in the existing host file. > > Use "read_csv" to read the data. For example > > - read_csv: > path: password.csv > delimiter: ' ' > fieldnames: [host, passwd] > register: my_db > - debug: > 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 > > 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] > > - replace: > path: hosts > after: '^\[windows\]$' > regexp: '^{{ item.host }}(.*)ansible_password=(.*)$' > replace: '{{ item.host }}\1ansible_password="{{ item.passwd > }}"' > loop: "{{ my_db.list }}" > > 2) Module "lineinfile" works as expected, but also here it's not > possible to select a section of the file > > - lineinfile: > path: hosts > regexp: '^{{ item.host }}(.*)ansible_password=(.*)$' > line: '{{ item.host }}\1ansible_password="{{ item.passwd }}"' > 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 > > - hosts: localhost > tasks: > - read_csv: > path: password.csv > delimiter: ' ' > fieldnames: [host, passwd] > register: my_db > - add_host: > groups: > - my_windows > hostname: "{{ item.host }}" > ansible_user: admin > ansible_password: "{{ item.passwd }}" > loop: "{{ my_db.list }}" > - hosts: my_windows > gather_facts: false > tasks: > - debug: > var: ansible_password > > gives (abridged) > > TASK [debug] **** > ok: [Server01.test.com] => { > "ansible_password": "passwordNew" > } > ok: [Server02.test.com] => { > "ansible_password": "WelcomeNew" > } > > -- > Vladimir Botka > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/26b83499-2ead-48c5-a8db-be1c7a2a7bfbo%40googlegroups.com.
