3

Looking for information on how to output machine readable output form ansible-playbook.

1 Answer 1

3

This is done using one of the configuration options. For a general overview of configuring Ansible, see the configuration documentation, in particular the callback plugin configuration.

Callback plugins change how Ansible provides feedback on the progress and state of tasks. There are several options for JSON output:

  1. JSON output to stdout
  2. JSON to syslog

Setting these configuration options can be done either in a configuration file (ansible.cfg) or with environment variables (ANSIBLE_XXX), depending on what you prefer. To quote the current documentation:

Changes can be made and used in a configuration file which will be searched for in the following order:

   ANSIBLE_CONFIG (environment variable if set)
   ansible.cfg (in the current directory)
   ~/.ansible.cfg (in the home directory)
   /etc/ansible/ansible.cfg

Ansible will process the above list and use the first file found, all others are ignored.

Assuming you want to send the output to stdout then, your ansible.cfg could look like:

[defaults]
callback_whitelist=json
stdout_callback=json

Using environment variables:

 ANSIBLE_CALLBACK_WHITELIST=json ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ...
3
  • Appreciate your help!
    – CLJ
    Aug 16, 2020 at 23:14
  • If instead of ansible-playbook you want to use the ansible ad-hoc CLI, make sure to also set ANSIBLE_LOAD_CALLBACK_PLUGINS=1.
    – Carl Walsh
    Oct 13, 2023 at 13:09
  • In 2021, ansible 2.11 deprecated the "whitelist" variable and instead renamed it to "enabled" i.e. ANSIBLE_CALLBACKS_ENABLED. But I think JSON is enabled by default now.
    – Carl Walsh
    Oct 13, 2023 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.