community.general.diy callback – Customize the output
Note
This callback plugin is part of the community.general collection (version 10.7.5).
You might already have this collection installed if you are using the ansible package.
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.general.
You need further requirements to be able to use this callback plugin,
see Requirements for details.
To use it in a playbook, specify: community.general.diy.
New in community.general 0.2.0
Callback plugin
This plugin is a stdout callback. You can use only use one stdout callback at a time. Additional aggregate or notification callbacks can be enabled though. See Callback plugins for more information on callback plugins.
Synopsis
- Callback plugin that allows you to supply your own custom callback templates to be output. 
Requirements
The below requirements are needed on the local controller node that executes this callback.
- set as stdout_callback in configuration 
Parameters
| Parameter | Comments | 
|---|---|
| Toggle to control displaying markers when running in check mode. The markers are  Choices: 
 Configuration: 
 | |
| Toggle to control whether failed and unreachable tasks are displayed to STDERR rather than STDOUT. Choices: 
 Configuration: 
 | |
| Toggle to control displaying ‘ok’ task/host results in a task. Choices: 
 Configuration: 
 | |
| Toggle to control displaying skipped task/host results in a task. Choices: 
 Configuration: 
 | |
| Output to be used for callback on_any. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback on_file_diff. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_handler_task_start. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_include. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_no_hosts_matched. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_no_hosts_remaining. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_notify. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_play_start. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_setup. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_start. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_stats. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_task_start. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback playbook_on_vars_prompt. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_item_on_failed. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_item_on_ok. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_item_on_skipped. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_failed. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_no_hosts. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_ok. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_skipped. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_start. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_on_unreachable. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| Output to be used for callback runner_retry. Configuration: 
 | |
| Output color to be used for  Template should render a valid color value. Configuration: 
 | |
| This adds the custom stats set via the set_stats plugin to the play recap. Choices: 
 Configuration: 
 | |
| This adds output that shows when a task starts to execute for each host. Choices: 
 Configuration: 
 | |
| When a task fails, display the path to the file containing the failed task and the line number. This information is displayed automatically for every task when running with  Choices: 
 Configuration: 
 | 
Note
Configuration entries listed above for each entry type (Ansible variable, environment variable, and so on) have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up. The entry types are also ordered by precedence from low to high priority order. For example, an ansible.cfg entry (further up in the list) is overwritten by an Ansible variable (further down in the list).
Notes
Note
- Uses the ansible.builtin.default callback plugin output when a custom callback - message(msg)is not provided.
- Makes the callback event data available using the - ansible_callback_diydictionary, which can be used in the templating context for the options. The dictionary is only available in the templating context for the options. It is not a variable that is available using the other various execution contexts, such as playbook, play, task, and so on so forth.
- Options being set by their respective variable input can only be set using the variable if the variable was set in a context that is available to the respective callback. Use the - ansible_callback_diydictionary to see what is available to a callback. Additionally,- ansible_callback_diy.top_level_var_nameswill output the top level variable names available to the callback.
- Each option value is rendered as a template before being evaluated. This allows for the dynamic usage of an option. For example, - "{{ 'yellow' if ansible_callback_diy.result.is_changed else 'bright green' }}".
- Condition for all - msgoptions: if value- is None or omit, then the option is not being used. Effect: use of the- defaultcallback plugin for output.
- Condition for all - msgoptions: if value- is not None and not omit and length is not greater than 0, then the option is being used without output. Effect: suppress output.
- Condition for all - msgoptions: if value- is not None and not omit and length is greater than 0, then the option is being used with output. Effect: render value as template and output.
- Valid color values: - black,- bright gray,- blue,- white,- green,- bright blue,- cyan,- bright green,- red,- bright cyan,- purple,- bright red,- yellow,- bright purple,- dark gray,- bright yellow,- magenta,- bright magenta,- normal.
See Also
See also
- default – default Ansible screen output
- The official documentation on the default callback plugin. 
Examples
ansible.cfg: >
  # Enable plugin
  [defaults]
  stdout_callback=community.general.diy
  [callback_diy]
  # Output when playbook starts
  playbook_on_start_msg="DIY output(via ansible.cfg): playbook example: {{ ansible_callback_diy.playbook.file_name }}"
  playbook_on_start_msg_color=yellow
  # Comment out to allow default plugin output
  # playbook_on_play_start_msg="PLAY: starting play {{ ansible_callback_diy.play.name }}"
  # Accept on_skipped_msg or ansible_callback_diy_runner_on_skipped_msg as input vars
  # If neither are supplied, omit the option
  runner_on_skipped_msg="{{ on_skipped_msg | default(ansible_callback_diy_runner_on_skipped_msg) | default(omit) }}"
  # Newline after every callback
  # on_any_msg='{{ " " | join("\n") }}'
