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

发布于 2021-01-29 15:02:50

我有一个简单的剧本,用于从共享中获取文件并将文件复制到远程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某个任务的结果,即它是否失败或是否通过。谢谢

关注者
0
被浏览
100
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看