DATAGERRY as a source for Ansible inventory

Ansible is an open source automation tool for software provisioning, configuration management and application deployment. It works agentless and connects to remote machines via SSH. Remote machines in Ansible are configured in an inventory, which can be an INI style or YAML based configuration file. Ansible can get its inventory also dynamically from a remote source. With DATAGERRY we provide such a remote source (=dynamic inventory) for Ansible. In this step-by-step guide, we’ll show you the configuration.

Exportd is the part of DATAGERRY, which exports CMDB objects to external systems, like monitoring systems, configuration management, backup tools, and many many more and will also provide the Ansible dynamic inventory. If you are looking for some background information, have a look at our Exportd article. Exportd is organized in Jobs. Each Job contains a set of objects that should be exported, a destination of the export and a mapping of object fields to values an external system will need. Export Jobs can push informations to an external system or external systems can trigger jobs via the Rest API and can get required data directly. For our Ansible dynamic inventory, we use Pull jobs, as Ansible will pull inventory informations when it is executed.

Our Example Type
In our example we configured a type server in DATAGERRY. A server has a management IP address, a hostname and some additional informations like the operating system or install date. We also added a few checkboxes, to store, which type of software is installed on a server.

Configuring the Export: Basics and Sources
To configure the export in DATAGERRY, choose “Exportd Job Settings” from the menu and create a new export job. At first, set a label for the job and choose “Pull” as type. By default, Exportd pushes data to external systems. “Pull” means, an external system will ask for information via REST from Exportd:

In a second step, we’ll define the objects, that should be part of the Ansible inventory. In our case, we want to export all objects of type server:

Configuring the Export: Destinations
In the next step, we need to tell DATAGERRY, where the CMDB objects should be exported. Add a destination and choose ExternalSystemAnsible from the list, to provide an Ansible dynamic inventory. You can leave the paramters of the destination empty, as ExternalSystemAnsible does not evaluate parameters at the moment.

Configuring the Export: Variables
Export Variables are a mapping of fields of DATAGERRY objects to values that are required by the Ansible dynamic inventory. For each system of our Ansible inventory, we’ll need the hostname, which is a FQDN or IP address, that Ansible will use for connecting to the host. So, in our example we’ll add the Export Variable hostname:

The value of a variable uses Jinja2 syntax. You can access the public id of an object, the value object fields and even access fields of referenced objects. Have a look at the following examples:

# access the ID of the current object

# access the field "management-ip" of the current object

# dereference an object reference in field "location" (access the field "city" of the referenced object)
# max 3 levels were dereferenced

In our case, we’ll set the value of the Export Variable hostname to the value of the field management-ip of our server object. If you export objects of multiple types (e.g. server and router), you can setup different values for each object type.

For our Ansible inventory, the following Export Variables can be set:

You can get this overview shown above by clicking on the blue “Variable Helpers” button.

As a second Export Variable, we’ll define a host variable for Ansible. In our example case, we want to export the operating system as Ansible host variable with the name os and use the field operating-system as value:

We also can define Ansible group memberships. For adding a CMDB object to the group webserver, we’ll define the Export Variable group_webserver. If the value of the Export Variable (in our case the field webserver) is True, the object will be added to the group.

Checkbox fields in object types are perfect for controlling the group memberships.

Configuring the Export: Finish the configuration
After configuring the Export Variables, finish the configuration of your Export Job.

Create user/group in DATAGERRY
The Export Job we configured for Ansible is a “Pull” job. That means, it is triggered via the DATAGERRY Rest API. For accessing the Rest API a valid user is required. To create a user with the minimum rights, we’ll add a new user group. In the menu, click on System -> User Management -> Groups and add a new group. Choose a name and label for your group. The only right, the group needs, is

After that, we create a new user Ansible and add it to our new group Exportd

Configure Ansible
At the moment, the Ansible integration works with the inventory script plugin of Ansible (we are working on a native plugin :wink: ). We provide a small script in the conrtrib directory of DATAGERRY, just have a look at our Git Repository. Download the script, make it executable and change the variables in the script to fit your own need:

# config variables

Start Ansible with the -i flag:

ansible -i [...]