gpt4 book ai didi

Ansible 按对象字段的值在列表中查找对象

转载 作者:行者123 更新时间:2023-12-04 18:24:31 24 4
gpt4 key购买 nike

我有这个结构。对于每个主机,这个结构可能有更多或更少的项目。在一项任务中,我想知道是否有一个使用特定名称定义的模块。

---
web_module_list:
- module_name: LaunchPad
module_version: 1.4.0
- module_name: Manager
module_version: 1.6.0
- module_name: NetworkInventory
module_version: 1.1.4
- module_name: Reporting
module_version: 1.0.18
- module_name: TriadJ
module_version: 4.1.0-1.1.7

例如,我想知道是否定义了 module_name Reporting,以便为它包含一组任务。
- set_fact:
reporting: if the web_module_list contains an item with module_name Reporting then true else false
woprinting: if the web_module_list contains an item with module_name WorkOrderPrinting then true else false


- name: If the reporting module is listed in inventory then execute its tasks
include: reporting.yml
when: reporting

- name: If the work order printing module is listed in inventory then execute its tasks
include: woprinting.yml
when: woprinting

我怎样才能让它工作?
有没有更好的办法?

最佳答案

您可以从您的web_module_list 创建一个键值列表。并检查字符串是否在该列表中:

- name: If the reporting module is listed in inventory then execute its tasks
include: reporting.yml
when: "'Reporting' in (web_module_list | map(attribute='module_name') )"

- name: If the work order printing module is listed in inventory then execute its tasks
include: woprinting.yml
when: "'WorkOrderPrinting' in (web_module_list | map(attribute='module_name') )"

您可能想为列表设置一个事实,这样过滤就不会重复,但对于 Ansible,它是一个清晰的问题,而不是性能问题。

关于Ansible 按对象字段的值在列表中查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44343146/

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