- 最近在配置ansible_runner的python调用时,出现了许多的坑,在此写下博客记录一下
- 首先使用
pip install ansible_runner和pip install ansible安装运行环境 - 然后在ansible-runner下载配置文件的demo。

下载以后在同级目录创建main.py,代码如下
import ansible_runner r = ansible_runner.run(private_data_dir='./demo', playbook='test.yml', json_mode=False) print("{}: {}".format(r.status, r.rc)) successful: 0 for each_host_event in r.events: print(each_host_event['event']) print("Final status:") print(r.stats)- 修改
demo/inventory下的hosts文件,添加需要执行命令的主机地址 修改
demo/project下的test.yml为下面所示--- - hosts: all tasks: - ping: register: out_msg - debug: msg={{out_msg}}- 修改
demo/env/下的extravars文件,删除ansible_connection: local这一行,这样就不会只访问本地 主机 - 最后运行即可
Last modification:July 19, 2021
© Allow specification reprint