Using an outer list to access an inner list with with_nested

So, I tried to do this, and found it didn’t work. I wasn’t terribly surprised, but it seems like a reasonable use-case.

The play is to be run on one host- a central server, and I have a list of groups that I want to perform actions for- for each host in each group.

The code looks something like this:

  1. vars:

  2. group_list: [ ‘group1’, ‘group2’, ‘group3’ ]

  3. my_script: /some/script/foo.sh

  4. tasks:

    • name: Nested task
  5. command: ${my_script} action-{{item.0}} “{{hostvars[item.1][‘some_fact’]}}”

  6. with_nested:

    • group_list
    • groups[item.0]

What happens is that ansible interprets “groups[item.0]” as a string and tries to iterate over it’s characters. Seems like a valid use case, but I can understand that re-evaluating the inner list on each outer-loop may be challenging.

It’s easily enough accomplished by unrolling the outer loop, but moderately annoying that I can’t reduce it to a nested loop.