Register a variable output with Ansible CLI / Ad-Hoc

假如想象 提交于 2019-12-08 14:50:43

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/50465035/register-a-variable-output-with-ansible-cli-ad-hoc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!