Ansible – Inventory

Ansible is an open source configuration management and orchestration utility. It can automate and standardize the configuration of remote hosts and virtual machines. Its orchestration functionality allows Ansible to coordinate the launch and graceful shutdown of multitiered applications. Because of this, Ansible can perform rolling updates of multiple systems in a way that results in zero downtime.
Instead of writing custom, individualized scripts, system administrators create high-level plays in Ansible. A play performs a series of tasks on the host, or group of hosts, specified in the play. A file that contains one or more plays is called a playbook. Ansible’s architecture is agentless. Work is pushed to remote hosts when Ansible executes. Modules are the programs that perform the actual work of the tasks of a play. Ansible is immediately useful because it comes with hundreds of core modules that perform useful system administrative work.

Inventories

  • An inventory is a list of hosts that Ansible manages
  • Inventory location may be specified as follows:
    • Default: /etc/ansible/hosts
    • Specified by CLI: ansible -i <filename>
    • Can be set in ansible.cfg
  • The inventory file may contain hosts, patterns, groups, and variables
  • You may specify the inventory as a directory containing a series of inventory files (both static and dynamic)
  • The inventory may be specified in YAML or INI format
  • Can be static or dynamic

Default inventory file

Default host inventory defines two host groups, webservers and
db-servers.

Consider such an example:

Host entries can also define how Ansible communicates with the managed host, including transport and user account information. In the previous example, SSH on web2.example.com is configured to listen on a non-standard port, port 1234. The account Ansible should use to log into that host is ftaylor.

 

Variables and Inventories

  • Ansible recommends that variables not be defined in inventory files:
    • Should be stored in YAML files located relative to inventory file
    • group_vars
    • host_vars
  • Files named by host or group and may end in yml or yaml

 

 

YAML vs INI

YAML

INI

 

Static & Dynamic Inventories

Static:

  • INI or YAML format
  • Maintained by hand
  • Easy to manage for static configuration

Dynamic:

  • Executable (bash script, python script, etc.)
  • Script returns JSON containing inventory information
  • Good for use with cloud resources subject to sudden change

On Dynamic Inventories

  • Specifying an executable file as the inventory is considered a dynamic inventory
  • JSON output is expected to be returned to  STDOUT from the executable
  • The implementation (python, java, C, bash, etc) does not matter as long as the program can run on the control host and behaves as Ansible expects
  • The program/script must respond to two possible parameters:
    • –list
    • –host [hostname]
  • The program script must return JSON in the format Ansible is expecting
  • Do not forget to make the file executable:
    • chmod +x dynamic.py
  • Using dynamic inventories, you can pull inventory information from the likes of:
    • A cloud provider
    • LDAP
    • Cobbler
    • Other CMDB software