Configuring the Ansible inventory

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.

How to do it…

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

A 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_vars, group_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
    ~]#
    

How it works…

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, as shown in the preceding recipe. 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.

There's more…

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.

See also

If you want more detailed information about the Ansible inventory, go to http://docs.ansible.com/ansible/intro_inventory.html.

Shameless self-promotion for a personal project and a tool to automate the inventory calls for a mention of https://github.com/bushvin/inventoryd/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset