How to run Ansible without specifying the inventor

2019-03-07 22:09发布

I want to run Ansible in Python without specifying the inventory file through (ANSIBLE_HOST) but just by:

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.

6条回答
叛逆
2楼-- · 2019-03-07 22:12

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.

查看更多
老娘就宠你
3楼-- · 2019-03-07 22:13

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").

查看更多
混吃等死
4楼-- · 2019-03-07 22:25

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.

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.

查看更多
成全新的幸福
5楼-- · 2019-03-07 22:27

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
查看更多
疯言疯语
6楼-- · 2019-03-07 22:36

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.

Hope this will help.

查看更多
Rolldiameter
7楼-- · 2019-03-07 22:36

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:

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()
查看更多
登录 后发表回答