Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register a variable output with Ansible CLI / Ad-Hoc

Can I register the output of a task? Is there an argument with ansible command for that ?

This is my command:

ansible all -m ios_command -a"commands='show run'" -i Resources/Inventory/hosts

I need this, because the output is a dictionary and I only need the value for one key. If this is not possible, is there a way to save the value of that key to a file?

like image 651
David Avatar asked Oct 18 '25 15:10

David


2 Answers

I have found that you can convert ansible output to json when executing playbooks with "ANSIBLE_STDOUT_CALLBACK=json" preceding the "ansible-playbook" command. Example:

ANSIBLE_STDOUT_CALLBACK=json ansible-playbook Resources/.Scripts/.Users.yml

This will give you a large output because it also shows each host's facts, but will have a key for each host on each task.

This method is not possible with ansible command, but it's output is similar to json. It just shows "10.20.30.111 | SUCCESS =>" before the main bracket.

Source

like image 89
David Avatar answered Oct 20 '25 17:10

David


Set the following in your ansible.cfg under the [defaults] group

bin_ansible_callbacks=True

Then as @D_Esc mentioned, you can use ANSIBLE_STDOUT_CALLBACK=json ansible all -m ios_command -a"commands='show run'" -i Resources/Inventory/hosts and can get the json output which you can try to parse.

I have not found a way to register the output to a variable using ad-hoc commands

like image 43
Meera Anand Avatar answered Oct 20 '25 18:10

Meera Anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!