Question:
I have this structure. For each host this structure may have more or less items. In a task I want to know if there is a a module defined with a particular name.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
--- 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 |
For instance I want to know if the module_name Reporting is defined so that I include a set of tasks for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- 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 |
How do I get this to work?
Is there a better way?
Answer:
You can create a list of values of a key from your web_module_list
and check if a string is on that list:
1 2 3 4 5 6 7 8 |
- 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') )" |
You might want to set a fact for the list, so that the filtering is not repeated, but with Ansible it’s rather a matter of clarity than performance.