Ansible:如何通知 Python 脚本有关任务的结果? [英] Ansible: How to inform a python script about the outcome of a task?
问题描述
我有一个简单的剧本来从共享中获取文件并将其复制到远程 Windows 客户端:
I have simple playbook to fetch and copy files from a share to remote windows clients:
- name: Test chocolatey module
hosts: win_clones
vars_files:
- /fullpath/hosts_vars
tasks:
- name: Fetching and copying the file on the client ...
win_get_url:
url: 'ftp://172.20.0.5/choco-repo/{{ item }}'
dest: 'C:\Test\{{ item }}'
with_items: "{{ clients[machine].to_install }}"
我使用子进程从 python 脚本运行此剧:
I run this play from a python script using subprocess:
for i in clients:
machine = "machine="
limit = "--limit="
an2 = subprocess.Popen(["ansible-playbook", "fetch.yml","-e", machine+i, limit+i], cwd='/home/diego/work/gitl_repo/ansible-software-deployment')
an2.wait()
当任务因任何原因失败时,我可以在终端中看到:
When a task fails for whatever reason I can see that in my terminal:
TASK [Fetching and copying the file on the client ...] *************************
failed: [cl1] (item=banana) => {"failed": true, "item": "banana", "msg": "Error downloading ftp://172.20.0.5/.....
是否可以将此信息传递给我的 python 脚本?换句话说,我怎么能告诉 python 某个任务的结果,即它是否失败或是否通过.谢谢
Is it possible to pass this information to my python script? In other words, How could I tell python about the outcome of a certain task, i.e. if it failed or if it went through. Thanks
推荐答案
您可以通过 配置文件或环境变量ANSIBLE_STDOUT_CALLBACK
.ansible-playbook
会将带有执行结果的巨大 json 文件打印到标准输出.
You can set stdout_callback = json
via configuration file or env variable ANSIBLE_STDOUT_CALLBACK
.
ansible-playbook
will print huge json file with execution result to stdout.
这篇关于Ansible:如何通知 Python 脚本有关任务的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!