问题描述
# cat file1
fostrain01.example.com
fostrain02.example.com
fostrain03.example.com
file2
的内容# cat fil2
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25
在下面的剧本中,它替换了两行的内容,一行是主机名作为 str
,另一行替换 ip 所以,我已经完成了两个不同的任务 1) 替换字符串在文件中
&2) 替换文件中的ip
来完成这个调用定义的变量.
In the below playbook, its replacing the contents on two lines one is hostname as str
and in another line replacing the ip So, i have taken the two different task 1) Replace strings in file
& 2) Replace ip in file
to accomplish this calling the variable defined.
- name: Replace string in hosts file
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 strings in file
replace:
path: "{{ item}}"
regexp: "{{ from_str }}"
replace: "{{ to_str }}"
backup: yes
loop: "{{ files }}"
- name: Replace ip in file
replace:
path: "{{ item}}"
regexp: "{{ from_ip }}"
replace: "{{ to_ip }}"
backup: yes
loop: "{{ files }}"
我们能否按如下方式编写任务,其中我们并没有真正写入两个不同的任务,我尝试过但没有了解如何使用以下方法遍历 "{{ files }}
.
Can we write the task as follows something where we don't really write to two different tasks, i tried but not getting how to loops through "{{ files }}
with below approach.
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}}" }
什么是期望的变化:
task 1) 替换文件中的字符串
&任务 2) 替换文件中的 ip
将其合并为一个.
What is Desired Change:
task 1) Replace strings in file
& task 2) Replace ip in file
to be clubbed into One.
# cat file1
dbfoxtrain01.example.com <-- Changed
fostrain02.example.com
fostrain03.example.com
# cat fil2
ServerIPS 171.20.20.16 171.20.20.17 172.20.20.18 171.20.20.19 171.20.20.20 <-- changed here ^172
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25
推荐答案
下面的任务完成工作
- replace:
path: "{{ item.path }}"
regexp: "{{ item.from }}"
replace: "{{ item.to }}"
loop:
- path: file1
from: 'fostrain01\.example\.com'
to: 'dbfoxtrain01.example.com'
- path: file2
from: '171\.20\.20\.18'
to: '172.20.20.18'
示例
shell> tree .
.
├── file1
├── file1.orig
├── file2
├── file2.orig
└── test.yml
0 directories, 5 files
shell> cat file1
fostrain01.example.com
fostrain02.example.com
fostrain03.example.com
shell> cat file2
ServerIPS 171.20.20.16 171.20.20.17 171.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25
shell> cat test.yml
- hosts: localhost
gather_facts: false
tasks:
- replace:
path: "{{ item.path }}"
regexp: "{{ item.from }}"
replace: "{{ item.to }}"
loop:
- path: file1
from: 'fostrain01\.example\.com'
to: 'dbfoxtrain01.example.com'
- path: file2
from: '171\.20\.20\.18'
to: '172.20.20.18'
shell> ansible-playbook test.yml
PLAY [localhost] ************************************************
TASK [replace] **************************************************
changed: [localhost] => (item={'path': 'file1', 'from': 'fostrain01\\.example\\.com', 'to': 'dbfoxtrain01.example.com'})
changed: [localhost] => (item={'path': 'file2', 'from': '171\\.20\\.20\\.18', 'to': '172.20.20.18'})
PLAY RECAP ******************************************************
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> cat file1
dbfoxtrain01.example.com
fostrain02.example.com
fostrain03.example.com
shell> cat file2
ServerIPS 171.20.20.16 171.20.20.17 172.20.20.18 171.20.20.19 171.20.20.20
ServerIPS 171.20.20.21 171.20.20.22 171.20.20.23 171.20.20.24 171.20.20.25
这篇关于Ansible 将两个不同的文件变量添加到单个任务中,循环遍历多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!