小编典典

Ansible:如何通知python脚本有关任务的结果?

python

我有一个简单的剧本,用于从共享中获取文件并将文件复制到远程Windows客户端:

- 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脚本运行此剧本:

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()

当任务由于某种原因而失败时,我可以在终端中看到:

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某个任务的结果,即它是否失败或是否通过。谢谢


阅读 336

收藏
2021-01-20

共1个答案

小编典典

您可以stdout_callback = json通过配置文件或env变量进行设置ANSIBLE_STDOUT_CALLBACK
ansible-playbook将打印出具有执行结果的巨大json文件到stdout。

2021-01-20