用于查找和替换嵌套键值的剧本任务
Playbook task for Find & replace nested key values
ip_details.yaml
---
ip1:
image:
name: <ip input> # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: <ip input> # ip address 2
id:
username: # ip username
password: # ip Password
fetch_value.yml
ip1_image_name: 192.0.0.1
ip2_image_name: 192.0.0.2
ip_username: user@123
ip_password: pass@123
任务是编写一个新的 update_ip.yml 任务,用于查找 ip_details.yaml 文件路径和 replacing/updating ip 输入以及 id 用户名和密码。
我试过了:
update_ip.yml
- lineinfile:
path: values.yml
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
backrefs: true
loop:
- {regexp: '^(\s*)ip1.image.name: <user ip>(.*)$', line: 'name: {{ ip1_image_name }}'}
- {regexp: '^(\s*)ip2.image.name: <user ip>(.*)$', line: 'name: {{ ip2_image_name }}'}
- {regexp: '^(\s*)ip2.id.username:\s+.*?(\s+#.*)$', line: 'username: {{ ip_username }}'}
- {regexp: '^(\s*)ip2.id.password:\s+.*?(\s+#.*)$', line: 'password: {{ ip_password }}'}
最终预期:ip_details.yml
---
ip1:
image:
name: 192.0.0.1 # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: 192.0.0.2 # ip address 2
id:
username: user@123 # ip username
password: pass@123 # ip Password
我建议您使用 template 模块:
在文件夹 templates
中创建您的模板文件 ip_details.j2
(与您的剧本相同级别)
---
ip1:
image:
name: {{ ip1_image_name }} # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: {{ ip2_image_name }} # ip address 2
id:
username: {{ ip_username }} # ip username
password: {{ ip_password }} # ip Password
创建剧本:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
ip1_image_name: 192.0.0.1
ip2_image_name: 192.0.0.2
ip_username: user@123
ip_password: pass@123
tasks:
- name: template
template:
src: ip_details.j2
dest: ip_details.yml
结果 ip_details.yml
:
---
ip1:
image:
name: 192.0.0.1 # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: 192.0.0.2 # ip address 2
id:
username: user@123 # ip username
password: pass@123 # ip Password
ip_details.yaml
---
ip1:
image:
name: <ip input> # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: <ip input> # ip address 2
id:
username: # ip username
password: # ip Password
fetch_value.yml
ip1_image_name: 192.0.0.1
ip2_image_name: 192.0.0.2
ip_username: user@123
ip_password: pass@123
任务是编写一个新的 update_ip.yml 任务,用于查找 ip_details.yaml 文件路径和 replacing/updating ip 输入以及 id 用户名和密码。
我试过了:
update_ip.yml
- lineinfile:
path: values.yml
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
backrefs: true
loop:
- {regexp: '^(\s*)ip1.image.name: <user ip>(.*)$', line: 'name: {{ ip1_image_name }}'}
- {regexp: '^(\s*)ip2.image.name: <user ip>(.*)$', line: 'name: {{ ip2_image_name }}'}
- {regexp: '^(\s*)ip2.id.username:\s+.*?(\s+#.*)$', line: 'username: {{ ip_username }}'}
- {regexp: '^(\s*)ip2.id.password:\s+.*?(\s+#.*)$', line: 'password: {{ ip_password }}'}
最终预期:ip_details.yml
---
ip1:
image:
name: 192.0.0.1 # ip address 1
ip2:
config:
range: 192.0.0.0
image:
name: 192.0.0.2 # ip address 2
id:
username: user@123 # ip username
password: pass@123 # ip Password
我建议您使用 template 模块:
在文件夹
templates
中创建您的模板文件ip_details.j2
(与您的剧本相同级别)--- ip1: image: name: {{ ip1_image_name }} # ip address 1 ip2: config: range: 192.0.0.0 image: name: {{ ip2_image_name }} # ip address 2 id: username: {{ ip_username }} # ip username password: {{ ip_password }} # ip Password
创建剧本:
- name: testplaybook jinja2 hosts: localhost gather_facts: no vars: ip1_image_name: 192.0.0.1 ip2_image_name: 192.0.0.2 ip_username: user@123 ip_password: pass@123 tasks: - name: template template: src: ip_details.j2 dest: ip_details.yml
结果
ip_details.yml
:--- ip1: image: name: 192.0.0.1 # ip address 1 ip2: config: range: 192.0.0.0 image: name: 192.0.0.2 # ip address 2 id: username: user@123 # ip username password: pass@123 # ip Password