Ansible: Automate Everything from First Playbook to Production · Production Patterns: Testing, Rolling Updates and CI/CD
CI/CD Pipeline: Run Ansible from GitHub Actions
Set up a GitHub Actions workflow that lints on PRs, dry-runs against staging on merge, and deploys to production with a manual approval gate.
What you'll build
A complete .github/workflows/deploy.yml that runs ansible-lint on every pull request, performs a dry-run (--check) against staging when code merges to main, waits for manual approval, then deploys to production. SSH keys and vault passwords come from GitHub Secrets.
The idea
Running Ansible from your laptop works for a small team but creates problems as you grow: different engineers have different local versions, secrets are scattered across machines, and there's no audit trail of who deployed what. A CI/CD pipeline standardizes the process:
- PR → lint — fast feedback, no servers needed.
- Merge to main → staging deploy — automatic, with a dry-run diff.
- Manual approval → production deploy — a human reviews the staging result before production.
What goes into GitHub Secrets:
SSH_PRIVATE_KEY— the deploy user's private key (ed25519).VAULT_PASSWORD— the Ansible Vault decryption password.KNOWN_HOSTS— the SSH fingerprints for your servers (prevents MITM).
The code
cat <<'WORKFLOW'
# .github/workflows/deploy.yml
---
name: Ansible Deploy
on:
pull_request:
paths:
- "playbooks/**"
- "roles/**"
- "inventory/**"
- "requirements.yml"
push:
branches: [main]
env:
ANSIBLE_FORCE_COLOR: "1"
ANSIBLE_HOST_KEY_CHECKING: "false"
PY_COLORS: "1"
jobs:
# ── 1. Lint on every PR ──
lint:
name: ansible-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible and lint
run: |
pip install ansible-core ansible-lint
ansible-galaxy collection install -r requirements.yml
- name: Run ansible-lint
run: ansible-lint playbooks/ roles/
# ── 2. Deploy to staging on merge to main ──
staging:
name: Deploy to staging
needs: lint
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible
run: |
pip install ansible-core
ansible-galaxy collection install -r requirements.yml
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Write vault password file
run: |
echo "${{ secrets.VAULT_PASSWORD }}" > .vault_pass
chmod 600 .vault_pass
- name: Dry run against staging
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/staging.ini \
--vault-password-file .vault_pass \
--check --diff
- name: Deploy to staging
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/staging.ini \
--vault-password-file .vault_pass
- name: Cleanup secrets
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_pass
# ── 3. Deploy to production with manual approval ──
production:
name: Deploy to production
needs: staging
runs-on: ubuntu-latest
environment:
name: production
url: https://app.example.com
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible
run: |
pip install ansible-core
ansible-galaxy collection install -r requirements.yml
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Write vault password file
run: |
echo "${{ secrets.VAULT_PASSWORD }}" > .vault_pass
chmod 600 .vault_pass
- name: Deploy to production
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/production.ini \
--vault-password-file .vault_pass
- name: Verify production health
run: |
for i in $(seq 10); do
status=$(curl -s -o /dev/null -w '%{http_code}' https://app.example.com/health)
if [ "$status" = "200" ]; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i: status $status, retrying..."
sleep 10
done
echo "Health check failed after 10 attempts"
exit 1
- name: Cleanup secrets
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_pass
WORKFLOW
Output
# .github/workflows/deploy.yml
---
name: Ansible Deploy
on:
pull_request:
paths:
- "playbooks/**"
- "roles/**"
- "inventory/**"
- "requirements.yml"
push:
branches: [main]
env:
ANSIBLE_FORCE_COLOR: "1"
ANSIBLE_HOST_KEY_CHECKING: "false"
PY_COLORS: "1"
jobs:
# ── 1. Lint on every PR ──
lint:
name: ansible-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible and lint
run: |
pip install ansible-core ansible-lint
ansible-galaxy collection install -r requirements.yml
- name: Run ansible-lint
run: ansible-lint playbooks/ roles/
# ── 2. Deploy to staging on merge to main ──
staging:
name: Deploy to staging
needs: lint
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible
run: |
pip install ansible-core
ansible-galaxy collection install -r requirements.yml
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Write vault password file
run: |
echo "${{ secrets.VAULT_PASSWORD }}" > .vault_pass
chmod 600 .vault_pass
- name: Dry run against staging
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/staging.ini \
--vault-password-file .vault_pass \
--check --diff
- name: Deploy to staging
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/staging.ini \
--vault-password-file .vault_pass
- name: Cleanup secrets
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_pass
# ── 3. Deploy to production with manual approval ──
production:
name: Deploy to production
needs: staging
runs-on: ubuntu-latest
environment:
name: production
url: https://app.example.com
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Ansible
run: |
pip install ansible-core
ansible-galaxy collection install -r requirements.yml
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Write vault password file
run: |
echo "${{ secrets.VAULT_PASSWORD }}" > .vault_pass
chmod 600 .vault_pass
- name: Deploy to production
run: |
ansible-playbook playbooks/deploy.yml \
-i inventory/production.ini \
--vault-password-file .vault_pass
- name: Verify production health
run: |
for i in $(seq 10); do
status=$(curl -s -o /dev/null -w '%{http_code}' https://app.example.com/health)
if [ "$status" = "200" ]; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i: status $status, retrying..."
sleep 10
done
echo "Health check failed after 10 attempts"
exit 1
- name: Cleanup secrets
if: always()
run: |
rm -f ~/.ssh/id_ed25519 .vault_passRun this on your computer — see “Get Started” for setup.
Set up the GitHub environments for the approval gate:
cat <<'EOF'
GitHub repository settings → Environments:
1. Create "staging" environment
- No protection rules (auto-deploys on merge)
2. Create "production" environment
- Required reviewers: [your-team-lead, your-sre]
- Wait timer: 0 (manual approval, no delay after)
- Deployment branches: main only
When the staging job completes, the production job pauses and shows:
"Waiting for review — production deployment requires approval"
A reviewer clicks "Approve and deploy" to continue.
GitHub Secrets (repository level):
SSH_PRIVATE_KEY → contents of the deploy user's private key
VAULT_PASSWORD → the Ansible Vault password
KNOWN_HOSTS → output of: ssh-keyscan web1.example.com web2.example.com
EOF
Output
GitHub repository settings → Environments: 1. Create "staging" environment - No protection rules (auto-deploys on merge) 2. Create "production" environment - Required reviewers: [your-team-lead, your-sre] - Wait timer: 0 (manual approval, no delay after) - Deployment branches: main only When the staging job completes, the production job pauses and shows: "Waiting for review — production deployment requires approval" A reviewer clicks "Approve and deploy" to continue. GitHub Secrets (repository level): SSH_PRIVATE_KEY → contents of the deploy user's private key VAULT_PASSWORD → the Ansible Vault password KNOWN_HOSTS → output of: ssh-keyscan web1.example.com web2.example.com
Run this on your computer — see “Get Started” for setup.
Common mistakes
| Mistake | Fix |
|---|---|
| Storing SSH keys as plain-text files in the repo | Use GitHub Secrets; write the key to a file at runtime and clean it up with if: always() |
Setting ANSIBLE_HOST_KEY_CHECKING: "false" in production |
Use KNOWN_HOSTS from ssh-keyscan instead; disabling host key checking allows MITM attacks |
| Not pinning Ansible and collection versions in CI | Use pip install ansible-core==2.16.* and version constraints in requirements.yml to ensure reproducible builds |
| Skipping the staging dry-run | The --check --diff step catches configuration drift and unexpected changes before the real deploy |
Key points
- Lint on PRs catches issues before code is merged — no servers needed, runs in seconds.
- Staging with dry-run shows exactly what will change before applying; the real deploy follows if the diff looks right.
- GitHub Environments with required reviewers create a manual approval gate before production.
- SSH keys and vault passwords come from GitHub Secrets, written to files at runtime and cleaned up with
if: always(). - Pin Ansible and collection versions in CI for reproducible builds — different versions can produce different results.
Practice questions
Why should you run ansible-lint on pull requests rather than only during deployment?
Answer
Linting on PRs catches style issues, deprecated modules, and missing FQCNs before code is merged. This is faster feedback (seconds, no servers needed) and prevents bad patterns from accumulating in the main branch.
How does the GitHub Environments feature create a manual approval gate for production?
Answer
By configuring the production environment with 'Required reviewers', the production job pauses after staging completes and displays a review request. A designated reviewer must click 'Approve and deploy' before the production job can run. This ensures a human verifies the staging result.
Why is `if: always()` important on the cleanup step that removes SSH keys and vault passwords?
Answer
Without if: always(), the cleanup step is skipped if a previous step fails. This would leave the SSH private key and vault password file on the GitHub Actions runner's disk. With if: always(), cleanup runs regardless of whether the deploy succeeded or failed.
Open in the app to mark lessons complete, take notes and track your progress.