Reputation: 320
G'day everyone.
I have two ansible tasks like this, running over the 'workernodes' group in the inventory file:
- name: count remote files
find:
paths: "{{ data_path }}"
register: exported_files
- name: sum up number of remote files
set_fact:
sum_of_exported_files: "{{ groups[ workernodes ] | map('extract', hostvars, 'exported_files') | map(attribute='matched') | sum }}"
run_once: yes
This works fine as long as all are nodes available. But if the find-Task count not run on any of the workernodes i get in error in the sum-up task. There is no exported_files variable on for this node, so the mapping to the attribute fails.
How can i filter for the existance of the exported_files attribute in the extarcted hostvars list?
Please EnlightMe
Thanks.
Upvotes: 2
Views: 1453
Reputation: 320
G'day everyone,
finally i found the (really simple) answer by myself:
sum_of_exported_files: "{{ groups[ workernodes ] | map('extract', hostvars, 'exported_files') | select("defined") | map(attribute='matched') | select("number") | sum }}"
Thanks isabellema for your thought that helped me to think it over.
Upvotes: 1
Reputation: 112
How about adding this when:
clause:
when: groups[ workernodes ] | map('extract', hostvars, 'exported_files')|list|first|length > 0"
Upvotes: 1