High-Level Architecture
Core Components
Ansible-cmdb consists of four main Python modules:ansible.py - Main Orchestrator
TheAnsible class is the central component responsible for gathering and managing all host information.
Key Responsibilities:
- Parsing fact directories containing JSON output from Ansible’s setup module
- Processing inventory files (static hosts files and dynamic inventory scripts)
- Parsing host_vars and group_vars directories
- Managing the complete host information dictionary
- Applying limits to filter hosts
Initialization
The
Ansible object is instantiated with paths to fact directories and optional inventory paths.Fact Parsing
For each fact directory,
_parse_fact_dir() is called to read JSON files containing host facts gathered by Ansible’s setup module.Inventory Scanning
If inventory paths are provided (
-i option), the system scans for:- Static hosts files (parsed by
HostsParser) - Dynamic inventory scripts (executable files)
- Directory structures containing multiple inventory files
Variable Collection
The system scans for and applies:
- Host-specific variables from
host_vars/directories - Group variables from
group_vars/directories
update_host(hostname, key_values, overwrite=True)- Deep-updates host informationget_hosts()- Returns the final list of hosts with limits appliedhosts_in_group(groupname)- Returns hosts belonging to a specific group
parser.py - Inventory Parser
The parser module contains classes for parsing different inventory formats.HostsParser
Parses Ansible hosts inventory files and generates a dictionary of hosts with their groups and variables. Parsing Process:-
Section Identification: Identifies sections in the inventory file:
[groupname]- Regular host groups[groupname:children]- Parent groups[groupname:vars]- Group variables
-
Host Expansion: Supports Ansible’s host pattern syntax:
-
Variable Application: Applies variables from:
- Inline host variables:
host1 ansible_port=2222 - Group variables from
:varssections - Parent group memberships from
:childrensections
- Inline host variables:
expand_hostdef(hostdef)- Expands host patterns into individual hostnames_parse_line_entry(line, type)- Parses individual inventory lines_apply_section(section, hosts)- Applies section data to hosts
DynInvParser
Parses JSON output from dynamic inventory scripts. Features:- Handles the
_metasection for host variables - Processes group definitions with hosts and vars
- Supports both dict and list formats for group definitions
render.py - Template Renderer
TheRender class is a wrapper that facilitates template rendering using Mako or Python-based templates.
Template Types:
-
Mako Templates (
.tplfiles):- Used for most output formats (HTML, CSV, Markdown, etc.)
- Supports template inheritance and includes
- Full access to host data and custom variables
-
Python Templates (
.pyfiles):- Executable Python scripts for complex rendering logic
- Used for formats like
html_fancy_splitandmarkdown_split - Must implement a
render(hosts, vars, tpl_dirs)function
Template Discovery
Searches for templates in specified directories:
- Direct file path if provided
{template_name}.tplin template directories{template_name}.pyin template directories
Template Loading
Loads the template with proper Mako configuration:
- UTF-8 encoding for input and output
- Error replacement for encoding issues
- Template lookup for includes
util.py - Utility Functions
Provides helper functions used throughout the codebase: Key Functions:-
deepupdate(target, src, overwrite=True)- Recursively merges dictionaries, lists, and sets. This is crucial for combining data from multiple sources (facts, inventory, host_vars, group_vars). -
is_executable(path)- Determines if a file is executable, used to identify dynamic inventory scripts. -
find_path(dirs, path_to_find)- Searches for a file across multiple directories. -
to_bool(s)- Converts string values to boolean.
Data Flow
Here’s how data flows through ansible-cmdb:Extension Points
Ansible-cmdb is designed to be extensible:Custom Templates
Create your own output formats by writing:- Mako templates for simple text-based formats
- Python templates for complex multi-file outputs
hosts- Complete host data dictionary- Custom parameters passed via
-p key=value
Custom Facts
Extend host information by:- Creating additional JSON files in a separate directory
- Passing the directory to ansible-cmdb alongside fact directories
- Data is automatically merged with existing host information
Dynamic Inventory
Integrate with external systems using dynamic inventory scripts that output JSON in Ansible’s format.File Organization
Dependencies
Ansible-cmdb relies on these key Python packages:- Mako - Template engine for generating output
- PyYAML - Parsing YAML files (host_vars, group_vars)
- ushlex - Unicode-aware shell lexer for Python 2
- jsonxs - JSON parsing with additional features