I have two files where these file contains server names and server IP's, I want to change/replace some specific server names and IP addressees in both the files based on the requirement.
This is related to This Post <-- as it was asked to open a new post.
My scenario:
In the Below example files(file & file2) i need to do follows..
1 - In file1 and fil2 i have to replace fostrain01.example.com with dbfostrain01.example.com.
2 - Where in another line i have to replace 171.20.20.18 with 172.20.20.18 in both the files as well.
# cat /etc/file1
fostrain01.example.com
fostrain02.example.com
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
# cat /etc/fil2
fostrain01.example.com
fostrain02.example.com
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
My Playbook:
---
- name: Replace file contents
hosts: all
gather_facts: false
vars:
files:
- /etc/file1
- /etc/file2
from_str: "fostrain01.example.com"
to_str: "dbfoxtrain01.example.com"
from_ip: "^(.*)171\\.20\\.20\\.18(.*)$"
to_ip: "\\g<1>172.20.20.18\\g<2>"
tasks:
- name: Replace elements in file
replace:
path: "{{ item.path }}"
regexp: "{{ item.From }}"
replace: "{{ item.To }}"
backup: yes
loop:
# Replace the desired string
- { path: "{{ item }}", From: "{{ from_str }}", To: "{{ to_str }}" }
# Replace the desired ip
- { path: "{{ item }}", From: "{{ from_ip }}", To: "{{ to_ip }}" }
In the above playbook, i have defined the variables for each sections as you can see.
What I'm Missing and Would Like to Know:
I am missing to understand about How i can use or reference files variable in the path section of replace module in my playbook above while using loop.
Just to clarify, i'm talking about below one..
files:
- /etc/file1
- /etc/file2
I am looking this to be fitted within the approach in the above playbook, as i know the other way around of doing it.
I am sorry, if i could not make it more clear.