playbook.yml: >-
  ---
  - name: "Default plugin output: play example"
    hosts: localhost
    gather_facts: false
    tasks:
      - name:  Default plugin output
        ansible.builtin.debug:
          msg: default plugin output
  - name: Override from play vars
    hosts: localhost
    gather_facts: false
    vars:
      ansible_connection: local
      green: "\e[0m\e[38;5;82m"
      yellow: "\e[0m\e[38;5;11m"
      bright_purple: "\e[0m\e[38;5;105m"
      cyan: "\e[0m\e[38;5;51m"
      green_bg_black_fg: "\e[0m\e[48;5;40m\e[38;5;232m"
      yellow_bg_black_fg: "\e[0m\e[48;5;226m\e[38;5;232m"
      purple_bg_white_fg: "\e[0m\e[48;5;57m\e[38;5;255m"
      cyan_bg_black_fg: "\e[0m\e[48;5;87m\e[38;5;232m"
      magenta: "\e[38;5;198m"
      white: "\e[0m\e[38;5;255m"
      ansible_callback_diy_playbook_on_play_start_msg: "\n{{green}}DIY output(via play vars): play example: {{magenta}}{{ansible_callback_diy.play.name}}\n\n"
      ansible_callback_diy_playbook_on_task_start_msg: "DIY output(via play vars): task example: {{ ansible_callback_diy.task.name }}"
      ansible_callback_diy_playbook_on_task_start_msg_color: cyan
      ansible_callback_diy_playbook_on_stats_msg: |+2
                CUSTOM STATS
        ==============================
        {% for key in ansible_callback_diy.stats | sort %}
        {% if ansible_callback_diy.stats[key] %}
        {% if key == 'ok' %}
        {% set color_one = lookup('vars','green_bg_black_fg') %}
        {% set prefix = '      ' %}
        {% set suffix = '     ' %}
        {% set color_two = lookup('vars','green') %}
        {% elif key == 'changed' %}
        {% set color_one = lookup('vars','yellow_bg_black_fg') %}
        {% set prefix = '   ' %}
        {% set suffix = '   ' %}
        {% set color_two = lookup('vars','yellow') %}
        {% elif key == 'processed' %}
        {% set color_one = lookup('vars','purple_bg_white_fg') %}
        {% set prefix = '  ' %}
        {% set suffix = '  ' %}
        {% set color_two = lookup('vars','bright_purple') %}
        {% elif key == 'skipped' %}
        {% set color_one = lookup('vars','cyan_bg_black_fg') %}
        {% set prefix = '   ' %}
        {% set suffix = '   ' %}
        {% set color_two = lookup('vars','cyan') %}
        {% else %}
        {% set color_one = "" %}
        {% set prefix = "" %}
        {% set suffix = "" %}
        {% set color_two = "" %}
        {% endif %}
        {{ color_one }}{{ "%s%s%s" | format(prefix,key,suffix) }}{{ color_two }}: {{ ansible_callback_diy.stats[key] | to_nice_yaml }}
        {% endif %}
        {% endfor %}
    tasks:
      - name: Custom banner with default plugin result output
        ansible.builtin.debug:
          msg: "default plugin output: result example"
      - name: Override from task vars
        ansible.builtin.debug:
          msg: "example {{ two }}"
        changed_when: true
        vars:
          white_fg_red_bg: "\e[0m\e[48;5;1m"
          two: "{{ white_fg_red_bg }}    2    "
          ansible_callback_diy_playbook_on_task_start_msg: "\nDIY output(via task vars): task example: {{ ansible_callback_diy.task.name }}"
          ansible_callback_diy_playbook_on_task_start_msg_color: bright magenta
          ansible_callback_diy_runner_on_ok_msg: "DIY output(via task vars): result example: \n{{ ansible_callback_diy.result.output.msg }}\n"
          ansible_callback_diy_runner_on_ok_msg_color: "{{ 'yellow' if ansible_callback_diy.result.is_changed else 'bright green' }}"
      - name: Suppress output
        ansible.builtin.debug:
          msg: i should not be displayed
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: ""
          ansible_callback_diy_runner_on_ok_msg: ""
      - name: Using alias vars (see ansible.cfg)
        ansible.builtin.debug:
          msg:
        when: false
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: ""
          on_skipped_msg: "DIY output(via task vars): skipped example:\n\e[0m\e[38;5;4m\u25b6\u25b6 {{ ansible_callback_diy.result.task.name }}\n"
          on_skipped_msg_color: white
      - name: Just stdout
        ansible.builtin.command: echo some stdout
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: "\n"
          ansible_callback_diy_runner_on_ok_msg: "{{ ansible_callback_diy.result.output.stdout }}\n"
      - name: Multiline output
        ansible.builtin.debug:
          msg: "{{ multiline }}"
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: "\nDIY output(via task vars): task example: {{ ansible_callback_diy.task.name }}"
          multiline: "line\nline\nline"
          ansible_callback_diy_runner_on_ok_msg: |+2
            some
            {{ ansible_callback_diy.result.output.msg }}
            output
          ansible_callback_diy_playbook_on_task_start_msg_color: bright blue
      - name: Indentation
        ansible.builtin.debug:
          msg: "{{ item.msg }}"
        with_items:
          - { indent: 1, msg: one., color: red }
          - { indent: 2, msg: two.., color: yellow }
          - { indent: 3, msg: three..., color: bright yellow }
        vars:
          ansible_callback_diy_runner_item_on_ok_msg: "{{ ansible_callback_diy.result.output.msg | indent(item.indent, True) }}"
          ansible_callback_diy_runner_item_on_ok_msg_color: "{{ item.color }}"
          ansible_callback_diy_runner_on_ok_msg: "GO!!!"
          ansible_callback_diy_runner_on_ok_msg_color: bright green
      - name: Using lookup and template as file
        ansible.builtin.shell: "echo {% raw %}'output from {{ file_name }}'{% endraw %} > {{ file_name }}"
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: "\nDIY output(via task vars): task example: {{ ansible_callback_diy.task.name }}"
          file_name: diy_file_template_example
          ansible_callback_diy_runner_on_ok_msg: "{{ lookup('template', file_name) }}"
      - name: 'Look at top level vars available to the "runner_on_ok" callback'
        ansible.builtin.debug:
          msg: ''
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: "\nDIY output(via task vars): task example: {{ ansible_callback_diy.task.name }}"
          ansible_callback_diy_runner_on_ok_msg: |+2
            {% for var in (ansible_callback_diy.top_level_var_names|reject('match','vars|ansible_callback_diy.*')) | sort %}
            {{ green }}{{ var }}:
              {{ white }}{{ lookup('vars', var) }}
            {% endfor %}
          ansible_callback_diy_runner_on_ok_msg_color: white
      - name: 'Look at event data available to the "runner_on_ok" callback'
        ansible.builtin.debug:
          msg: ''
        vars:
          ansible_callback_diy_playbook_on_task_start_msg: "\nDIY output(via task vars): task example: {{ ansible_callback_diy.task.name }}"
          ansible_callback_diy_runner_on_ok_msg: |+2
            {% for key in ansible_callback_diy | sort %}
            {{ green }}{{ key }}:
              {{ white }}{{ ansible_callback_diy[key] }}
            {% endfor %}
