Ansible: How to inform a python script about the outcome of a task?

前端 未结 2 879
你的背包
你的背包 2020-12-21 23:16

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:
  - /         


        
相关标签:
2条回答
  • 2020-12-21 23:23

    You can run the playbook from the Python API (though it's more code and Ansible doesn't officially support it for external callers). Use the example from Python API 2.0 as a baseline, and instead of loading a play, load a playbook:

    from ansible.playbook import Playbook
    
    playbook = Playbook(loader).load('fetch.yml' loader=loader, variable_manager=variable_manager)
    

    Then substitute

    result = tqm.run(play)
    

    for this

    for play in playbook.get_plays():
        result = tqm.run(play)
    

    You'll need some adaptation to get it to iterate multiple times per your for loop above, but this is the framework.

    0 讨论(0)
  • 2020-12-21 23:30

    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.

    0 讨论(0)
提交回复
热议问题