unit 9 · Workplace ecosystem

Jenkins + Ansible

Where your playbooks meet the pipeline

essential · 7 min · linux + windows

you may want to read 26 · Galaxy, requirements.yml & pinning , 27 · Ansible Vault & vault IDs , 29 · Run control: check, diff, tags, limit first — but nothing is locked

tl;dr

  • Two ways in: shell out to ansible-playbook on the agent, or the Jenkins plugin's ansiblePlaybook step.
  • The pipeline shape: checkout scm, ansible-galaxy install -r requirements.yml, then ansiblePlaybook.
  • Secrets — SSH keys, vault passwords — live in the Jenkins credential store, referenced by ID.

why it matters Your new shop deploys through Jenkins. Reading and modifying the stage that calls Ansible is day-one work — and it's where lesson 27's vault password finally finds a safe home.

Everything so far ran from your terminal. Your new shop runs it from Jenkins: a pipeline checks out the repo, runs the playbook, and posts the result to the PR or the deploy channel. This lesson is the seam between those worlds — the Ansible half is lessons 1–40, the Jenkins half is two steps and a credential ID.

Two ways in

There are exactly two integration approaches, and most shops mix them:

  1. Shell out. Install Ansible on the Jenkins agent and call it like you would: sh 'ansible-playbook site.yml -i inventories/prod/hosts'. Every flag from lesson 29 — --check, --diff, --tags, --limit — works unchanged, because it is the CLI.
  2. The Ansible plugin. Jenkins’s Ansible plugin adds pipeline steps — ansiblePlaybook and ansibleAdhoc — that wrap the CLI in Groovy and wire directly into Jenkins credentials. Ansible still has to be installed on the agent. The plugin only drives it.

The stage you’ll actually read

Here is the pattern lesson 26 promised you’d meet verbatim — checkout, install dependencies, run:

pipeline {
  agent any                 // an agent with Ansible installed
  stages {
    stage('Deploy') {
      steps {
        checkout scm        // the playbook repo, same commit as the PR
        sh 'ansible-galaxy install -r requirements.yml' // agent starts empty
        ansiblePlaybook credentialsId: 'private_key',   // SSH key, by ID
          inventory: 'inventories/prod/hosts',
          playbook: 'site.yml'
      }
    }
  }
}
a minimal deploy stage — the shape most Jenkinsfiles follow

Three lines carry the weight. checkout scm pulls the repo the pipeline belongs to. The ansible-galaxy install line exists because a CI agent has none of your collections or roles. The pinned requirements.yml from lesson 26 makes the agent’s run match yours. ansiblePlaybook maps one-to-one onto the CLI. inventory is -i, playbook is the positional argument, and credentialsId names an SSH private key stored in the Jenkins credential store — the key material never appears in the Jenkinsfile or the repo.

That store also ties off lesson 27’s loose end. The vault password that decrypts your secrets lives in Jenkins as a credential too. The pipeline hands it to the run at execution time — the --vault-password-file route from lesson 27, fed by Jenkins instead of a file in the repo. Encrypted values in Git, key in the credential store. The two only meet inside a job.

Making it liveable

Four practicalities:

  • Colors. Ansible detects it isn’t writing to a terminal and strips color. Set ANSIBLE_FORCE_COLOR=true in the stage’s environment and install the AnsiColor plugin. The console log then reads like your terminal.
  • Timeouts. A serial rolling update across forty hosts takes real time. Raise the job timeout before the first long run gets killed at the default and leaves half a fleet updated.
  • Targeted deploys. --limit is how pipelines do blue-green: run the play against the idle pool’s group, verify, flip traffic, rerun against the rest. Same playbook, two invocations.
  • Division of labour. Keep the Jenkinsfile orchestration-only — when to run, which inventory, which credentials — and keep configuration logic in playbooks and roles. Groovy that templates config files is logic your Ansible tooling can’t lint, test or reuse.

One version note: the plugin’s test matrix currently covers ansible 2.18–2.20 , while core sits at 2.21 — plugin coverage trails core releases. That’s rarely a blocker, but it’s one more reason teams keep the shell-out path handy: sh plus the CLI has no compatibility matrix at all.

Lesson 37 already gave this pipeline its first stage — lint before deploy. With this lesson you can read, run and modify the whole file your new team lives in. What’s left is vocabulary: lesson 42 closes the course with the jargon, debugging reflexes and interview answers.

at work, this sounds like

“Blue-green it — same job, different --limit.”
Deploy to the idle pool by restricting the run to its host group, verify, then rerun against the rest.
“Grab the vault password from the credential store.”
It's a Jenkins credential, not a file in the repo — the pipeline injects it at run time.

self-check — recall, not a test

  1. 1 Name the two ways a Jenkins pipeline can run a playbook, and one reason to pick each.

  2. 2 Why does the deploy stage run ansible-galaxy install -r requirements.yml before the playbook?

  3. 3 Where do the SSH key and the vault password live, and what must never happen to them?

next up 42 · Jargon, debugging & interview prep 8 min · unit 9