The playbook below
- hosts: localhost
gather_facts: false
vars:
oracle_home: /oracle/db/19.0.0
rblist: [30621255, 29213893]
tasks:
- include_vars:
file: tracker.txt
name: tracker
- copy:
dest: tracker.txt
content: |
{
'{{ oracle_home }}':
{{ tracker[oracle_home]|combine({'rollback': rollback}) }}
}
vars:
rsuccess: "{{ dict(rblist|product(['success'])) }}"
rollback: "{{ tracker[oracle_home]['rollback']|combine(rsuccess) }}"
- hosts: localhost
gather_facts: false
tasks:
- include_vars:
file: tracker.txt
name: tracker
- debug:
var: tracker
gives
tracker:
/oracle/db/19.0.0:
apply:
'28318139': null
'28788272': null
'29213893': null
'31431771': null
'32044280': null
rollback:
'28318139': null
'29213893': success
'29802382': null
'29867728': null
'30621255': success
The file is stored in JSON
shell> cat tracker.txt
{"/oracle/db/19.0.0": {"rollback": {"30621255": "success", "29213893": "success", "29867728": null, "29802382": null, "28318139": null, "30621255": "success", "29213893": "success"}, "apply": {"28318139": null, "29213893": null, "28788272": null, "31431771": null, "32044280": null}}}
Q: "Retain the file track.txt the way ... since its readability is better."
A: It is possible. There are 2 options on how to use Jinja. Create the complete dictionary tracker and use filter to_nice_yaml
- copy:
dest: tracker.txt
content: |
{{ tracker|to_nice_yaml }}
The next option is to format the content by indent and to_nice_yaml. Both options would make the code more complex and error-prone. I'd keep JSON. There is a plethora of options on how to display JSON. For example
shell> cat tracker.txt | jq .
{
"/oracle/db/19.0.0": {
"rollback": {
"30621255": "success",
"29213893": "success",
"29867728": null,
"29802382": null,
"28318139": null
},
"apply": {
"28318139": null,
"29213893": null,
"28788272": null,
"31431771": null,
"32044280": null
}
}
}
"Understand the code"
- Read the file and put the variables into the dictionary tracker
- include_vars:
file: tracker.txt
name: tracker
- debug:
var: tracker
gives
tracker:
/oracle/db/19.0.0:
apply:
28318139: null
28788272: null
29213893: null
31431771: null
32044280: null
rollback:
28318139: null
29213893: null
29802382: null
29867728: null
30621255: null
- Create the dictionary of succeeded patches
- debug:
var: rsuccess
vars:
rsuccess: "{{ dict(rblist|product(['success'])) }}"
gives
rsuccess:
29213893: success
30621255: success
- Combine the rollback dictionary with rsuccess
- debug:
msg: "{{ rollback }}"
vars:
rsuccess: "{{ dict(rblist|product(['success'])) }}"
rollback: "{{ tracker[oracle_home]['rollback']|combine(rsuccess) }}"
gives
rollback:
28318139: null
29213893: success
29802382: null
29867728: null
30621255: success
- Combine updated rollback with oracle_home
- debug:
msg: "{{ tracker[oracle_home]|combine({'rollback': rollback}) }}"
vars:
rsuccess: "{{ dict(rblist|product(['success'])) }}"
rollback: "{{ tracker[oracle_home]['rollback']|combine(rsuccess) }}"
gives
msg:
apply:
28318139: null
28788272: null
29213893: null
31431771: null
32044280: null
rollback:
28318139: null
29213893: success
29802382: null
29867728: null
30621255: success
- Create the text block
- debug:
msg: |
{
'{{ oracle_home }}':
{{ tracker[oracle_home]|combine({'rollback': rollback}) }}
}
vars:
rsuccess: "{{ dict(rblist|product(['success'])) }}"
rollback: "{{ tracker[oracle_home]['rollback']|combine(rsuccess) }}"
gives
msg:
/oracle/db/19.0.0:
apply:
28318139: null
28788272: null
29213893: null
31431771: null
32044280: null
rollback:
28318139: null
29213893: success
29802382: null
29867728: null
30621255: success
Q: with_items: "{{ rblist }}" "tracker[oracle_home]['rollback'][item].item": "VARIABLE IS NOT DEFINED!"
A: The items of rblist are integers. For some reason, the indexes do not evaluate to strings by default. Probably because of possible expected arithmetic inside the indexes. But, the keys in JSON have been stored as strings. For example, the task below
- debug:
msg: "{{ item }} {{ item|type_debug }}"
loop: "{{ tracker[oracle_home]['rollback'].keys()|list }}"
vars:
oracle_home: /oracle/db/19.0.0
gives
msg: 30621255 str
msg: 29213893 str
msg: 29867728 str
msg: 29802382 str
msg: 28318139 str
It's necessary to cast the keys to string. For example, the task below
- debug:
var: tracker[oracle_home]['rollback'][item|string]
loop: "{{ rblist }}"
vars:
oracle_home: /oracle/db/19.0.0
rblist: [30621255, 29213893]
gives
tracker[oracle_home]['rollback'][item|string]: success
tracker[oracle_home]['rollback'][item|string]: success