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
The task is to write one new update_ip.yml task for finding the ip_details.yaml file path and replacing/updating the ip input and id username and password.
I tried :
update_ip.yml
- lineinfile:
path: values.yml
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
backrefs: true
loop:
- {regexp: '^(\s*)ip1.image.name: <user ip>(.*)$', line: '\1name: {{ ip1_image_name }}\2'}
- {regexp: '^(\s*)ip2.image.name: <user ip>(.*)$', line: '\1name: {{ ip2_image_name }}\2'}
- {regexp: '^(\s*)ip2.id.username:\s+.*?(\s+#.*)$', line: '\1username: {{ ip_username }}\2'}
- {regexp: '^(\s*)ip2.id.password:\s+.*?(\s+#.*)$', line: '\1password: {{ ip_password }}\2'}
Final expectation : 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
i suggest you to use a template module:
create your template file ip_details.j2
in folder templates
(same level than your playbook)
---
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
create playbook:
- 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
result in 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