簡體   English   中英

如何在仍然使用本地文件作為我的游戲的同時以編程方式運行 ansible play?

[英]How can I run an ansible play programmatically while still using a local file as my play?

設置/約束

我想運行一個位於 /tmp/run_tests.yml 的 ansible play,我想在 python 腳本中執行運行,而不是命令行(這些是對我正在處理的問題的一般限制)。 我已經嘗試了幾種不同的方法,這些方法都感覺像是對 Runner 類進行逆向工程的猜測工作,但都沒有成功。 我希望知道這是否可行以及代碼是什么樣的。

如果我想運行單個命令,我可以簡單地使用 Ansible API 的運行程序:

works.py(使用帶有模塊的 Runner 的簡單示例)

ansible.runner.Runner(**{
            "pattern": '*',
            "module_name": 'ping',
            "inventory": webInventory,
            "remote_user": self.username,
            "private_key_file": self.private_key
        }).run() 

doesnotwork.py(嘗試使用 runner 進行播放)

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'}}}

從源代碼來看,該錯誤表明缺少 shebang,並且由於我是 ansible 的新手,因此我推測傳遞 yml 文件不是 module_name 的合適文件。 為了運行我的 python 游戲,runner 命令必須是什么樣子的?

我相信您在 3 個多月后已經弄清楚了這一點,但是 Ansible Runner對象中的module_name應該是 Ansible 的模塊索引中可用的模塊,例如“apt”或“service”。

我認為您正在尋找Ansible 的 ansible ansible-playbook等價物,它有自己的run類方法。

看起來以編程方式運行 Playbook工作示例可能在這里

您可以在 Ansible 的 github 存儲庫中找到CLI ansible-playbook示例以及它的使用方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM