ansible-cmdb tool can be used to create system configuration report of your host inventory in different formats like html,csv, json,sql etc.  Let’s go through the below demonstration . Lets say you have hundreds of servers and you can want to generate a report that has server overview details like cpu,mem,disk usage,server type,cpu type etc of all your servers. In that case ansible-cmdb tool can be used to achieve that target.

Install ansible-cmdb tool:

Download link – > https://github.com/fboender/ansible-cmdb/releases

 

 


[root@DBHOST ansible-cmdb_rpm]# tar -vxzf ansible-cmdb-1.28.tar.gz
ansible-cmdb-1.28/
ansible-cmdb-1.28/.gitignore
ansible-cmdb-1.28/LICENSE
ansible-cmdb-1.28/Makefile
ansible-cmdb-1.28/README.md
ansible-cmdb-1.28/contrib/
ansible-cmdb-1.28/contrib/ansible-cmdb.man.1
ansible-cmdb-1.28/contrib/debian/
ansible-cmdb-1.28/contrib/debian/DEBIAN/
ansible-cmdb-1.28/contrib/debian/DEBIAN/control
ansible-cmdb-1.28/contrib/debian/changelog
ansible-cmdb-1.28/contrib/debian/copyright
ansible-cmdb-1.28/contrib/release_Makefile
ansible-cmdb-1.28/contrib/screenshot-detail.png
ansible-cmdb-1.28/contrib/screenshot-overview.png
ansible-cmdb-1.28/docs/
ansible-cmdb-1.28/docs/dev.md
ansible-cmdb-1.28/docs/faq.md
ansible-cmdb-1.28/docs/index.md
ansible-cmdb-1.28/docs/installation.md
ansible-cmdb-1.28/docs/usage.md
ansible-cmdb-1.28/example/
ansible-cmdb-1.28/example/ansible.cfg
ansible-cmdb-1.28/example/dyninv_test.py
ansible-cmdb-1.28/example/dyninv_vbox.py
.
.
.
.
ansible-cmdb-1.28/test/f_inventory/mixeddir/dyninv.py
ansible-cmdb-1.28/test/f_inventory/mixeddir/hosts
ansible-cmdb-1.28/test/f_inventory/out/
ansible-cmdb-1.28/test/f_inventory/out/db.dev.local
ansible-cmdb-1.28/test/test.py
ansible-cmdb-1.28/test/test.sh


[root@DBHOST ansible-cmdb_rpm]# cd ansible-cmdb-1.28/


[root@DBHOST ansible-cmdb-1.28]# make install
umask 0022 && mkdir -p /usr/local/lib/ansible-cmdb
umask 0022 && mkdir -p /usr/local/man/man1
umask 0022 && cp -a src/* /usr/local/lib/ansible-cmdb
umask 0022 && cp -r lib/* /usr/local/lib/ansible-cmdb
umask 0022 && cp LICENSE /usr/local/lib/ansible-cmdb
umask 0022 && cp README.md /usr/local/lib/ansible-cmdb
umask 0022 && gzip -9 -c contrib/ansible-cmdb.man.1 > /usr/local/man/man1/ansible-cmdb.man.1.gz
umask 0022 && ln -s /usr/local/lib/ansible-cmdb/ansible-cmdb /usr/local/bin/ansible-cmdb

[root@DBHOST ansible-cmdb-1.28]# which ansible-cmdb
/usr/local/bin/ansible-cmdb


Create inventory file


cat /home/ansible/ansible-scipts/dbaclass_inventory

linux_host1 ansible_host=182.34.86.60 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host2 ansible_host=182.34.86.61 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host3 ansible_host=182.34.86.62 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host4 ansible_host=182.34.86.63 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host5 ansible_host=182.34.86.64 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host6 ansible_host=182.34.86.65 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host7 ansible_host=182.34.86.40 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host8 ansible_host=182.34.86.41 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host9 ansible_host=182.34.86.42 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host10 ansible_host=182.34.86.43 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host11 ansible_host=182.34.86.44 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host12 ansible_host=182.34.86.45 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host13 ansible_host=182.34.86.46 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host14 ansible_host=182.34.86.47 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host15 ansible_host=182.34.86.48 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123
linux_host16 ansible_host=182.35.36.114 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbaclass@123

Generate report:

Before generating report, first step is to collect the facts gathering details of all hosts.

mkdir out 

ansible -i /home/ansible/ansible-scipts/dbaclass_inventory  -m setup --tree out/ all

this command will create unique file for each host in out directory .

ansible-cmdb provides different templates to generate report. Most popular and default one is the html template.

  1. Generate html report:


root# ansible-cmdb -i /home/ansible/ansible-scipts/dbaclass_inventory -t html_fancy out/ > dbaclass_inventory.html

 

Now view the dbaclass_inventory.html report in your browser. You can select different columns and view the updated reports.

 

 

 

 

 

 

 

       3. Generate csv report:

You can generate the inventory overview report in csv format also.

 [ansible@DBHOST ansible-scipts]$ ansible-cmdb  -i /home/ansible/ansible-scipts/dbaclass_inventory   -t csv out > hostinv.csv

 

 

       3. Generate text output on command prompt:


[ansible@DBHOST ansible-scipts]$  ansible-cmdb  -i /home/ansible-scipts/dbaclass_inventory  -t txt_table --columns ip,os,mem,cpus,memfree,memused out/

OS            IP              Mem   MemFree  MemUsed  CPUs
------------  --------------  ----  -------  -------  ----
RedHat 6.10   182.34.86.41    32g   29g      3g       1
RedHat 6.10   182.34.86.42    64g   61g      3g       2
RedHat 7.9    182.34.86.65    64g   60g      4g       2
RedHat 7.7    182.34.86.40    161g  45g      116g     4
RedHat 7.9    182.34.86.63    64g   18g      46g      2
RedHat 7.9    182.34.86.64    64g   59g      6g       2
RedHat 6.10   182.34.86.61    32g   29g      3g       1
RedHat 7.9    182.34.86.62    64g   30g      34g      2
RedHat 6.10   182.34.86.60    161g  27g      135g     4
RedHat 6.10   182.35.62.68    32g   26g      6g       2
RedHat 6.10   182.35.62.67    32g   23g      9g       2

      4. Generate a sql file:

You can generate a sql file, which can be run on any sql database to create a host table and load the inventory data.

[ansible@DBHOST ansible-scipts]$  ansible-cmdb  -i /home/ansible-scipts/dbaclass_inventory  -t sql out > hostinv.sql

cat hostinv.sql

DROP TABLE IF EXISTS hosts;
CREATE TABLE hosts (
    name VARCHAR(255),
    fqdn VARCHAR(255),
    main_ip VARCHAR(15),
    os_name VARCHAR(80),
    os_version VARCHAR(40),
    system VARCHAR(40),
    kernel VARCHAR(40),
    arch_hardware VARCHAR(12),
    arch_userspace VARCHAR(12),
    virt_type VARCHAR(20),
    virt_role VARCHAR(20),
    cpu_type VARCHAR(60),
    vcpus INT,
    ram FLOAT,
    disk_total FLOAT,
    disk_free FLOAT
);

    INSERT INTO hosts (
        name,
        fqdn,
        main_ip,
        os_name,
        os_version,
        system,
        kernel,
        arch_hardware,
        arch_userspace,
        virt_type,
        virt_role,
        cpu_type,
        vcpus,
        ram,
        disk_total,
        disk_free
    ) VALUES (
        "linux_host8",
.
.
.
.
.

 

NOTE – > For more information regarding ansible-cmd visit https://ansible-cmdb.readthedocs.io/en/latest/usage/