(ANSIBLE_HOST) を使用してインベントリ ファイルを指定せずに Python で Ansible を実行したいのですが、次のようにします。
ansible.run.Runner(
  module_name='ping',
  host='www.google.com'
)
私は実際にファブリックでこれを簡単に行うことができますが、Python でこれを行う方法を知りたいと思っています。一方、Python 用の Ansible API のドキュメントは完全ではありません。
驚いたことに、トリックは追加することです,
# Host and IP address
ansible all -i example.com,
ansible all -i 93.184.216.119,
また
# Requires 'hosts: all' in your playbook
ansible-playbook -i example.com, playbook.yml
の前にあるホスト パラメータ,には、ホスト名または IPv4/v6 アドレスを指定できます。
私はこの質問が本当に古いことを知っていますが、この小さなトリックは、これについて助けが必要な将来のユーザーに役立つと思います:
ansible-playbook -i 10.254.3.133, site.yml
ローカルホストで実行する場合:
ansible-playbook -i localhost, --connection=local site.yml
秘訣は、IP アドレス/DNS 名の後に、引用符の中にカンマを入れ、Playbook で ' hosts: all' を要求することです。
これが役立つことを願っています。
これは次の方法で実行できます。
hosts = ["webserver1","webserver2"]
webInventory = ansible.inventory.Inventory(hosts)
webPing = ansible.runner.Runner(
    pattern='webserver*',
    module_name='ping',
    inventory = webInventory
).run()
ホストにあるものは何でもインベントリになり、パターンで検索できます(または「すべて」を実行できます)。
また、 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()