ruk·si

Ansible
Inventories

Updated at 2017-03-21 23:46

Inventory is a collection of machines that Ansible is meant to manage.

# hosts file contents
web ansible_ssh_host=198.199.115.67
ansible <host-pattern> -i <hosts-file> -m <module-name> -a <arguments>

ansible all -i hosts -m ping

# Or if you have ansible.cfg with `inventory` defined:
ansible all -m ping

ansible webservers -m service -a "name=httpd state=restarted"

Inventory can also be a Python file. These are called dynamic inventories where Python script should print JSON about the current infrastructure.

./my_inventory.py --list
# ... should json.dump something like the following
{
  "webserver": {
    "vars": {
      "ansible_user": "ubuntu"
    },
    "hosts": [
      "34.251.191.44"
    ]
  },
  "worker": {
    "vars": {
      "ansible_user": "ubuntu"
    },
    "hosts": [
      "34.248.245.45",
      "34.248.245.46",
      "34.248.245.47",
    ]
  }
}
# Usage example:
ansible -i my_inventory.py all -m ping -k -v

Sources