Ansible 嵌套循环通过清单加上列表的外循环 [英] Ansible nested loops through inventories plus outer loop of a list

查看:36
本文介绍了Ansible 嵌套循环通过清单加上列表的外循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下剧本:

---
- 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 }}

如果我将名为group"的变量定义为列表而不是单个变量,我该如何更改循环部分?例如:

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'

谢谢和问候.

斯蒂夫

推荐答案

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

我认为会做你想做的

这篇关于Ansible 嵌套循环通过清单加上列表的外循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