Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Ansible without specifying the inventory but the host directly?

Tags:

python

ansible

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.

like image 373
Ngoc Tran Avatar asked Jun 19 '13 09:06

Ngoc Tran


People also ask

Can you run an Ansible playbook without inventory?

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.

Which option would target a playbook to run only on certain hosts?

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.

Which command instructs Ansible to execute the playbook on all the hosts except?

yaml . playbook. yaml. This playbook file instructs Ansible to execute the instructions on all hosts.

How do I skip hosts in Ansible?

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.


2 Answers

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.

like image 122
trkoch Avatar answered Sep 22 '22 08:09

trkoch


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.

like image 43
Arbab Nazar Avatar answered Sep 19 '22 08:09

Arbab Nazar