(Last Updated On: September 30, 2018)

Today let’s look at how you can generate host overview from ansible fact gathering output using Ansible-cmdb. This tool takes the output of Ansible’s fact-gathering as its input and converts it into a static HTML overview page with all gathered system configuration information.

Ansible-cmd supports multiple types of output (HTML, csv, sql, etc) and extending information gathered by Ansible with custom data. It shows the groups, host variables, custom variable, and machine-local facts for each host.

Features of Ansible-cmdb

Below are the features of Ansible-cmdb as outlined on the Project Github page.

  • Support for multiple output formats/templates:
    • Fancy HTML using the option (--template html_fancy)
    • Fancy HTML Split (--template html_fancy_split), with each host’s details in a separate file (for a large number of hosts).
    • CSV (--template csv), the trustworthy and flexible comma-separated format.
    • JSON (--template json), a dump of all facts in JSON format.
    • Markdown (--template markdown), useful for copy-pasting into Wiki’s and such.
    • Markdown Split (‘–template markdown_split’), with each host’s details in a separate file (for a large number of hosts).
    • SQL (--template sql), for importing host facts into a (My)SQL database.
    • Plain Text table (--template txt_table), for the console gurus.
    • and of course, any custom template you’re willing to make.
  • Host overview and detailed host information.
  • Host and group variables.
  • Gathered host facts and manual custom facts.
  • Adding and extending facts of existing hosts and manually adding entirely new hosts.

How to Install Ansible-cmdb

You can either install from Ansible-cmdb from source release package or using Python pip package manager

Method 1: Install Ansible-cmdb using pip

You need to have pip installed on your server before you can install Ansible-cmdb using this method.

Install pip on CentOS

Run the commands below on your terminal of choice to install pip

sudo yum -y install epel-release
sudo yum install python-pip
sudo pip install --upgrade pip

Install pip on Fedora

Add epel repository and install pip

sudo dnf install epel-release
sudo dnf install python-pip
sudo pip install --upgrade pip

Install pip on Ubuntu / Fedora

Use the command

sudo apt install python-pip
sudo pip install --upgrade pip

Install pip on Arch Linux / Manjaro

Use pacman package manager

sudo pacman -S python-pip
sudo pip install --upgrade pip

Once pip is installed, download and install ansible-cmdb

sudo pip install ansible-cmdb

You can also upgrade Ansible-cmdb through Pip:

sudo pip install --upgrade ansible-cmdb

For macOS users, you can use brew

brew install ansible-cmdb

Method 2: Install Ansible-cmdb from source

Clone source code from Github

git clone https://github.com/fboender/ansible-cmdb.git

Change to ansible-cmdb directory and run make install

cd ansible-cmdb
sudo make install

Sample output

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

How to use Ansible-cmdb

To view help usage for ansible-cmdb command, pass --help option

# ansible-cmdb --help
Usage: ansible-cmdb.py [option] <dir> > output.html

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -t TEMPLATE, --template=TEMPLATE
                        Template to use. Default is 'html_fancy'
  -i INVENTORY, --inventory=INVENTORY
                        Inventory to read extra info from
  -f, --fact-cache      <dir> contains fact-cache files
  -p PARAMS, --params=PARAMS
                        Params to send to template
  -d, --debug           Show debug output
  -q, --quiet           Don't report warnings
  -c COLUMNS, --columns=COLUMNS
                        Show only given columns
  --exclude-cols=EXCLUDE_COLUMNS
                        Exclude cols from output

Start by generating Ansible output for your hosts:

mkdir cmdb-output
ansible -m setup --tree cmdb-output/ all

You can as well specify a user to run as and password

ansible -m setup --user=root --tree cmdb-output/ all --ask-pass

Or specific host group

ansible -m setup --user=root --tree cmdb-output/ db-servers --ask-pass

Next, call ansible-cmdb on the resulting cmdb-output/ directory to generate the CMDB overview page:

ansible-cmdb cmdb-output/ > ansible-cmdb-overview.html

By default, the html_fancy template is used, which generates output containing an overview of all your hosts, with a section of detailed information for each host.

To explicitly specify the use of a fancy template for output generation, use

ansible-cmdb --template html_fancy cmdb-output/ > ansible-cmdb-overview.html

You can now open overview.html in your browser to view the results.

To use a custom inventory file, specify its path with -i

$ ansible-cmdb -i ./hosts cmdb-output/ > overview.html

If host_vars and group_vars directories exist at that location, they will be included automatically.

Available templates

Ansible-cmdb currently provides the following templates out of the box:

  • html_fancy: A dynamic, modern HTML page containing all hosts.
  • html_fancy_split: A dynamic, modern HTML page with each host’s details in a separate file.
  • txt_table: A quick text table summary of the available hosts with some minimal information.
  • json: Dumps all hosts including groups, variable, custom info in JSON format.
  • csv: The CSV template outputs a CSV file of your hosts.
  • markdown: The Markdown template generates host information in the Markdown format.
  • sql: The SQL template generates a .sql file that can be loaded into an SQLite or MySQL database.

Use the txt_table template and print columns

$ ansible-cmdb -t txt_table --columns name,os,ip,mem,cpus cmdb-output/
Name            OS               IP              Mem  CPUs  
--------------  ---------------  --------------  ---  ----  
10.200.3.205    CentOS 7.5.1804  10.200.3.205    2g   1     
10.200.3.204    CentOS 7.5.1804  10.200.3.204    2g   1     
10.200.3.206    CentOS 7.5.1804  10.200.3.206    2g   1     
10.200.200.250  CentOS 7.5.1804  10.200.200.250  4g   1     
10.200.200.251  CentOS 7.5.1804  10.200.200.250  2g   1

Get json output

ansible-cmdb -t json cmdb-output/ > cmdb.json

For more examples, visit Ansible-cmdb documentation page.