Python 如何在不指定清单但直接指定主机的情况下运行 Ansible?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17188147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Ansible without specifying the inventory but the host directly?
提问by Ngoc Tran
I want to run Ansible in Python without specifying the inventory file through (ANSIBLE_HOST) but just by:
我想在 Python 中运行 Ansible 而不通过 (ANSIBLE_HOST) 指定库存文件,而只是通过:
ansible.run.Runner(
module_name='ping',
host='www.google.com'
)
I can actually do this in fabric easily but just wonder how to do this in Python. On the other hand, documentation of the Ansible API for python is not really complete.
我实际上可以很容易地在 fabric 中做到这一点,但只是想知道如何在 Python 中做到这一点。另一方面,Ansible API for python 的文档并不完整。
回答by bnsmith
This isn't a full answer, but there's some discussion of this topic in this discussion thread. At the end of the first post in that thread, a suggestion is made to create a wrapper bash script for ansible-playbook, which is a bit of a hack but workable.
这不是一个完整的答案,但在这个讨论主题中有一些关于这个主题的讨论。在该线程的第一篇文章的末尾,建议为 ansible-playbook 创建一个包装器 bash 脚本,这有点小技巧但可行。
Other things that I've been considering are the use of 'ansible-pull' and the creation of an ansible inventory plugin. I'm also interested in finding the answer to this question, and I'll keep updating this answer as I find more information.
我一直在考虑的其他事情是使用“ansible-pull”和创建 ansible 清单插件。我也有兴趣找到这个问题的答案,当我找到更多信息时,我会不断更新这个答案。
回答by trkoch
Surprisingly, the trick is to append a ,
令人惊讶的是,诀窍是附加一个 ,
# Host and IP address
ansible all -i example.com,
ansible all -i 93.184.216.119,
or
或者
# Requires 'hosts: all' in your playbook
ansible-playbook -i example.com, playbook.yml
The host parameter preceding the ,can be either a hostname or an IPv4/v6 address.
前面的主机参数,可以是主机名或 IPv4/v6 地址。
回答by seumasmac
You can do this with:
你可以这样做:
hosts = ["webserver1","webserver2"]
webInventory = ansible.inventory.Inventory(hosts)
webPing = ansible.runner.Runner(
pattern='webserver*',
module_name='ping',
inventory = webInventory
).run()
Whatever is in hosts becomes your inventory and you can search it with pattern (or do "all").
主机中的任何内容都会成为您的库存,您可以使用模式搜索它(或执行“全部”)。
回答by billkw
I also needed to drive the Ansible Python API, and would rather pass hosts as arguments rather than keep an inventory. I used a temporary file to get around Ansible's requirement, which may be helpful to others:
我还需要驱动Ansible Python API,并且宁愿将主机作为参数传递而不是保留清单。我使用了一个临时文件来解决 Ansible 的要求,这可能对其他人有帮助:
from tempfile import NamedTemporaryFile
from ansible.inventory import Inventory
from ansible.runner import Runner
def load_temporary_inventory(content):
tmpfile = NamedTemporaryFile()
try:
tmpfile.write(content)
tmpfile.seek(0)
inventory = Inventory(tmpfile.name)
finally:
tmpfile.close()
return inventory
def ping(hostname):
inventory = load_temporary_inventory(hostname)
runner = Runner(
module_name='ping',
inventory=inventory,
)
return runner.run()
回答by Arbab Nazar
I know this question is really old but think that this little trick might helpful for future users who need help for this:
我知道这个问题真的很老,但认为这个小技巧可能对需要帮助的未来用户有所帮助:
ansible-playbook -i 10.254.3.133, site.yml
if you run for local host:
如果您为本地主机运行:
ansible-playbook -i localhost, --connection=local site.yml
The trick is that after ip address/dns name, put the comma inside the quotes and requires 'hosts: all' in your playbook.
诀窍是在 ip 地址/dns 名称之后,将逗号放在引号内,并hosts: all在您的剧本中要求 ' '。
Hope this will help.
希望这会有所帮助。
回答by Abhijith
There seems to be not direct way to give a pattern. This is my hack to solve it.
似乎没有直接的方法来给出模式。这是我解决它的技巧。
echo fldn[3789:3799].mysite.com >test; ansible all -i test -m ping
回答by Donn Lee
In my case, I did not want to have hosts: allin my playbook, because it would be bad if someone ran the playbook and forgot to include -i 10.254.3.133,
就我而言,我不想hosts: all在我的剧本中加入,因为如果有人运行剧本而忘记包括-i 10.254.3.133,
This was my solution (ansible 2.6):
这是我的解决方案(ansible 2.6):
$ ansible-playbook myplaybook.yml -e "{target: 10.1.1.1}" -i 10.1.1.1, ...
And then, in the playbook:
然后,在剧本中:
- hosts: "{{ target }}"
remote_user: donn
vars_files:
- myvars
roles:
- myrole
This is a special use-case when I need to provision a host and I don't want/need to add it to the inventory.
这是一个特殊的用例,当我需要配置一个主机并且我不想/不需要将它添加到清单中时。

