Ansible : How to Configure the Ansible Inventory

0
0

The Ansible inventory is the heart of the product as it provides a lot of variables about your environment to the deployment mechanism. These variables are known as facts and serve Ansible to make decisions, template text-based files, and so on.

There are several ways of adding information about your environment to your inventory.

THE STATIC INVENTORY FILE

The static inventory is basically a mini-formatted file containing the definitions for hosts and groups. Here’s what you need to do:

  1. Create /etc/ansible/hosts with the following contents:
    ~]# cat << EOF >> /etc/ansible/hosts
    localhost         ansible_connection=local
    srv1.domain.tld   ansible_connection=ssh ansible_ssh_user=root
    
    [mail]
    mail[01..50].domain.tld
    
    [mail:vars]
    dns_servers=[ '8.8.8.8', '8.8.4.4' ]
    mail_port=25
    EOF
    ~]#
    

THE DYNAMIC INVENTORY FILE

The dynamic inventory file has to be an executable file, generating a JSON string containing information about your hosts and groups. Follow these steps:

  1. Create an ~/inventory.py script with the following contents:
    -]# cat << EOF >> ~/inventory.py
    #!/usr/bin/python -tt
    # -*- coding: utf-8 -*-
    # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
    import json
    
    def main():
        inventory = {
            '_meta': {
                'hostvars': {
                    'localhost': {
                        'ansible_connection': 'local' },
                    'srv1.domain.tld': {
                        'ansible_connection': 'ssh',
                        'ansible_ssh_user': 'root' },
                    }
                },
            'all': {
                'hosts': [
                    'localhost',
                    'srv1.domain.tld' ] },
            'mail': {
                'hosts': [],
                'vars': {
                    'dns_servers': [ '8.8.8.8', '8.8.4.4' ],
                    'mail_port': 25} }
        }
    
        for x in range(1,50):
            hostname = 'mail' + ('00%d' % x)[-2:] + '.domain.tld'
            inventory['_meta']['hostvars'].update({ hostname: {} })
            inventory['mail']['hosts'].append(hostname)
    
    
    
        print json.dumps(inventory, sort_keys=True, indent=4, separators=(',',': '))
    
    if __name__ == '__main__':
        main()
    ~]#
    
  2. Now, make the script executable, as follows:
    ~]# chmod +x ~/inventory.py
    

HOST_VARS FILES

host_vars file is a yml-formatted one containing extra facts, which will only be applied to the host with the same name as the file. Simply do the following:

  1. Create a host_vars file for srv1.domain.tld through this command:
    ~]# cat << EOF >> ~/host_vars/srv1.domain.tld.yml
    ansible_connection: ssh
    ansible_ssh_user: root
    EOF
    ~]#
    

GROUP_VARS FILES

Like host_varsgroup_vars files are yml-formatted ones containing extra facts. These will be applied to the group with the same name as the file. Perform the following:

  1. Create a group_vars file for mail via the following command:
    ~]# cat << EOF >> ~/group_vars/mail.yml
    dns_servers: [ '8.8.8.8', '8.8.4.4' ]
    mail_port: 25
    EOF
    ~]#
    

The inventory file location is set in the Ansible configuration file—look for the line starting with hostfile within the defaults section. This file is either a static file, or a script returning a JSON-formatted list of hosts and groups. Ansible automatically detects whether a file is a script and treats it this way to import information.

There is one caveat, however: the script needs to show the JSON-formatted information by specifying --list.

Ansible can automatically combine the inventory with the host_vars and group_vars files if the latter two directories are in the same directory as the inventory file / script. Take a look at the following:

/etc/ansible/hosts
/etc/ansible/host_vars
/etc/ansible/host_vars/srv1.domain.tld.yml
/etc/ansible/host_vars/...
/etc/ansible/group_vars
/etc/ansible/group_vars/mail.yml
/etc/ansible/group_vars/...

The same can be achieved by putting the host_vars and group_vars directories in the same directory as the playbook you are executing.

TIP

The facts in host_vars and group_vars take priority over the variables returned through the inventory.

Ansible already seeds the inventory with the facts that it retrieves from the host itself. You can easily find out which facts Ansible prepares for your use by executing the following command:

~]# ansible -m setup <hostname>

This will produce a lengthy JSON-formatted output with all the facts Ansible knows about your destination host.

If you want even more information, on RHEL systems, you can install redhat-lsb-core to have access to LSB-specific facts.

Enterprises tend to have databases containing information regarding all their systems for change management. This is an excellent source for the inventory script to get its information.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here