2017-06-03 69 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來說,這只是一個清晰而不是表現的問題。

相關問題