ansible

How to make ansible task output line `failed: [server]`?


A failed Ansible task can have two kind of log lines:

failed: [server] ...

or

fatal: [server] ...

However I tried very hard to simulate a situation that can have the failed: one but without any luck:

All of them output fatal:

Any idea why some task has failed:?


Solution

  • failed: [server] only happen in a really specific case: when an item of a loop produce a failed state, as you can see from this line of the v2_runner_item_on_failed method of the default callback plugin:

    msg = "failed: [%s]" % (host_label,)
    

    Source: lib/ansible/plugins/callback/default.py

    So, in order to create a failed: [server], you need to be in a loop, for example, the task:

    - ansible.builtin.fail:
      loop: [~]
    

    Will produce

    failed: [localhost] (item=None) => changed=false 
      ansible_loop_var: item
      item: null
      msg: Failed as requested from task