Overview
The -l or --limit option allows you to filter which hosts appear in the generated CMDB output. This works similarly to Ansible’s built-in limit functionality.
Host limiting requires the -i option to point to your inventory file. Without an inventory, ansible-cmdb cannot determine group memberships.
Basic Usage
ansible-cmdb -i hosts -l 'pattern' out/ > cmdb.html
Pattern Syntax
Ansible-cmdb supports the following pattern types:
Include all hosts in the inventory. ansible-cmdb -i hosts -l 'all' out/ > cmdb.html
Include a specific host by its exact name. ansible-cmdb -i hosts -l 'web1.example.com' out/ > cmdb.html
Include all hosts in a specific group. ansible-cmdb -i hosts -l 'webservers' out/ > cmdb.html
Exclude hosts matching the pattern (inversion). ansible-cmdb -i hosts -l '!development' out/ > cmdb.html
Combine multiple patterns with colon (:) separator. ansible-cmdb -i hosts -l 'webservers:databases' out/ > cmdb.html
Examples
Single Group
Include only hosts in the webservers group:
ansible-cmdb -i hosts -l 'webservers' out/ > webservers_cmdb.html
Specific Host
Generate CMDB for a single host:
ansible-cmdb -i hosts -l 'db1.example.com' out/ > db1_cmdb.html
Multiple Groups
Include hosts from multiple groups:
ansible-cmdb -i hosts -l 'webservers:databases' out/ > web_and_db_cmdb.html
Exclude Group
Include all hosts except those in a specific group:
ansible-cmdb -i hosts -l 'all:!development' out/ > production_cmdb.html
Complex Pattern
Combine inclusion and exclusion:
# Include all hosts in cust.acme, exclude the group itself, but add back foo.example.com
ansible-cmdb -i hosts -l 'all:!cust.acme:foo.example.com' out/ > cmdb.html
Real-World Use Cases
Environment Separation
Customer Segregation
Function-Based
Compliance Reporting
Generate separate CMDBs for each environment: # Development CMDB
ansible-cmdb -i hosts -l 'development' out/ > dev_cmdb.html
# Production CMDB
ansible-cmdb -i hosts -l 'production' out/ > prod_cmdb.html
# Non-production (test + dev)
ansible-cmdb -i hosts -l 'development:testing' out/ > nonprod_cmdb.html
Generate CMDBs per customer: # Single customer
ansible-cmdb -i hosts -l 'cust.acme' out/ > acme_cmdb.html
# All customers except one
ansible-cmdb -i hosts -l 'all:!cust.internal' out/ > external_cmdb.html
Generate CMDBs by infrastructure function: # Web tier only
ansible-cmdb -i hosts -l 'webservers' out/ > web_tier.html
# Database tier only
ansible-cmdb -i hosts -l 'databases' out/ > db_tier.html
# Application stack (web + app + db)
ansible-cmdb -i hosts -l 'webservers:appservers:databases' out/ > app_stack.html
Generate compliance-specific reports: # PCI-compliant hosts only
ansible-cmdb -i hosts -l 'pci_scope' out/ > pci_cmdb.html
# Everything except PCI
ansible-cmdb -i hosts -l 'all:!pci_scope' out/ > non_pci_cmdb.html
Sample Inventory
To illustrate limiting, here’s a sample inventory structure:
[development]
dev1.example.com
dev2.example.com
[testing]
test1.example.com
test2.example.com
[production]
web1.example.com
web2.example.com
db1.example.com
[webservers]
web1.example.com
web2.example.com
dev1.example.com
[databases]
db1.example.com
[cust.acme]
web1.example.com
db1.example.com
[cust.internal]
dev1.example.com
dev2.example.com
test1.example.com
test2.example.com
Example Limit Patterns
Using the inventory above:
Production only
Production webservers
Everything except development
Specific customer with exception
ansible-cmdb -i hosts -l 'production' out/ > cmdb.html
# Includes: web1, web2, db1
Limitations
Current limitations of the --limit option:
No wildcards : Patterns like db*.example.com are not supported
No host expansions : Patterns like db[0-3].example.com are not supported
Requires inventory : The -i option must be specified for limiting to work
Unsupported Patterns
# These do NOT work:
ansible-cmdb -i hosts -l 'web*.example.com' out/ > cmdb.html # ✗ wildcards
ansible-cmdb -i hosts -l 'web[1-3].example.com' out/ > cmdb.html # ✗ ranges
# Use exact names or groups instead:
ansible-cmdb -i hosts -l 'web1.example.com:web2.example.com:web3.example.com' out/ > cmdb.html # ✓
ansible-cmdb -i hosts -l 'webservers' out/ > cmdb.html # ✓
Combining with Templates
Limiting works with all templates:
html_fancy
txt_table
json
csv
ansible-cmdb -i hosts -l 'production' -t html_fancy out/ > prod.html
ansible-cmdb -i hosts -l 'webservers' -t txt_table out/
ansible-cmdb -i hosts -l 'databases' -t json out/ > db.json
ansible-cmdb -i hosts -l 'production' -t csv out/ > prod.csv
Automation Examples
Generate Multiple CMDBs
Create separate CMDB files for different groups:
#!/bin/bash
# Output directory
OUTPUT_DIR = "/var/www/html/cmdb"
# Generate CMDB for each environment
for env in development testing production ; do
ansible-cmdb -i /etc/ansible/hosts -l " $env " /var/cache/ansible/facts > " $OUTPUT_DIR /${ env }_cmdb.html"
done
# Generate CMDB for each customer
for customer in acme globex initech ; do
ansible-cmdb -i /etc/ansible/hosts -l "cust.${ customer }" /var/cache/ansible/facts > " $OUTPUT_DIR /${ customer }_cmdb.html"
done
Scheduled Reports
Use cron to generate regular reports:
# Generate production CMDB daily at 2 AM
0 2 * * * /usr/local/bin/ansible-cmdb -i /etc/ansible/hosts -l 'production' /var/cache/ansible/facts > /var/www/html/prod_cmdb.html
# Generate development CMDB every 6 hours
0 */6 * * * /usr/local/bin/ansible-cmdb -i /etc/ansible/hosts -l 'development' /var/cache/ansible/facts > /var/www/html/dev_cmdb.html
Best Practices
Organize your inventory - Use meaningful group names that map to your limiting needs
Use hierarchical groups - Create parent groups for easier limiting:
[production:children]
prod_web
prod_db
prod_app
Document patterns - Keep a list of common limit patterns for your team
Test patterns - Verify your pattern matches expected hosts using Ansible first:
ansible -i hosts --list-hosts 'production:!databases'
Combine with parameters - Use limiting with template parameters for customized output:
ansible-cmdb -i hosts -l 'production' -p skip_empty= 1 out/ > prod.html
Troubleshooting
No hosts in output
If the generated CMDB is empty:
Verify the inventory contains matching hosts:
ansible -i hosts --list-hosts 'your-pattern'
Check that fact files exist for those hosts:
Ensure host names match exactly between inventory and fact files
Unexpected hosts included
If wrong hosts appear:
Check group memberships in your inventory
Remember that patterns use OR logic (: means union)
Use ! to explicitly exclude unwanted groups
Limiting without inventory
If you see an error about limiting requiring inventory:
# This won't work:
ansible-cmdb -l 'production' out/ > cmdb.html
# You must specify inventory:
ansible-cmdb -i hosts -l 'production' out/ > cmdb.html