Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does --inventory option in Ansible work when it is given value with a trailing comma?

Tags:

syntax

ansible

Man pages for ansible and ansible-playbook define -i option as:

-i PATH, --inventory=PATH
       The PATH to the inventory hosts file, which defaults to
       /etc/ansible/hosts.

Yet to run on a local system the following syntax is used in examples:

ansible -i "localhost," -c local -m ping localhost

What exactly is this "localhost," with comma at the end (otherwise it is treated as filename) and how does it relate to PATH?

like image 715
techraf Avatar asked Oct 24 '15 11:10

techraf


People also ask

How does Ansible dynamic inventory work?

In Ansible, Dynamic inventory is generated either by scripts which are written in a programming language like python, php etc. or using available inventory plugins. When using script, they gets all real time data from the target source environments, like Cloud platforms AWS, OpenStack, GCP etc.

How do you calculate Ansible inventory?

The default location for the inventory file is /etc/ansible/hosts. You can also create project-specific inventory files in alternate locations. The inventory file can list individual hosts or user-defined groups of hosts.

How do you pass dynamic inventory in Ansible?

To tie your Ansible inventory to Cobbler, copy this script to /etc/ansible and chmod +x the file. Run cobblerd any time you use Ansible and use the -i command line option (for example, -i /etc/ansible/cobbler.py ) to communicate with Cobbler using Cobbler's XMLRPC API.


1 Answers

This is (now, at least) a documented feature. From the man page:

-i, --inventory, --inventory-file
specify inventory host path or comma separated host list. --inventory-file is deprecated

(emphasis added)

What's still not in the manual is that "comma separated host list" means that you need to add a comma even if the "list" is a single item, to distinguish between "target a single host called hostname":

$ ansible -i 'hostname,' ...

and "load inventory from a file called hostname":

$ ansible -i 'hostname,' ...

If anyone out there has time, maybe you could submit a pull request to change the help text to explain this (and to add a hyphen in "comma-separated", but maybe that's just me..)

like image 177
supervacuo Avatar answered Sep 24 '22 03:09

supervacuo