Ansible dynamic inventory

The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. ... The inventory file can list individual hosts or user-defined groups of hosts.

DYNAMIC INVENTORY

If we have the setup, where we add and remove the hosts very frequently, then keeping your inventory always up-to-date become a little bit problematic. In such a case Dynamic inventory comes into the picture, generally are scripts (Python/Shell) for dynamic environments.

Cloud Environments

With Ansible, as aforementioned, can use “-i” to specify the custom inventory file.For example, if you use AWS cloud and you manage EC2 inventory using its Query API, or through command-line tools such as awscli, then you can make use of dynamic inventory.

DYNAMIC INVENTORIES EXAMPLE

Host inventory in Ansible can be dynamically generated. Sources for dynamic inventory information include public/private cloud providers, cobbler system information, LDAP database, or CMDB (Configuration Management database). Ansible includes scripts that handle dynamic host, group, and variable information from the most common providers such as Amazon EC2, Cobbler, Rackspace, and OpenStack.

We can write our own customize dynamic inventory program in any programming language and must return in JSON format when passed appropriate options. In order for Ansible to use script to retrieve hosts information from external inventory system, this script has to support the –list parameter,

To create dynamic direcory ,

#mkdir dynamic-inventories

#cd dynamic-inventories/

Let's create an example dynamic file using python,

#cat dynamic.py

#!/usr/bin/env python

'''

Example custom dynamic inventory script for Ansible, in Python.

call it with --list to show list.

call it with --host [hostname] for specific hosts 

'''

import os

import sys

import argparse

try:

 import json

except ImportError:

 import simplejson as json

class ExampleInventory(object):

 def __init__(self):

 self.inventory = {}

 self.read_cli_args()

 # Called with `--list`.

 if self.args.list:

 self.inventory = self.example_inventory()

 # Called with `--host [hostname]`.

 elif self.args.host:

 # Not implemented, since we return _meta info `--list`.

 self.inventory = self.empty_inventory()

 # If no groups or vars are present, return empty inventory.

 else:

 self.inventory = self.empty_inventory()

 print json.dumps(self.inventory);

 # Example inventory for testing.

 def example_inventory(self):

 return {

 'group': {

 'hosts': ['ansible2.zippyops.com', 'ansible3.zippyops.com'],

 'vars': {

 'ansible_ssh_user': 'ansible',

 'test_variable': 'nonspecific_value'

 }

 },

 '_meta': {

 'hostvars': {

 'ansible2.zippyops.com': {

 'log_folder': '/var/log'

 },

 'ansible3.zippyops.com': {

 'log_folder': '/var/log2'

 }

 }

 }

 }

 # Empty inventory for testing.

 def empty_inventory(self):

 return {'_meta': {'hostvars': {}}}

 # Read the command line args passed to the script.

 def read_cli_args(self):

 parser = argparse.ArgumentParser()

 parser.add_argument('--list', action = 'store_true')

 parser.add_argument('--host', action = 'store')

 self.args = parser.parse_args()

# Get the inventory.

ExampleInventory()


To change file permission 

#chmod +x dynamic.py


Now we can run,

#ansible all -i dynamic.py -m ping

ansible2.zippyops.com | SUCCESS => {

 "changed": false, 

 "ping": "pong"

}

ansible3.zippyops.com | SUCCESS => {

 "changed": false, 

 "ping": "pong"

}

To run list host in that,

# ./dynamic.py --list

{"group": {"hosts": ["ansible2.zippyops.com", "ansible3.zippyops.com"], "vars": 

{"ansible_ssh_user": "ansible", "test_variable": "nonspecific_value"}}, "_meta": {"hostvars": 

{"ansible2.zippyops.com": {"log_folder": "/var/log"}, "ansible3.zippyops.com": {"log_folder": 

"/var/log2"}}}}

Dynamic host file keeps changing as we add new hosts or decommission old ones. The IP addresses of hosts are also dynamic as we stop and start new host systems.



Relevant Blogs:

custom log in ansible  

Ansible Playbook 

Ansible Vault 

OWASP Installation and Configuration

Recent Comments

No comments

Leave a Comment