简体   繁体   中英

Ansible nested loops through inventories plus outer loop of a list

Suppose I have the following playbook:

---
- hosts: all
  gather_facts: False
  vars:
    group: 'dev'

  tasks:
  - name: Just loop through a group and group_vars
    debug:
      msg: 'group is {{group}} target is {{item.0}} port is {{item.1}}'
    loop: >
      {{ groups[group] |
         product(hostvars[groups[group][0]]["ports"]) |
         list }}

How can I change the loop part in case I have the variable named "group" defined as list and not as a single variable? For example:

vars:
  group:
  - 'dev'
  - 'int'

Thanks and regards.

Stef

loop: >
   {{ groups | dict2items | 
      selectattr("key", "in", group_list ) |
      map(attribute="value") | list |
      ### and then the rest of your pipeline }}

I think will do what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM