💡 Landing directly on this lab without having done the previous ones? Local prerequisite: none (you just write a YAML workflow). To run it: a GitHub account + a fork of your role.
🔗 Ansible CI with GitHub Actions (in French)
A complete CI for an Ansible role orchestrates, on every PR:
1. Lint (ansible-lint --profile production + yamllint)
2. Molecule test (matrix OS × Ansible versions)
3. (optional) Publish to Galaxy on git tag
GitHub Actions is free for public repos, ideal for open source roles.
By the end of this lab, you will know how to:
- Write
.github/workflows/test.ymlwith 2 jobs (lint + molecule). - Use
strategy.matrixto test N distros × M Ansible versions in parallel. - Cache
pip installbetween runs withactions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0. - Configure minimal permissions (
permissions: {}). - Disable
persist-credentials: falseonactions/checkout(security).
No local installation. Optional: act to run GitHub Actions workflows locally
(debug).
labs/ci/github-actions/
├── README.md
├── requirements.yml ← collections + roles
├── roles/webserver/
└── .github/
└── workflows/
└── test.yml ← full CI workflow
---
name: Test webserver role
on:
push:
branches: [main]
pull_request:
schedule:
- cron: "0 6 * * 1" # Monday morning (anti-rot)
permissions: {} # least privilege: grant per job, only what it needs
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false # security
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
with:
python-version: "3.12"
cache: pip
- run: pip install ansible-lint yamllint
- run: yamllint .
- run: ansible-lint --profile production
molecule:
runs-on: ubuntu-latest
needs: lint
strategy:
fail-fast: false
matrix:
distro:
- quay.io/centos/centos:stream10
- docker.io/library/debian:12
- docker.io/library/ubuntu:24.04
ansible: ["2.16", "2.17", "2.18"]
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
with:
python-version: "3.12"
cache: pip
- run: |
pip install molecule molecule-plugins[podman]
pip install ansible-core>=${{ matrix.ansible }},<$(echo ${{ matrix.ansible }} | awk -F. '{print $1"."$2+1}')
- run: molecule test
env:
MOLECULE_DISTRO: ${{ matrix.distro }}🔍 Observation:
needs: lint: themoleculejob runs only iflintpasses.fail-fast: false: if a combination fails, we continue the others to get a complete report.matrix.distro × matrix.ansible= 9 jobs in parallel.
permissions: {}
# OR more precise:
permissions:
contents: read
pull-requests: write # only if we comment the PR🔍 Observation: by default, GITHUB_TOKEN has a lot of rights. In
production, we reduce to the strict minimum to limit the blast radius if a
workflow is compromised.
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false🔍 Observation: by default, actions/checkout leaves the GITHUB_TOKEN
accessible on the runner's filesystem. If a malicious dependency exfiltrates it,
it is game over. Disable it unless you explicitly need it (to push an auto
commit for example).
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
with:
python-version: "3.12"
cache: pip🔍 Observation: without cache, each run reinstalls ~50 pip packages (~30 s). With cache, it is ~5 s. On 9 matrix jobs, that changes the total CI duration.
on:
schedule:
- cron: "0 6 * * 1" # Monday morning🔍 Observation: a role can break without touching the code (Ansible upstream changes, distro upgrade). Running the CI weekly detects silent regressions.
needs:chains the jobs (lint → molecule).strategy.matrix= free parallelization. Multi-distro × multi-version in a few lines of YAML.- Minimal permissions +
persist-credentials: false= defense in depth. schedule= detect silent regressions (anti-rot).- GitHub Actions is free on public repos. On private repos: 2000 free minutes/month, then billed.
-
How would you adapt your solution if the target went from 1 host to a fleet of 50 servers? Which parameters (
forks,serial,strategy) would you need to tune to keep acceptable execution times? -
Which alternative Ansible modules could you have used to reach the same result? What are their trade-offs (guaranteed idempotence, performance, external collection dependency)?
-
If a playbook step fails mid-run, what is the impact on the hosts already processed? How do you make the scenario resumable (
block/rescue/always,--start-at-task,serial)?
See challenge/README.md.
act(local):act -W .github/workflows/test.ymlruns the workflow on your machine.- Reusable workflows:
uses: org/.github/workflows/ansible-test.yml@mainto share across several roles. - Dependabot: auto-PR to update action versions
(it bumps the pinned SHA and its
# vX.Y.Zcomment, so pinning stays maintainable). - GitGuardian / TruffleHog: add a secret scanning job on top of pre-commit.
ansible-lint labs/ci/github-actions/