How to list a subelement from hostvars for all ansible_play_hosts in a single line

296 views
Skip to first unread message

jean-christophe manciot

unread,
May 7, 2021, 8:13:34 AM5/7/21
to Ansible Project
The use case is to run a specific task once over all hosts only when at least one host failed the previous task.

It could be something like:

        - name: How to list a subelement from hostvars for all ansible_play_hosts
          gather_facts: True
          hosts: all
          strategy: debug
          tasks:
                - name: Run first task
                  include_tasks: "task.yml"
                  register: return_task
                  ignore_errors: yes

                - name: Run second task when first task has failed on at least one playing host
                  debug:
                        msg: "first task has failed on at least one of: {{ ansible_play_hosts }}"
                  when: ansible_play_hosts | map('extract', hostvars, 'return_task.failed') | list is any

The conditional makes the last task fail with:
'ansible.vars.hostvars.HostVarsVars object' has no attribute 'return_task.failed'

Any suggestion to correct the conditional?

jean-christophe manciot

unread,
May 7, 2021, 9:17:32 AM5/7/21
to Ansible Project
The solution seems to be easy actually:
when: ansible_play_hosts | map('extract', hostvars, 'return_task') | list | selectattr('failed') | list is any
Reply all
Reply to author
Forward
0 new messages