Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible stdout Formatting

Assuming the below tasks:

shell: "some_script.sh"
 register: "some_script_result"
debug:
   msg: "Output: {{ some_script_result.stdout_lines }}

I receive the below output:

"msg": "Output: [u'some_value',u'some_value2,u'some_value3]"

How do I get the output to print as?

"msg":
Output:
some_value
some_value2
some_value3

Ansible version is 2.4.2.

Thank you!

like image 542
Kimmel Avatar asked Apr 24 '18 19:04

Kimmel


People also ask

What is Ansible output format?

By default Ansible sends output of the plays, tasks and module arguments to STDOUT in the format that is not suitable for human reading. Starting from Ansible 2.5, the default output format can be changed to a human-readable using the callback plugin.

What is stdout in Ansible?

stdout. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities. "stdout": "foo!"

How do you capture command output in Ansible?

To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.


2 Answers

Try this option. You’ll love it.

There's a new YAML callback plugin introduced with Ansible 2.5 — meaning any machine running Ansible 2.5.0 or later can automatically start using this format without installing custom plugins.

To use it, edit your ansible.cfg file (either global, in /etc/ansible/ansible.cfg, or a local one in your playbook/project), and add the following lines under the [defaults] section:

# Use the YAML callback plugin.
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True

Now I can easily read through your output message

If you get the following error:

ERROR! Invalid callback for stdout specified: yaml

run

ansible-galaxy collection install community.general
like image 81
imjoseangel Avatar answered Sep 30 '22 01:09

imjoseangel


Another option:

https://blog.alexgittings.com/improving-the-ansible-output-with-anstomlog/

just store it inside ansible/ansible.cfg

➜ tree ansible     
ansible
├── ansible.cfg
├── callbacks
│   ├── anstomlog.py
└── playbooks
    └── nginx.yaml

ANSIBLE_CONFIG=ansible/ansible.cfg ansible-playbook -u centos --private-key .ssh/key -i `terraform output bastion_ip`, ansible/playbooks/nginx.yaml

enter image description here

like image 36
DmitrySemenov Avatar answered Sep 30 '22 01:09

DmitrySemenov