Ansible - Cobbler dynamic inventory script

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.

[inventory]
# 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.

[inventory]
# 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.

advanced_host_list.py
constructed.py
host_list.py
ini.py
openstack.py
script.py
virtualbox.py
yaml.py

 

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 cobbler.py script to /etc/ansible.

curl -X GET -o "/etc/ansible/cobbler.py" --url https://raw.githubusercontent.com/ansible-collections/community.general/main/scripts/inventory/cobbler.py

 

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

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

 

Use the chmod command to update cobbler.py to be executable.

chmod +x /etc/ansible/cobbler.py

 

Now cobbler.py should be executable.

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

 

Create the cobbler.ini file.

touch /etc/ansible/cobbler.ini

 

Add the following to the cobbler.ini file.

[cobbler]
host = http://127.0.0.1/cobbler_api
cache_path = /tmp
cache_max_age = 900

 

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

~]# python /etc/ansible/cobbler.py
Traceback (most recent call last):
  File "/etc/ansible/cobbler.py", 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/cobbler.py
Traceback (most recent call last):
  File "/etc/ansible/cobbler.py", line 55, in <module>
    import xmlrpclib
ModuleNotFoundError: No module named 'xmlrpclib'

 

In Cobbler.py, 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)

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |