Bootstrap FreeKB - Ansible - Cobbler dynamic inventory script
Ansible - Cobbler dynamic inventory script

Updated:   |  Ansible articles

If you are not familiar with static inventory vs. dynamic inventory, and dynamic inventory plugins vs. dynamic inventory scripts, check out Ansible - Getting Started with Dynamic Inventory.

Install Cobbler

On your control node (that's your Ansible server), use the yum list command to determine if Cobbler is installed.

yum list cobbler


If Cobbler is not installed, something like this should be returned.

Available Packages
cobbler.x86_64     2.8.5-0.3.el7      epel


In this scenario, use the yum install command to install Cobbler.

yum install epel-release cobbler


Use the systemctl command to start and enable Cobbler.

systemctl enable cobblerd
systemctl start cobblerd
systemctl status cobblerd


Cobbler also uses an HTTPD server, so lets start and enable the HTTPD server.

systemctl enable httpd
systemctl start httpd
systemctl status httpd


Ensure Cobbler does not return any errors.

cobbler check


Configure ansible.cfg

By default, enable_plugins in /etc/ansible/ansible.cfg is commented out, like this.

# enable inventory plugins, default: 'host_list', 'script', 'yaml', 'ini'
#enable_plugins = host_list, virtualbox, yaml, constructed


To use inventory plugins, you will uncomment enable_plugins, and list (in order of precedence) the plugins you will be using. As the comment suggest, you will almost always want to enable the host_list, scriptyaml, and ini inventory plugins.

# enable inventory plugins, default: 'host_list', 'script', 'yaml', 'ini'
enable_plugins = host_list, script, yaml, ini


The plugins that can be used will be found at /usr/lib/python<version>/site-packages/ansible/plugins/inventory.


Likewise, the ansible-doc command can be used to gather more information on the plugins found at /usr/lib/python<version>/site-packages/ansible/plugins/inventory.

ansible-doc --type inventory --list

advanced_host_list Parses a 'host list' with ranges
constructed        Uses Jinja2 to construct vars and groups based on existing inventory.
host_list          Parses a 'host list' string
ini                Uses an Ansible INI file as inventory source.
openstack          OpenStack inventory source
script             Executes an inventory script that returns JSON
virtualbox         virtualbox inventory source
yaml               Uses a specifically YAML file as inventory source.


Setting up Cobbler

Use the curl command to download the script to /etc/ansible.

curl -X GET -o "/etc/ansible/" --url


This is what should be displayed when listing the /etc/ansible directory. Notice lacks the x (execute) permission.

~]# ll /etc/ansible/ | grep
-rw-r--r--. 1 root root 11362 Oct  3 20:33


Use the chmod command to update to be executable.

chmod +x /etc/ansible/


Now should be executable.

~]# ll /etc/ansible/ | grep
-rwxr-xr-x. 1 root root 11362 Oct  3 20:33


Create the cobbler.ini file.

touch /etc/ansible/cobbler.ini


Add the following to the cobbler.ini file.

host =
cache_path = /tmp
cache_max_age = 900


When invoking, if the following is returned, this means that your Ansible Python 

~]# python /etc/ansible/
Traceback (most recent call last):
  File "/etc/ansible/", line 59, in <module>
    from ansible.module_utils.six import iteritems
ImportError: No module named ansible.module_utils.six


Use the ansible --version command to determin your Ansible Python module location.

ansible --version
. . .
  ansible playbook module location = /usr/lib/python2.7/site-packages/ansible


In this example, we would check to see if /usr/lib/python3.6/site-packages/ansible/module_utils contains "six". If it does, it is probably the case that the "python" command is invoking version 2 of Python. In this scenario, refer to Python - Configure the "python" command to invoke the "python3" command.


Next, you may get the following.

~]$ python /etc/ansible/
Traceback (most recent call last):
  File "/etc/ansible/", line 55, in <module>
    import xmlrpclib
ModuleNotFoundError: No module named 'xmlrpclib'


In, replaces these lines . . . 

import xmlrpclib
self.conn = xmlrpclib.Server(self.cobbler_host, allow_none=True)


To be the following.

import xmlrpc.client
self.conn = xmlrpc.client.Server(self.cobbler_host, allow_none=True)


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 56bcad in the box below so that we can be sure you are a human.