Search code examples
ansibleansible-2.xansible-module

Ansible debug module : display debug message only once no matter hosts count


I would like to display a debug message in my playbook.

This is my playbook:

[blablabla]
- debug:
    msg:
    - "######################################################################"
    - "                    DIAGNOSTIC AGENT INSTALLATION                     "
    - "######################################################################"
[blablabla]

When I run this playbook on multiple target , the debug message on console output is displayed as many times as hosts count (as intended).

TASK [sifac-sap-diagnosticagent : debug] *******
task path: /etc/ansible/xxxxxx/tasks/main.yml:158
ok: [server1] => {
    "msg": [
        "######################################################################",
        "                    DIAGNOSTIC AGENT INSTALLATION                     ",
        "######################################################################"
    ]
}
ok: [server2] => {
    "msg": [
        "######################################################################",
        "                    DIAGNOSTIC AGENT INSTALLATION                     ",
        "######################################################################"
    ]
}
ok: [server3] => {
    "msg": [
        "######################################################################",
        "                    DIAGNOSTIC AGENT INSTALLATION                     ",
        "######################################################################"
    ]
}
ok: [server4] => {
    "msg": [
        "######################################################################",
        "                    DIAGNOSTIC AGENT INSTALLATION                     ",
        "######################################################################"
    ]
}

I would like to display this message only once, no matter hosts count. This in order to not overload my output.

Should I use option bypass_host_loop from debug ansible module ? Another method ?

enter image description here

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html


Solution

  • Use the run_once keyword on the task. It will run the task only for the first available host.

    Look also for run_once in the playbook keywords.

    [blablabla]
    - debug:
      msg:
        - "######################################################################"
        - "                    DIAGNOSTIC AGENT INSTALLATION                     "
        - "######################################################################"
      run_once: true
    [blablabla]