Wednesday, June 15, 2022
HomeHealthImplementing Infrastructure as Code- How NDFC works with Ansible and Terraform

Implementing Infrastructure as Code- How NDFC works with Ansible and Terraform

Automation has been the main target of curiosity within the business for fairly a while now. Out of the highest instruments accessible, Ansible and Terraform have been popularly used amongst automation fanatics like me. Whereas Ansible and Terraform are totally different of their implementation, they’re equally supported by merchandise from the Cloud Networking Enterprise Unit at Cisco (Cisco ACI, DCNM/NDFC, NDO, NXOS). Right here, we are going to talk about how Terraform and Ansible work with Nexus Dashboard Cloth Controller (NDFC). 

First, I’ll clarify how Ansible and Terraform works, together with their workflow. We’ll then take a look at the use instances. Lastly, we are going to talk about implementing Infrastructure as Code (IaC).

Ansible – Playbooks and Modules:

For these of you which are new to automation, Ansible has two primary components – the stock file and playbooks. The stock file offers details about the gadgets we’re automating together with any sandbox environments arrange. The playbook acts because the instruction guide for performing duties on the gadgets declared within the stock file. 

Ansible turns into a system of documentation as soon as the duties are written in a playbook. The playbook leverages REST API modules to explain the schema of the info that may be manipulated utilizing Relaxation API calls. As soon as written, the playbook will be executed utilizing the ansible-playbook command line.

Ansible Workflow

Terraform – Terraform Init, Plan and Apply:

Terraform has one primary half – the TF template. The template will include the supplier particulars, the gadgets to be automated in addition to the directions to be executed. The next are the three details about terraform:

  1. Terraform defines infrastructure as code and handle the total lifecycle. Creates new sources, manages present ones, and destroys ones now not essential. 
  2. Terraform presents a sublime consumer expertise for operators to predictably make adjustments to infrastructure.
  3. Terraform makes it straightforward to re-use configurations for comparable infrastructure designs.

Whereas Ansible makes use of one command to execute a playbook, Terraform makes use of three to 4 instructions to execute a template. Terraform Init checks the configuration information and downloads required supplier plugins. Terraform Plan permits the consumer to create an execution plan and examine if the execution plan matches the specified intent of the plan. Terraform Apply applies the adjustments, whereas Terraform Destroy permits the consumer to delete the Terraform managed infrastructure.

As soon as a template is executed for the primary time, Terraform creates a file referred to as terraform.state to retailer the state of the infrastructure after execution. This file is helpful when making mutable adjustments to the infrastructure. The execution of the duties can also be achieved in a declarative technique. In different phrases, the route of circulate doesn’t matter. 

Terraform Open Source workflow
Terraform Workflow


Use Circumstances of Ansible and Terraform for NDFC:

Ansible executes instructions in a prime to backside strategy. Whereas utilizing the NDFC GUI, it will get a bit tedious to handle all of the required configuration when there are quite a lot of switches in a material. For instance, to configure a number of vPCs or to take care of community attachments for every of those switches, it might get a bit tiring and takes up quite a lot of time. Ansible makes use of a variable within the playbook referred to as states to carry out varied actions equivalent to creation, modification and deletion which simplifies making these adjustments. The playbook makes use of the modules now we have relying on the duty at hand to execute the required configuration modifications. 

Terraform follows an infrastructure as code strategy for executing duties. We’ve one file which accommodates all of the duties that are executed with a terraform plan and apply command. We are able to use the terraform plan command for the supplier to confirm the duties, examine for errors and a terraform apply executes the automation. As a way to work together with software particular APIs, Terraform makes use of suppliers. All Terraform configurations should declare a supplier discipline which can be put in and used to execute the duties. Suppliers energy all of Terraform’s useful resource sorts and discover modules for rapidly deploying frequent infrastructure configurations. The supplier section has a discipline the place we specify whether or not the sources are offered by DCNM or NDFC.

Ansible Code Instance

Terraform Code Instance

Under are a number of examples of how Ansible and Terraform works with NDFC. Utilizing the ansible-playbook command we are able to execute our playbook to create a VRF and community. 

Under is a pattern of how a Terraform code execution seems to be: 

Infrastructure as Code Workflow (IaC):

Infrastructure as a Code – CI/CD Workflow

One widespread method to make use of Ansible and Terraform is by constructing it from a steady integration (CI) course of after which merging it from a steady supply (CD) system upon a profitable software construct:

  • The CI asks Ansible or Terraform to run a script that deploys a staging surroundings with the appliance.
  • When the stage assessments go, CD then proceeds to run a manufacturing deployment.
  • Ansible/Terraform can then try the historical past from model management on every machine or pull sources from the CI server.

