- PostgreSQL schema: 14 tables, JSONB attributes, audit triggers, soft delete - FastAPI backend: CRUD, search/filter, relationship graph, bulk import, JWT RBAC - React frontend: CI table, detail card, force-graph, dashboard - Seed data: homelab scenario (Proxmox, Mikrotik, VMs, services) - Docker Compose + Kubernetes manifests - 20 backend tests (pytest + httpx)
2.9 KiB
2.9 KiB
Ansible + CMDB Integration Guide
Overview
Ansible can auto-discover infrastructure and register it as CIs in the CMDB.
Setup
- Install
ansibleandrequestson your control node - Get a CMDB API token:
curl -X POST .../api/auth/login -d '{"username":"admin","password":"..."}' - Set
CMDB_TOKENandCMDB_URLin your inventory
Playbook: Import Hosts
# playbooks/cmdb-sync.yml
---
- name: Sync Ansible inventory to CMDB
hosts: all
gather_facts: true
vars:
cmdb_url: "http://cmdb-host:8000/api"
cmdb_token: "{{ lookup('env', 'CMDB_TOKEN') }}"
tasks:
- name: Register host as CI
uri:
url: "{{ cmdb_url }}/ci"
method: POST
headers:
Authorization: "Bearer {{ cmdb_token }}"
Content-Type: "application/json"
body_format: json
body:
name: "{{ inventory_hostname }}"
ci_type_name: "{% if 'proxmox' in inventory_hostname %}PhysicalServer{% elif 'vm-' in inventory_hostname %}VirtualMachine{% else %}Application{% endif %}"
status: active
tags: "{{ group_names }}"
attributes:
os: "{{ ansible_distribution }} {{ ansible_distribution_version }}"
cpu_cores: "{{ ansible_processor_vcpus }}"
ram_mb: "{{ ansible_memtotal_mb }}"
ip: "{{ ansible_default_ipv4.address | default('N/A') }}"
kernel: "{{ ansible_kernel }}"
hostname: "{{ ansible_hostname }}"
status_code: [201, 409]
register: cmdb_result
- name: Show result
debug:
msg: "{{ inventory_hostname }} → {{ cmdb_result.status }}"
Playbook: Update Software Versions
- name: Update software instances in CMDB
hosts: all
tasks:
- name: Get installed packages
package_facts:
become: true
- name: Report key packages to CMDB
uri:
url: "{{ cmdb_url }}/ci/bulk/import"
method: POST
headers:
Authorization: "Bearer {{ cmdb_token }}"
body_format: json
body:
items: >-
{{
package_facts.packages.keys() | select('search', 'nginx|docker|postgresql|redis|prometheus') |
map(attribute='-', {
'name': item,
'ci_type_name': 'Application',
'attributes': {'version': package_facts.packages[item].version}
}) | list
}}
status_code: [201]
when: "'nginx' in package_facts.packages or 'docker-ce' in package_facts.packages"
Best Practices
- Idempotent registration — use 409 handling to update existing CIs
- Tag with inventory groups — auto-tag CIs with Ansible group names
- Schedule via cron —
0 */6 * * * ansible-playbook cmdb-sync.yml - Use lookup plugins —
uriplugin for API calls - Store token securely — use Ansible Vault for
CMDB_TOKEN