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.
So, it seems this isn't widely used or documented, but it is possible to run a playbook without passing through an inventory file. It's very handy when trying to deploy hosts but you don't want to need to manage static files with host entries.
Ansible command limit option Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.
yaml . playbook. yaml. This playbook file instructs Ansible to execute the instructions on all hosts.
By using –limit argument with ansible-playbook command we can exclude a host from playbook execution. If hostname starts with “!” it will excluded from host execution.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With