An necessary profit that’s highlighted by IaC is the simplification of testing and verification. CI guidelines out quite a lot of frequent points if now we have sufficient check instances after deploying on the staging community. CD robotically deploys these adjustments onto manufacturing with only a easy click on of a button. 

Whereas Ansible and Terraform have their variations, NDFC helps the automation by each software program equally and clients are given the choice to decide on both one and even each.

Terraform and Ansible complement one another within the sense that they each are nice at dealing with IaC and the CI/CD pipeline. The virtualized infrastructure configuration stays in sync with adjustments as they happen within the automation scripts. 

There are a number of DevOps software program alternate options on the market to deal with the runner jobs. Gitlab, Jenkins, AWS and GCP to call a number of. 

Within the instance under, we are going to see how GitLab and Ansible work collectively to create a CI/CD pipeline.  For every change in code that’s pushed, CI triggers an automatic construct and confirm sequence on the staging surroundings for the given mission, which supplies suggestions to the mission builders. With CD, infrastructure provisioning and manufacturing deployment is ensured as soon as the confirm sequence by CI has been efficiently confirmed. 

As now we have seen above, Ansible works in comparable strategy to a standard line interpreter, we outline a set of instructions to run towards our hosts in a easy and declarative method. We even have a reset yaml file which we are able to use to revert all adjustments we make to the configuration.  

NDFC works together with Ansible and the Gitlab Runner to perform a CI/CD Pipeline. 

Gitlab Runner is an software that works with Gitlab CI/CD to run jobs in a pipeline. Our CI/CD job pipeline runs in a Docker container. We set up GitLab Runner onto a Linux server and register a runner that makes use of the Docker executor. We are able to additionally restrict the variety of individuals with entry to the runner so Pull Requests (PRs) of the merge will be raised and authorised of the merge by a choose variety of individuals. 

Step 1: Create a Repository for the staging and manufacturing surroundings and an Ansible file to maintain credentials secure. On this, I’ve used the ansible vault command to retailer the credentials file for NDFC.

Step 2: Create an Ansible file for useful resource creation. In our case, now we have one primary file for staging and manufacturing individually adopted by a group_vars folder to have all of the details about the sources. The primary file pulls the small print from the group_vars folder when executed. 

Step 3: Create a workflow file and examine the output.

As above, our and hosts.stage.yml stock information act as the primary file for implementing useful resource allocation to each manufacturing and staging respectively. Our group_vars folder accommodates all of the useful resource data together with material particulars, swap data in addition to overlay community particulars. 

For the above instance, we can be displaying how including a community to the overlay.yml file after which committing this alteration will invoke a CI/CD pipeline for the above structure. 

Optionally available Step 4: Create a password file (Optionally available). Create a brand new file referred to as password.txt containing the ansible vault password to encrypt and decrypt the Ansible vault file.

Our overlay.yml file at present has 2 networks. Our staging and manufacturing surroundings has been reset to this stage.   We’ll now add our new community network_db to the yaml file as under:

First, we make this alteration to the staging by elevating a PR and as soon as it has been verified, the admin of the repo can then approve this PR merge which is able to make the adjustments to manufacturing. 

As soon as we make these adjustments to the Ansible file, we create a department below this repo to which we commit the adjustments.

After this department has been created, we elevate a PR request. It will robotically begin the CI pipeline.

As soon as the staging verification has handed, the admin/supervisor of the repo can go forward and approve of the merge which kicks within the CD pipeline for the manufacturing surroundings.

If we examine the NDFC GUI, we are able to discover each staging and manufacturing include the brand new community network_db. 


All of our Cloud Networking merchandise help automation utilizing each Ansible and Terraform. Automating infrastructure provisioning and CI/CD deployment helps in some ways. It lets us hold a log of adjustments within the infrastructure whereas in the long run saving quite a lot of time (testing configuration adjustments, creating a very new material consisting of many sources, modifying present sources to call a number of). Fallouts requiring guide intervention are considerably diminished as we are able to revert any adjustments by a easy command. Automating the workflow helps us hold monitor of adjustments made and we received’t come throughout conditions of outages or failures the place we face a configuration change made a number of months in the past and don’t know what or why it was made. 

View the information for this IaC implementation.


Associated Hyperlinks:

Cisco DevNet Ansible Modules

Terraform DCNM Registry

Cisco Studying Lab for automation utilizing Terraform

Ansible CICD Pipeline Code Instance

Working Terraform for A number of groups and Functions





Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments