Infrastructure and Automation · Topic 3 of 8

CI/CD for Infrastructure and Infrastructure as Code

Learn the parts and benefits of a CI/CD pipeline for infrastructure automation, plus infrastructure as code principles such as idempotence and drift.

  • 5 min
  • Includes a diagram
In a nutshell
  • IaC describes infrastructure in files that are kept under version control.
  • Git is the source of truth for the desired state.
  • Idempotence means that repeated runs give the same result.

What it is

Infrastructure as code, or IaC, means describing servers, networks and their settings in files that are stored, reviewed and applied like software.

A CI/CD pipeline for infrastructure then tests those files and applies them to the network automatically. Together they bring software discipline to network operations.

The aim is changes that are repeatable, reviewed, tested and easy to roll back.

Engineeredits config filescommit and reviewGitsource of truthtriggersPipelinevalidate, test, simulateapprovedNetworkchanges applied

How it works

The files in Git describe the desired state, and the pipeline moves that state to the network.

Principles of infrastructure as code

  • Source of truth. Desired configuration lives in version control, not in anyone's memory or on the devices alone.
  • Declarative description. You state the end result, and the tool works out the steps.
  • Idempotence. Running the same code twice gives the same result. The second run changes nothing.
  • Repeatability. The same code builds the same environment every time, in a lab or in production.
  • Version control and review. Every change has an author, a reason and a reviewer, and you can see the history.
  • Automation over manual work. People change the code, and machines apply it.
  • Immutability, where possible. Replace a component with a fresh one instead of patching it by hand.

Declarative and imperative

StyleIdeaExample
DeclarativeDescribe the desired end stateTerraform configuration, a YANG based service model
ImperativeDescribe the steps to runA Python script or a shell script

Many tools mix the two. An Ansible task is written as a step, but most modules check the current state and change only what differs.

Configuration drift

Drift happens when a device's real configuration no longer matches the code, usually after a manual change. IaC reduces it. The pipeline can compare the device to the source of truth and report or correct the difference.

Templates and variables

IaC separates the pattern from the data. A template holds the pattern, and a variables file holds the values for each device or site.

# vars.yaml
vlans:
  - id: 110
    name: CAMERAS
  - id: 120
    name: PRINTERS

The same template with different variables builds the configuration for each site.

CI/CD pipeline components

An engineer commits config files to Git, which triggers a pipeline of validate, lab test, manual approval, production deploy and verify stages before the change reaches the network; a problem is rolled back by reverting the commit.

ComponentRole in infrastructure automation
Source controlHolds configuration, templates, playbooks and tests
TriggerA commit, a merge or a pull request starts the pipeline
ValidationSyntax checks and linting of YAML, JSON and templates
TestUnit tests, a run in a lab or simulator, and checks of the planned change
ApprovalA reviewer or a manual gate before production
DeploymentThe tool, such as Ansible or Terraform, applies the change
VerificationPost change checks, such as a pyATS comparison, and a way to roll back

A small pipeline file might look like this.

stages:
  - validate
  - test
  - deploy

lint:
  stage: validate
  script:
    - yamllint .

lab_test:
  stage: test
  script:
    - ansible-playbook -i lab_inventory site.yml --check

deploy_prod:
  stage: deploy
  script:
    - ansible-playbook -i prod_inventory site.yml
  when: manual

The --check option runs a playbook in dry run mode, so Ansible reports what it would change. The when: manual line makes the production step wait for a person.

Benefits

  • Speed. Changes that took days can take minutes.
  • Consistency. Every device gets the same treatment.
  • Fewer errors. Automated tests catch mistakes before production.
  • Audit trail. Git records who changed what and why.
  • Easy rollback. Revert the commit and run the pipeline again.
  • Collaboration. Teams review changes through merge requests.

Challenges

IaC needs new skills, careful secret handling and good tests. A pipeline can also push a wrong change very quickly, so approvals and staged rollouts matter.

Exam tip

Two words sum up IaC. Declarative means you describe the end state, and idempotent means that applying it again changes nothing.

What to remember for the exam

Seven facts cover this topic.

  • IaC describes infrastructure in files that are kept under version control.
  • Git is the source of truth for the desired state.
  • Idempotence means that repeated runs give the same result.
  • Declarative tools state the end result, and imperative scripts state the steps.
  • Drift is a gap between the real configuration and the code.
  • A pipeline validates, tests, approves, deploys and verifies a change.
  • Benefits include speed, consistency, an audit trail and easy rollback.

Common mistakes

Four mistakes are typical.

Editing devices by hand. This creates drift from the code.

Skipping the lab stage. A bad change then goes straight to production.

Storing passwords in the repository. Use a secret store or vault for them.

Confusing declarative and idempotent. One is about how you describe a goal, and the other is about what happens on a repeat run.

Watch out

A pipeline that deploys on every commit can break production fast. Keep a manual approval before production changes.

Check yourself

Work through these three questions.

  • What does idempotent mean in infrastructure as code?

    Running the same code again produces the same result, and the second run changes nothing.

  • Why keep infrastructure files in Git?

    Git is the source of truth, and it gives history, review and an easy way to roll back.

  • What is configuration drift?

    It is a difference between the real device configuration and the code, often caused by a manual change.

Topic 3 of 8 in Infrastructure and Automation

Test yourself on Infrastructure and Automation

The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.