+

Search Tips   |   Advanced Search

Developing Cisco ACI modules

This is a brief walk-through of how to create new Cisco ACI modules for Ansible.

For more information about Cisco ACI, look at the Cisco ACI user guide.

What's covered in this section:


Introduction

The cisco.aci collection already includes a large number of Cisco ACI modules, however the ACI object model is huge and covering all possible functionality would easily cover more than 1500 individual modules.

If you need specific functionality, you have 2 options:


See also

ACI Fundamentals: ACI Policy Model

A good introduction to the ACI object model.

APIC Management Information Model reference

Complete reference of the APIC object model.

APIC REST API Configuration Guide

Detailed guide on how the APIC REST API is designed and used, incl. many examples.

So let's look at how a typical ACI module is built up.


ACI module structure


Importing objects from Python libraries

The following imports are standard across ACI modules:


Defining the argument spec

The first line adds the standard connection parameters to the module. After that, the next section will update the argument_spec dictionary with module-specific parameters. The module-specific parameters should include:

Hint

Do not provide default values for configuration arguments. Default values could cause unintended changes to the object.


Using the AnsibleModule object

The following section creates an AnsibleModule instance. The module should support check-mode, so we pass the argument_spec and supports_check_mode arguments. Since these modules support querying the APIC for all objects of the module's class, the object/parent IDs should only be required if state: absent or state: present.


Mapping variable definition

Once the AnsibleModule object has been initiated, the necessary parameter values should be extracted from params and any data validation should be done. Usually the only params that need to be extracted are those related to the ACI object configuration and its child configuration. If you have integer objects that you would like to validate, then the validation should be done here, and the ACIModule.payload() method will handle the string conversion.


Using the ACIModule object

The ACIModule class handles most of the logic for the ACI modules. The ACIModule extends functionality to the AnsibleModule object, so the module instance must be passed into the class instantiation.

The ACIModule has six main methods that are used by the modules:

The first two methods are used regardless of what value is passed to the state parameter.

Constructing URLs

The construct_url() method is used to dynamically build the appropriate URL to interact with the object, and the appropriate filter string that should be appended to the URL to filter the results.

Our design goal is to take all ID parameters that have values, and return the most specific data possible. If you do not supply any ID parameters to the task, then all objects of the class will be returned. If your task does consist of ID parameters sed, then the data for the specific object is returned. If a partial set of ID parameters are passed, then the module will use the IDs that are passed to build the URL and filter strings appropriately.

The construct_url() method takes 2 required arguments:

Example:

Some modules, like aci_tenant, are the root class and so they would not need to pass any additional arguments to the method.

The construct_url() method takes 4 optional arguments, the first three imitate the root class as described above, but are for child objects:

Sometimes the APIC will require special characters ([, ], and -) or will use object metadata in the name ('vlanns' for VLAN pools); the module should handle adding special characters or joining of multiple parameters in order to keep expected inputs simple.

Getting the existing configuration

Once the URL and filter string have been built, the module is ready to retrieve the existing configuration for the object:

When state is present

When state: present, the module needs to perform a diff against the existing configuration and the task entries. If any value needs to be updated, then the module will make a POST request with only the items that need to be updated. Some modules have children that are in a 1-to-1 relationship with another object; for these cases, the module can be used to manage the child objects.

Building the ACI payload

The aci.payload() method is used to build a dictionary of the proposed object configuration. All parameters that were not provided a value in the task will be removed from the dictionary (both for the object and its children). Any parameter that does have a value will be converted to a string and added to the final dictionary object that will be used for comparison against the existing configuration.

The aci.payload() method takes two required arguments and 1 optional argument, depending on if the module manages child objects.

Performing the request

The get_diff() method is used to perform the diff, and takes only one required argument, aci_class. Example: aci.get_diff(aci_class='fvBD')

The post_config() method is used to make the POST request to the APIC if needed. This method doesn't take any arguments and handles check mode. Example: aci.post_config()

Example code

When state is absent

If the task sets the state to absent, then the delete_config() method is all that is needed. This method does not take any arguments, and handles check mode.

Exiting the module

To have the module exit, call the ACIModule method exit_json(). This method automatically takes care of returning the common return values for you.


Testing ACI library functions

You can test your construct_url() and payload() arguments without accessing APIC hardware by using the following python script:

This will result in:


Testing for sanity checks

You can run from your fork something like:


See also

Sanity Tests

Information on how to build sanity tests.


Testing ACI integration tests

You can run this:

You may need to add --python 2.7 or --python 3.6 in order to use the correct python version for performing tests.

You may want to edit the used inventory at test/integration/inventory.networking and add something like:


See also

Integration tests

Information on how to build integration tests.


Testing for test coverage

You can run this:

Next Previous