gpt4 book ai didi

linux - Ansible将两个不同的文件变量调用到单个任务中循环遍历多个文件

转载 作者:行者123 更新时间:2023-12-03 09:50:59 26 4
gpt4 key购买 nike

fil1的内容

# 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) 替换字符串in file & 2) Replace ip in file 来调用定义的变量。

剧本:

- 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 }}

  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}}" }

什么是期望的改变:

任务 1) 替换文件中的字符串 & 任务 2) 替换文件中的 ip 合并为一个。

预期结果:

# 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

关于linux - Ansible将两个不同的文件变量调用到单个任务中循环遍历多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62869642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com