How to create a host’s Inventory using Ansible


In this guide, we can see how to create a host inventory using “ansible-cmd” command. If we required collecting all host information with the configuration in a single shot it’s possible using “ansible-cmdb“.

To know how to install and configure “Ansible” you can refer to the below links.

  1. Install and configure Ansible Automation IT Tool
  2. Install Ansible using Python installation manager pip
  3. How to create a host’s Inventory using Ansible
  4. Managing Groups and User creation using Ansible
  5. Creating Logical volume based file system using Ansible

Install with “ansible-cmdb” using pip.

# pip install ansible-cmdb

Sample output from above command.

[root@ansible ~]# pip install ansible-cmdb
Collecting ansible-cmdb Downloading ansible-cmdb-1.26.1.tar.gz (181kB) 100% |################################| 184kB 2.8MB/s Collecting mako (from ansible-cmdb) Downloading Mako-1.0.7.tar.gz (564kB) 100% |################################| 573kB 1.3MB/s Requirement already satisfied: pyyaml in /usr/lib64/python2.7/site-packages (from ansible-cmdb) Collecting ushlex (from ansible-cmdb) Downloading ushlex-0.99.tar.gz Collecting jsonxs (from ansible-cmdb) Downloading jsonxs-0.6.tar.gz Requirement already satisfied: MarkupSafe>=0.9.2 in /usr/lib/python2.7/site-packages (from mako->ansible-cmdb) Installing collected packages: mako, ushlex, jsonxs, ansible-cmdb Running setup.py install for mako ... done Running setup.py install for ushlex ... done Running setup.py install for jsonxs ... done Running setup.py install for ansible-cmdb ... done Successfully installed ansible-cmdb-1.26.1 jsonxs-0.6 mako-1.0.7 ushlex-0.99
[root@ansible ~]

Collect the host facts using ansible. This will collect the facts of each host end to end and it will save under /home/ansible/facts/.

$ ansible -i hosts -m setup --tree /home/ansible/facts/ oel7_prod

Below output truncated due to long output.

[ansible@ansible ~]$ ansible -i hosts -m setup --tree /home/ansible/facts/ oel7_prod
ansiclient1.oel7.local | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.107.226"
], 
"ansible_all_ipv6_addresses": [
"fe80::2bc3:7839:705a:9d93"
], 
"ansible_apparmor": {
"status": "disabled"
}, 
"ansible_architecture": "x86_64", 
"ansible_bios_date": "05/19/2017", 
"ansible_bios_version": "6.00", 
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-514.el7.x86_64", 
"LANG": "en_US.UTF-8", 
"crashkernel": "auto", 
"quiet": true, 
"rd.lvm.lv": "rhel/swap", 
"rhgb": true, 
"ro": true, 
"root": "/dev/mapper/rhel-root"
}, 
"ansible_date_time": {
"date": "2018-03-07", 
"day": "07", 
"epoch": "1520369349",

We got the facts from remote servers.

[ansible@ansible ~]$ ls -lthr facts/ 
-rw-rw-r--. 1 ansible ansible 15K Mar 6 22:22 ansiclient3.oel7.local
-rw-rw-r--. 1 ansible ansible 15K Mar 6 22:22 ansiclient1.oel7.local
-rw-rw-r--. 1 ansible ansible 15K Mar 6 22:22 ansiclient2.oel7.local
[ansible@ansible ~]$

Now let’s create an inventory file using collected facts.

We can convert the inventory output in .html format, CSV format, markdown format to get into Wiki, or in the .sql format that can be loaded into an SQLite or MySQL database. Below are the commands for saving in various format.

$ ansible-cmdb -t html_fancy -p local_js=1,collapsed=1,host_details=1 facts > all_hosts.html
$ ansible-cmdb -t csv -p host_details=1 facts > all_hosts.csv
$ ansible-cmdb -t markdown -p host_details=1 facts > all_hosts.md
$ ansible-cmdb -t sql -p host_details=1 facts > all_hosts.sql

Below are the converted format.

[ansible@ansible ~]$ ls -lthr 
-rw-rw-r--. 1 ansible ansible 81 Mar 6 22:12 hosts
drwxrwxr-x. 2 ansible ansible 96 Mar 6 22:22 facts
-rw-rw-r--. 1 ansible ansible 156K Mar 7 00:38 all_hosts.html
-rw-rw-r--. 1 ansible ansible 441 Mar 7 00:39 all_hosts.csv
-rw-rw-r--. 1 ansible ansible 30K Mar 7 00:41 all_hosts.md
-rw-rw-r--. 1 ansible ansible 2.5K Mar 7 00:45 all_hosts.sql
[ansible@ansible ~]$


Below is the output from above html_fancy option.

ansible_inventory_report_output_www.oel7.com


Below is the output from the above CSV option.

Ansible Inventory in csv format
Ansible Inventory in csv format


That’s it we have successfully created an inventory using “Ansible”. Let us hear something from you by commenting in the comment section.