Skip to content

IaC & CI/CD: Ansible

Ansible commands from the IaC & CI/CD folder.

Index
6 items

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

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/…