2017-06-03 45 views
2

我有这样的结构。对于每个主机,这个结构可能有更多或更少的项目。在一个任务中,我想知道是否有一个用特定名称定义的模块。Ansible通过对象字段的值在列表中找到对象

--- 
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报告定义,这样我包括一组它的任务。

- 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 

我如何得到这个工作? 有没有更好的方法?

回答

2

您可以从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来说,这只是一个清晰而不是表现的问题。

相关问题