[英]How can I run an ansible play programmatically while still using a local file as my play?
I want to run an ansible play located at /tmp/run_tests.yml and I want to perform the run in a python script, not command line (these are general constraints on the problem I am working on).我想运行一个位于 /tmp/run_tests.yml 的 ansible play,我想在 python 脚本中执行运行,而不是命令行(这些是对我正在处理的问题的一般限制)。 I;ve tried several different approaches which all feel like guess work on reverse engineering the Runner class but have been unsuccessful.我已经尝试了几种不同的方法,这些方法都感觉像是对 Runner 类进行逆向工程的猜测工作,但都没有成功。 I was hoping to find out if this is possible and what the code would look like.我希望知道这是否可行以及代码是什么样的。
If I want to run a single command I can simply use the Ansible API's runner:如果我想运行单个命令,我可以简单地使用 Ansible API 的运行程序:
ansible.runner.Runner(**{
"pattern": '*',
"module_name": 'ping',
"inventory": webInventory,
"remote_user": self.username,
"private_key_file": self.private_key
}).run()
hosts = ["127.0.0.0.1"] #dummy ip address
webInventory = ansible.inventory.Inventory(hosts)
runner = ansible.runner.Runner(pattern="*", )
response = runner.run(**{
"pattern": '*',
"module_name": "/tmp/run_tests.yml",
"inventory": webInventory,
"remote_user": "ubuntu",
"private_key_file": "~/.ssh/id_rsa"
})
{'contacted': {}, 'dark': {'127.0.0.1': {'failed': True, 'msg': 'module is missing interpreter line'}}}
From the source, the error suggests a shebang is missing and since I am new to ansible I speculate passing a yml file is not an appropriate file for a module_name.从源代码来看,该错误表明缺少 shebang,并且由于我是 ansible 的新手,因此我推测传递 yml 文件不是 module_name 的合适文件。 What would the runner command have to look like in order to run my python play?为了运行我的 python 游戏,runner 命令必须是什么样子的?
I'm sure you've figured this out after 3+ months, but the module_name
within an Ansible Runner
object should be a module available from Ansible's module index , such as "apt" or "service".我相信您在 3 个多月后已经弄清楚了这一点,但是 Ansible Runner
对象中的module_name
应该是 Ansible 的模块索引中可用的模块,例如“apt”或“service”。
I think you're looking for Ansible's ansible-playbook
equivalent , which has its own run
class method.我认为您正在寻找Ansible 的 ansible ansible-playbook
等价物,它有自己的run
类方法。
It looks like a working example of running a Playbook programmatically might be here .看起来以编程方式运行 Playbook的工作示例可能在这里。
You can find examples of the CLI ansible-playbook
and how it's used in Ansible's github repository .您可以在 Ansible 的 github 存储库中找到CLI ansible-playbook
示例以及它的使用方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.