I have a playbook which takes a input list of comma-separated values from the user:
a,b,c,d
And turns it into a list using split (extra_hosts is the variable that stores the input from the user)
- name: Generate hosts list
set_fact:
hosts_list: []
- name: Build hosts list
set_fact:
san_list: "{{ hosts_list + [host] }}"
loop_control:
loop_var: host
with_items: "{{ extra_hosts | split(',') }}"
Now this works fine so far. If I debug at this point, I get:
ok: [localhost] => {
"msg": [
"a",
"b",
"c",
"d"
]
}
What I want to do now is to output into a file, the list in the format:
Host 1: a
Host 2: b
Host 3: c
Host 4: d
I can easily iterate over the list and output using this:
- name: Append hosts to file
lineinfile:
path: "hosts.conf"
line: "Host: {{ host }}"
loop_control:
loop_var: host
with_items: "{{ hosts_list }}"
However, this outputs (obviously) as:
Host: a
Host: b
Host: c
Host: d
But I don't think I can iterate with two variables (the second being the sequence module), so I can't do something like:
- name: Append hosts to file
lineinfile:
path: "hosts.conf"
line: "Host: {{ host }}"
loop_control:
loop_var: host
with_items: "{{ hosts_list }}"
loop_control:
loop_var: id
with_sequence: start=1
I was thinking maybe somehow converting the list into a dict, so I'd end up with something like:
{id: 1, host: "a"},
{id: 2, host: "b"},
{id: 3, host: "c"},
{id: 4, host: "d"}
Then I can then just use something like {{ item.id }} and {{ item.host }} to write those out -- but to build the dict, I would still need to iterate with two pointers -- an incremental value, and the pointer within the list.
Is there way I can do this, and what is the best/correct way of doing it?