gpt4 book ai didi

loops - 如何在 Ansible 中获取子组列表?

转载 作者:行者123 更新时间:2023-12-04 19:00:05 25 4
gpt4 key购买 nike

我有一个看起来像这样的 list 文件:

[master]
host01

[nl]
host02

[us]
host03

[satellites:children]
nl
us

如何获得具有 satellites 的组列表作为他们的 parent ?

我正在寻找与此类似的解决方案:
- debug: msg="{{ item }}"
with_items: "{{ groups['satellites:children'] }}"

更新:

我能够提出的唯一解决方案是:
- debug: {{ item }}
with_items: "{{ groups }}"
when: item != "master" and item != "satellites" and item != "all" and item != "ungrouped"

但这不是很灵活。

最佳答案

您可以尝试以下方法:

  vars:
parent: 'satellites'
tasks:
# functional style
- debug: msg="{{hostvars[item].group_names | select('ne', parent) | first}}"
with_items: "{{groups[parent]}}"
# set style
- debug: msg="{{hostvars[item].group_names | difference(parent) | first}}"
with_items: "{{groups[parent]}}"

还有 select('ne', parent)可与 reject('equalto', parent) 互换取决于对您来说更易读的内容。

链接:
set theory operator
select and reject filters

根据评论更新答案。灵感来自 this thread .
vars:
parent: 'satellites'
tasks:
- name: build a subgroups list
set_fact: subgroups="{{ ((subgroups | default([])) + hostvars[item].group_names) | difference(parent) }}"
with_items: "{{groups[parent]}}"

- debug: var=subgroups

输出:
 "subgroups": [
"nl",
"us"
]

关于loops - 如何在 Ansible 中获取子组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com