
Tackling the complexities of enterprise inventories
One common challenge our customers face is the need to track hosts from multiple sources: LDAP, cloud providers, and enterprise CMDB systems. Using a dynamic inventory allows users to integrate with these systems and update the Ansible inventory as it varies over time, with hosts spinning up and shutting down in response to business demands.
Ansible supports two ways to connect with external inventory: Inventory plugins and inventory scripts.
Today we are going to cover dynamic inventory plugins as a Collection for network device management through an /etc/hosts
file. This same type of setup can be used for creating any dynamic inventory using different items from /etc/hosts
files to ini
files or even csv
’s.
The first mission: Where is the source of truth?
We are going to start by figuring out the source of truth of the inventory we want to import.
If you want to test and use this inventory plugin you can find the code in this Github repository:
https://github.com/jmcleroy/inventoryplugin.git
In this case, it will be an /etc/hosts
file externally stored in the Github/Gitlab inventory plugin repo as a test, in a similar fashion this file can also be pulled from a server with the correct host file with all of your network devices. In this Git repo we are going to make the necessary changes to permit a multi-vendor setup and we will use keywords from the naming to trigger grouping capabilities.
The hosts file will look like this:
% cat hosts
10.25.25.25 ansible.cisco.external
10.25.26.26 ansible2.juniper.external
10.26.35.52 ansible3.arista.external
From there we will have the hierarchy for the Collections plugin. This is new to Ansible starting in 2.11+ and Red Hat Ansible Automation Platform 2.1.
The hierarchical structure looks like this:
inventoryplugin % tree
.
├── README.md
├── collections
│ └── ansible_collections
│ └── ansible
│ └── network
│ └── plugins
│ └── inventory
│ └── etc_hosts.py
├── etc_hosts.yml
└── hosts
6 directories, 4 files
The second mission: Using the plugin
After we define our source of truth, we have to add the playbook to call this new plugin. There are some items you need to set, however within the ansible.cfg
, you will need to set the Collections path see following code:
#
#inject_facts_as_vars = True
# Paths to search for collections, colon separated
collections_paths = ~/.ansible/collections:/usr/share/ansible/collections:/Users/jmcleroy/PycharmProjects/inventoryplugin/collections
# Paths to search for roles, colon separated
#roles_path = ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
# Host key checking is enabled by default
host_key_checking = False
Also you will need to set the inventory path see below:
# set plugin path directories here, separate with colons
#action_plugins = /usr/share/ansible/plugins/action
#become_plugins = /usr/share/ansible/plugins/become
#cache_plugins = /usr/share/ansible/plugins/cache
#callback_plugins = /usr/share/ansible/plugins/callback
#connection_plugins = /usr/share/ansible/plugins/connection
#lookup_plugins = /usr/share/ansible/plugins/lookup
inventory_plugins = /usr/share/ansible/plugins/inventory:/Users/jmcleroy/PycharmProjects/inventoryplugin/collections/ansible_collections/ansible/network/plugins/inventory
#vars_plugins = /usr/share/ansible/plugins/vars
#filter_plugins = /usr/share/ansible/plugins/filter
#test_plugins = /usr/share/ansible/plugins/test
#terminal_plugins = /usr/share/ansible/plugins/terminal
#strategy_plugins = /usr/share/ansible/plugins/strategy
Here is a snippet of the Python plugin:
DOCUMENTATION = '''
name: Etc_Hosts Inventory
plugin_type: inventory
author:
- Eric McLeroy (@jmcleroy)
short_description: Dynamic inventory plugin for a etc/hosts network file.
version_added: "n/a"
extends_documentation_fragment:
- constructed
options:
plugin:
description: Token that ensures this is a source file for the plugin.
required: True
choices: ['ansible.network.etc_hosts']
file_path:
description:
- The path to the etc/hosts file.
- This can be either an absolute path, or relative to inventory file.
required: True
requirements:
- python >= 2.7
'''
EXAMPLES = r'''
# example etc_hosts.yml file
---
plugin: ansible.network.etc_hosts
file_path: /etc/hosts
'''
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.inventory import BaseFileInventoryPlugin, Constructable
from string import digits
import os
import re
class InventoryModule(BaseFileInventoryPlugin, Constructable):
NAME = 'ansible.network.etc_hosts'
def verify_file(self, path):
super(InventoryModule, self).verify_file(path)
return path.endswith(('etc_hosts.yml', 'etc_hosts.yaml'))
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path)
self._read_config_data(path)
hosts_file_in = self.get_option('file_path')
if os.path.isabs(hosts_file_in):
hosts_file = hosts_file_in
else:
hosts_file = os.path.join(os.path.dirname(path), hosts_file_in)
file=open(hosts_file, 'r')
lines=file.readlines()
for line in lines:
group_name = line.split(' ')[1]
group_name = re.split("\.|-", group_name)[1].rstrip('0123456789')
self.inventory.add_group(group_name)
host_name = self.inventory.add_host(line.split(' ')[1].strip(), group_name)
self.inventory.set_variable(host_name, 'ansible_host' , line.split()[0])
Next we will use the plugin to show the output as created by the plugin that took the ini
file from above and dynamically created groups from a variable.
[emcleroy@rhel3 etcinventory]$ ansible-inventory --inventory etc_hosts.yml --list
{
"_meta": {
"hostvars": {
"ansible.cisco.external": {
"ansible_host": "10.25.25.25"
},
"ansible2.juniper.external": {
"ansible_host": "10.25.26.26"
},
"ansible3.arista.external": {
"ansible_host": "10.26.35.52"
}
}
},
"all": {
"children": [
"arista",
"cisco",
"juniper",
"ungrouped"
]
},
"arista": {
"hosts": [
"ansible3.arista.external"
]
},
"cisco": {
"hosts": [
"ansible.cisco.external"
]
},
"juniper": {
"hosts": [
"ansible2.juniper.external"
]
}
}
What can I do next?
You can view a step by step example on how to use the plugin in the following video demonstration:
Whether you are beginning your automation journey or are a seasoned veteran, there are a variety of resources to enhance your automation knowledge:
- AnsibleFest 2022 - Come hang out with us, to learn and share automation experiences with Ansible enthusiasts in Chicago October 18-19, 2022.
- Subscribe to the Red Hat Ansible Automation Platform YouTube channel.Be sure to check out our web series, Automated Live hosted by Colin McNaughton.
- Follow Red Hat AnsibleAutomation Platform on Twitter - Do you have questions or an automation project you want to show off? Tweet at us!
À propos de l'auteur
Parcourir par canal
Automatisation
Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements
Intelligence artificielle
Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement
Cloud hybride ouvert
Découvrez comment créer un avenir flexible grâce au cloud hybride
Sécurité
Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies
Edge computing
Actualité sur les plateformes qui simplifient les opérations en périphérie
Infrastructure
Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde
Applications
À l’intérieur de nos solutions aux défis d’application les plus difficiles
Programmes originaux
Histoires passionnantes de créateurs et de leaders de technologies d'entreprise
Produits
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Services cloud
- Voir tous les produits
Outils
- Formation et certification
- Mon compte
- Assistance client
- Ressources développeurs
- Rechercher un partenaire
- Red Hat Ecosystem Catalog
- Calculateur de valeur Red Hat
- Documentation
Essayer, acheter et vendre
Communication
- Contacter le service commercial
- Contactez notre service clientèle
- Contacter le service de formation
- Réseaux sociaux
À propos de Red Hat
Premier éditeur mondial de solutions Open Source pour les entreprises, nous fournissons des technologies Linux, cloud, de conteneurs et Kubernetes. Nous proposons des solutions stables qui aident les entreprises à jongler avec les divers environnements et plateformes, du cœur du datacenter à la périphérie du réseau.
Sélectionner une langue
Red Hat legal and privacy links
- À propos de Red Hat
- Carrières
- Événements
- Bureaux
- Contacter Red Hat
- Lire le blog Red Hat
- Diversité, équité et inclusion
- Cool Stuff Store
- Red Hat Summit