ansible-inventory

From wikieduonline
Revision as of 20:33, 4 October 2021 by Welcome (talk | contribs)
Jump to navigation Jump to search

Basic operations with your inventory

  • List managed hosts:
ansible all --list-hosts
ansible YOUR_GROUP --list-hosts
ansible-inventory --graph
ansible-inventory --list
To filter just one group of host: ansible-inventory --list | jq '.["YOUR_GROUP_NAME"]'
  • List defined groups
ansible localhost -m debug -a 'var=groups.keys()'
ansible localhost -m debug -a 'var=groups'


Inventory of managed nodes

https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible Inventory is defined in /etc/ansible/hosts file. It allows you to define your managed hosts by hostname or IP address, and group them, such as "my_webservers_group" in our example in INI format.

Groups of groups, hierarchies, is also supported using (:children) keyword: [YOUR_NEW_GROUP_OF_GROUPS:children] [1]

#This is a example of a host configuration file. You can use # to include your comments on hosts file

foo_server.example.com
192.168.6.1
bar_server.example.com

[my_webservers_group]
foo5.example.com
bar6.example.com

[my_dbservers_group]
onedb1.example.com
twodb.example.com

#Example of a server alias on standard Ansible port
my_local_defined_hostname ansible_host=192.0.2.50

#Example of a server alias on a non standard Ansible port

my_jumper_server_alias ansible_host=192.0.2.50 ansible_port=5555 

You can also read Ansible best practices[2]

Activities

Help

ansible-inventory
usage: ansible-inventory [-h] [--version] [-v] [-i INVENTORY] [--vault-id VAULT_IDS]
                         [--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES] [--playbook-dir BASEDIR] [--list]
                         [--host HOST] [--graph] [-y] [--toml] [--vars] [--export] [--output OUTPUT_FILE]
                         [host|group]

positional arguments:
  host|group

optional arguments:
  --ask-vault-pass      ask for vault password
  --export              When doing an --list, represent in a way that is optimized for export,not as an accurate representation
                        of how Ansible has processed it
  --output OUTPUT_FILE  When doing --list, send the inventory to a file instead of to the screen
  --playbook-dir BASEDIR
                        Since this tool does not use playbooks, use this as a substitute playbook directory.This sets the
                        relative path for many features including roles/ group_vars/ etc.
  --toml                Use TOML format instead of default JSON, ignored for --graph
  --vars                Add vars to graph display, ignored unless used with --graph
  --vault-id VAULT_IDS  the vault identity to use
  --vault-password-file VAULT_PASSWORD_FILES
                        vault password file
  --version             show program's version number, config file location, configured module search path, module location,
                        executable location and exit
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY
                        specify inventory host path or comma separated host list. --inventory-file is deprecated
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)
  -y, --yaml            Use YAML format instead of default JSON, ignored for --graph

Actions:
  One of following must be used on invocation, ONLY ONE!

  --graph               create inventory graph, if supplying pattern it must be a valid group name
  --host HOST           Output specific host info, works as inventory script
  --list                Output all hosts info, works as inventory script

Show Ansible inventory information, by default it uses the inventory script JSON format
ERROR! No action selected, at least one of --host, --graph or --list needs to be specified.

See also

Advertising: