Skip to content

Commands: IaC & CI/CD

Terraform, Ansible, Helm, GitHub Actions, GitLab CI and Jenkins commands.

Index
  • #terraform
  • #ansible
  • #helm
  • #github-actions
  • #gitlab
  • #jenkins
Updated

Also:tf · iac · ci · cd · pipelines · gh · glab · playbook

23 items

Terraform6

Terraform: Initialize and upgrade providers

Safe

Download modules/providers and upgrade within the version constraints.

terraform init -upgrade
What each part does
  1. terraform initset up the backend, download providers and modules
  2. -upgradepick the newest versions the constraints allow and update .terraform.lock.hcl

Also known as: tf init

Terraform: Plan and save to a file

Safe

Preview changes and save the exact plan that will be applied.

terraform plan -out=tfplan
What each part does
  1. terraform planshow what would change
  2. -out=tfplansave the plan so terraform apply tfplan does exactly that

Also known as: tf plan

Terraform: Apply a saved plan

Caution

Applies exactly what was reviewed in the saved plan. No re-plan surprises.

terraform apply tfplan
What each part does
  1. terraform applymake the changes
  2. tfplanthe saved plan file: applies exactly what was reviewed, with no new prompt

Also known as: tf apply

Terraform: Inspect state for a resource

Safe

All attributes Terraform has recorded for one resource.

terraform state show <resource-address>
What each part does
  1. terraform state showprint every attribute Terraform has stored for one resource
  2. <resource-address>e.g. aws_instance.web or module.vpc.aws_subnet.private[0] (see terraform state list)
Example
$ terraform state show 'module.vpc.aws_subnet.private[0]'

Also known as: tf state show · terraform state list

Terraform: Release a stuck state lock

Destructive

Removes a lock left behind by a crashed run. Make sure nothing else is running first.

terraform force-unlock <lock-id>
What each part does
  1. terraform force-unlockremove a state lock left by a crashed or killed run
  2. <lock-id>the ID shown in the Error acquiring the state lock message; make sure no one else is running

My note The lock ID is in the error message. See the gotcha on state locks.

Also known as: Error acquiring the state lock · state lock · ConditionalCheckFailedException · tf lock

Terraform: Enable debug logging

Safe

Write provider/API level debug logs to a file.

TF_LOG=DEBUG TF_LOG_PATH=./tf-debug.log terraform plan
What each part does
  1. TF_LOG=DEBUGlog level for this run: TRACE, DEBUG, INFO, WARN, ERROR
  2. TF_LOG_PATH=./tf-debug.logwrite the log to a file instead of stderr
  3. terraform planthe command to debug

Also known as: TF_LOG · tf debug

Ansible6

Ansible: Ping all hosts in an inventory

Safe

Checks SSH connectivity and Python on every host (not ICMP ping).

ansible all -i <inventory> -m ping
What each part does
  1. ansiblerun a single ad-hoc module (no playbook)
  2. allhost pattern: every host in the inventory
  3. -i <inventory>inventory file or directory
  4. -m pingthe ping module: checks SSH login + Python on the host (not ICMP); success prints pong

Also known as: UNREACHABLE · ansible ping

Ansible: Dry-run a playbook with diff

Safe

Shows what would change, file diffs included, without changing anything.

ansible-playbook -i <inventory> <playbook> --check --diff
What each part does
  1. ansible-playbookrun a playbook (a YAML list of plays and tasks)
  2. -i <inventory>inventory file or directory listing the hosts
  3. <playbook>the playbook file, e.g. site.yml
  4. --checkdry run: report what would change, change nothing
  5. --diffshow a before/after diff for templated and edited files

Also known as: check mode · dry run

Ansible: Run a playbook on a subset of hosts

Caution

Target one host or group, e.g. --limit web-01 or --limit 'web:!web-03'.

ansible-playbook -i <inventory> <playbook> --limit <host-pattern>
What each part does
  1. ansible-playbookrun a playbook
  2. -i <inventory>inventory file or directory
  3. <playbook>the playbook file
  4. --limit <host-pattern>only run on hosts matching the pattern, e.g. web01, webservers, 'web*:!web03'

Also known as: limit hosts · canary

Ansible: Run an ad-hoc command as root

Caution

Quick one-off command on many hosts with privilege escalation.

ansible <host-pattern> -i <inventory> -b -m shell -a "<command>"
What each part does
  1. ansible <host-pattern>run an ad-hoc module against matching hosts
  2. -i <inventory>inventory file or directory
  3. -bbecome: escalate privileges (sudo to root by default)
  4. -m shellshell module: runs through /bin/sh, so pipes and redirects work
  5. -a "<command>"the module arguments: the command line to run
Example
$ ansible web -i inventory/prod -b -m shell -a "df -h /"

Also known as: ad-hoc · ansible shell

Ansible: Show facts for a host

Safe

Gathered facts (OS, IPs, mounts…). Filter with a glob like ansible_distribution*.

ansible <host> -i <inventory> -m setup -a "filter=<fact-filter>"
What each part does
  1. ansible <host>run an ad-hoc module on one host (or pattern)
  2. -i <inventory>inventory file or directory
  3. -m setupsetup module: gathers the facts Ansible knows about the host
  4. -a "filter=<fact-filter>"only return matching facts, e.g. ansible_distribution* or ansible_default_ipv4

