I have two registered variables, both with equal number of lines, in Ansible.
register_1.stdout_lines
has below output
1
2
...
120
register_2.stdout_lines
has below output
one
two
...
one hundred twenty
I would like to match the first value of register_1.stdout_lines
with the first value of register_2.stdout_lines
, the second value of register_1.stdout_lines
with the second value of register_2.stdout_lines
and go on till 120.
Expected output:
1 one
2 two
... ...
120 one hundred twenty
I tried with a loop, but I am getting an output as:
1 one
1 two
1 three
...
Matching two list of the same size is something you would do with the zip
filter:
Given:
- name: Spell out numbers
ansible.builtin.debug:
msg: "{{ item.0 }} {{ item.1 }}"
loop: "{{ register_1.stdout_lines | zip(register_2.stdout_lines) }}"
loop_control:
label: "{{ item.1 }}"
vars:
register_1:
stdout_lines:
- 1
- 2
- 3
- ...
- 120
register_2:
stdout_lines:
- one
- two
- three
- ...
- one hundred twenty
You will get an output of:
ok: [localhost] => (item=one) =>
msg: 1 one
ok: [localhost] => (item=two) =>
msg: 2 two
ok: [localhost] => (item=three) =>
msg: 3 three
ok: [localhost] => (item=...) =>
msg: '... ...'
ok: [localhost] => (item=one hundred twenty) =>
msg: 120 one hundred twenty
And if you need it as a big blob of text, you can join the lists back together:
- name: Spell out numbers
ansible.builtin.debug:
msg: "{{ register_1.stdout_lines
| zip(register_2.stdout_lines)
| map('join', ' ')
| join('\n') }}"
vars:
register_1:
stdout_lines:
- 1
- 2
- 3
- ...
- 120
register_2:
stdout_lines:
- one
- two
- three
- ...
- one hundred twenty
Which gives:
ok: [localhost] =>
msg: |-
1 one
2 two
3 three
... ...
120 one hundred twenty