help with yaml file

8 views
Skip to first unread message

Vinod P

unread,
Jun 8, 2021, 2:20:30 AM6/8/21
to Ansible Project
Hello All,
Need some help with yaml file

I am trying to run find command to fetch old IP address from multiple files in /etc directory and replace them with new IP address as follows

sudo find /etc  -type f -exec sed -i -e 's/10.1.3.51/10.2.3.54/g' {} +

I can accomplish this using the command but how do I do it using yaml file as i need to run it on multiple servers. 

Please help and thanks in advance.  

Vladimir Botka

unread,
Jun 8, 2021, 2:45:50 AM6/8/21
to Vinod P, ansible...@googlegroups.com
On Mon, 7 Jun 2021 23:20:30 -0700 (PDT)
Vinod P <vinod...@gmail.com> wrote:

> in /etc directory and replace them with new IP address as follows
> sudo find /etc -type f -exec sed -i -e 's/10.1.3.51/10.2.3.54/g' {} +

For example, given the file

shell> cat etc/file1
10.1.3.50
10.1.3.51
10.1.3.52

the playbook

shell> cat test-007.yml
- hosts: localhost
gather_facts: false
become: true
tasks:
- find:
path: etc
file_type: file
recurse: true
register: result
- replace:
path: "{{ item }}"
regexp: "10.1.3.51"
replace: "10.2.3.54"
loop: "{{ result.files|
map(attribute='path')|
list }}"

gives

shell> ansible-playbook test-007.yml -CD

PLAY [localhost]
******************************************

TASK [find]
******************************************
ok: [localhost]

TASK [replace]
******************************************
--- before: etc/file1 +++ after: etc/file1
@@ -1,3 +1,3 @@
10.1.3.50
-10.1.3.51
+10.2.3.54
10.1.3.52

changed: [localhost] => (item=etc/file1)


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages