Files
----/docs/ansible-integration.md
smolkik-code 9b15f8b09c feat: CMDB full-stack app - FastAPI + PostgreSQL + React
- 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)
2026-06-25 13:01:40 +07:00

2.9 KiB

Ansible + CMDB Integration Guide

Overview

Ansible can auto-discover infrastructure and register it as CIs in the CMDB.

Setup

  1. Install ansible and requests on your control node
  2. Get a CMDB API token: curl -X POST .../api/auth/login -d '{"username":"admin","password":"..."}'
  3. Set CMDB_TOKEN and CMDB_URL in 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

  1. Idempotent registration — use 409 handling to update existing CIs
  2. Tag with inventory groups — auto-tag CIs with Ansible group names
  3. Schedule via cron0 */6 * * * ansible-playbook cmdb-sync.yml
  4. Use lookup pluginsuri plugin for API calls
  5. Store token securely — use Ansible Vault for CMDB_TOKEN