Ansible nested loops through inventories plus outer loop of a list

空扰寡人 提交于 2019-12-24 15:09:47

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/53687709/ansible-nested-loops-through-inventories-plus-outer-loop-of-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!