Also known as: ansible facts · setup module

Ansible: Edit an encrypted vault file

Safe

Decrypts into $EDITOR and re-encrypts on save.

ansible-vault edit <file>
What each part does
  1. ansible-vault editdecrypt to a temp file, open it in $EDITOR, re-encrypt on save
  2. <file>the vault-encrypted file; asks for the vault password (or use --vault-password-file)

Also known as: ansible vault · vault password

Helm3

Helm: Upgrade or install a release

Safe

Idempotent deploy. --atomic rolls back automatically if the upgrade fails.

helm upgrade --install <release> <chart> -n <namespace> -f <values-file> --atomic --wait
What each part does
  1. helm upgrade --install <release>upgrade the release, or install it if it doesn't exist
  2. <chart>chart reference: repo/chart, a path, or an OCI URL
  3. -n <namespace>target namespace (add --create-namespace if it may not exist)
  4. -f <values-file>values file overriding the chart defaults
  5. --atomicroll back automatically if the upgrade fails
  6. --waitwait for pods, services etc. to be ready before reporting success

Also known as: helm upgrade · helm install

Helm: Show release history

Safe

Revisions, status and chart/app versions of a release.

helm history <release> -n <namespace>
What each part does
  1. helm history <release>every revision: number, date, status, chart and app version, description
  2. -n <namespace>namespace of the release

Also known as: helm history · another operation (install/upgrade/rollback) is in progress

Helm: Roll back a release

Caution

Return the release to an earlier revision (see helm history).

helm rollback <release> <revision> -n <namespace> --wait
What each part does
  1. helm rollback <release>redeploy a previous revision of a release (as a new revision)
  2. <revision>revision number from helm history; leave it out for the previous one
  3. -n <namespace>namespace of the release
  4. --waitwait until the resources are ready

Also known as: helm rollback

GitHub Actions2

GitHub Actions: Show logs of failed steps

Safe

Only the log output of failed steps. gh run list shows recent run ids.

gh run view <run-id> --log-failed
What each part does
  1. gh run view <run-id>show a workflow run (gh run list gives the IDs)
  2. --log-failedprint only the logs of the failed steps

Also known as: gha logs · workflow failed · gh run view

GitHub Actions: Trigger a workflow manually

Safe

Kick off a workflow_dispatch workflow on a branch.

gh workflow run <workflow-file> --ref <branch>
What each part does
  1. gh workflow run <workflow-file>start a workflow that has a workflow_dispatch trigger, e.g. deploy.yml
  2. --ref <branch>branch or tag to run it on; pass inputs with -f name=value

Also known as: workflow_dispatch · gh workflow run

GitLab CI4

GitLab CI: Watch the pipeline for this branch

Safe

Live status of the current branch's pipeline from the terminal.

glab ci status --live
What each part does
  1. glab ci statusstatus of the latest pipeline on the current branch, job by job
  2. --livekeep refreshing until the pipeline finishes

Also known as: glab ci · pipeline status

GitLab CI: Show a job's log

Safe

Stream the log of a CI job (pick interactively if you omit the id).

glab ci trace <job-id>
What each part does
  1. glab ci tracestream a CI job's log (follows it while running)
  2. <job-id>the job ID; leave it out to pick a job interactively

Also known as: job log · glab trace

GitLab CI: Lint .gitlab-ci.yml

Safe

Validate the CI config against the GitLab instance before pushing.

glab ci lint
What each part does
  1. glab ci lintsend .gitlab-ci.yml to the GitLab CI lint API and report syntax / config errors

Also known as: yaml invalid · ci lint

GitLab CI: Run a pipeline on a branch

Safe

Trigger a new pipeline, e.g. to re-deploy.

glab ci run --branch <branch>
What each part does
  1. glab ci runcreate a new pipeline
  2. --branch <branch>the branch to run it on; add --variables KEY:value for CI variables

Also known as: trigger pipeline

Jenkins2

Jenkins: Tail a build's console log

Safe

Last 100 lines of the latest build's console without opening the UI.

curl -sS -u <user>:<api-token> "<jenkins-url>/job/<job>/lastBuild/consoleText" | tail -n 100
What each part does
  1. curl -sSquiet, but still show errors
  2. -u <user>:<api-token>basic auth with a Jenkins API token
  3. /job/<job>/lastBuild/consoleTextplain-text console output of the latest build (use a build number instead of lastBuild)
  4. | tail -n 100only the last 100 lines

Also known as: jenkins console · build log

Jenkins: Trigger a parameterized build

Safe

Start a job remotely with parameters via the REST API.

curl -X POST -u <user>:<api-token> "<jenkins-url>/job/<job>/buildWithParameters?<param>=<value>"
What each part does
  1. curl -X POSTtrigger endpoints need POST
  2. -u <user>:<api-token>basic auth with an API token (no CSRF crumb needed with a token)
  3. /job/<job>/buildWithParametersqueue a build of a parameterized job
  4. ?<param>=<value>job parameters, joined with &

Also known as: jenkins api · remote build

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…