CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / IaC & Config Management / ansible

ansible

Run one task across many servers (ad-hoc)

Beginneransibleconfig-mgmtssh

The ansible command runs a single module against a group of hosts from your inventory, over SSH, with no agent on the servers: check they're reachable, run a command everywhere, copy a file, restart a service or read facts. Playbooks (ansible-playbook) are the repeatable version.

Syntaxansible PATTERN -m MODULE [-a 'ARGS'] [-i INVENTORY] [-b] [-u USER] [-f FORKS] [--limit HOSTS] [--check]

Watch it run

Before running anything, check that the control node can SSH into every host and run Python on it.

devops@devops-lab: ~/ansible — zsh — 96×28
➜ ~/ansible
Step 1 of 3
cat inventory.ini

An INI inventory: groups in brackets, one host per line with optional variables, and :children to nest groups. ansible.cfg in this directory sets inventory = inventory.ini, so -i is optional here.

Anatomy of a command

$
commandansible

Ad-hoc mode: run one module once against the matched hosts.

How it works

Ansible runs on a control node (your laptop or a CI runner); targets only need SSH and Python. It reads ansible.cfg and the inventory, expands the pattern into hosts and their variables, and starts up to -f forks. For each host it opens an SSH connection (reused through ControlPersist), and for a Python module it builds an AnsiballZ zip of the module and its arguments, copies it to a temporary directory on the target (or pipes it), runs it with the target's Python, optionally through sudo with -b, and deletes it. The module does its work idempotently, checking current state first, and prints JSON: changed true/false, and module-specific fields. Ansible prints one result per host: SUCCESS (green, nothing changed), CHANGED (yellow), FAILED (red) or UNREACHABLE (SSH failed). The raw module is the exception: it runs a command without Python, used to bootstrap Python.

Options you'll actually use

-i FILEInventory file or directory (or set it in ansible.cfg)
-m MODULEModule to run (default ansible.builtin.command)
-a 'ARGS'Module arguments
-b / -KBecome root via sudo / ask for the sudo password
-u USERSSH as this user
-f NParallel forks (default 5)
-l, --limitFurther restrict the matched hosts
-C, --checkDry run for modules that support it
-e 'k=v'Extra variables (highest precedence)
--list-hostsShow which hosts the pattern matches, run nothing
-v ... -vvvvMore output; -vvvv shows SSH details

Common mistakes

ansible all -m shell -a 'apt-get install -y nginx' -b

Not idempotent, no clear changed/ok status, and it breaks if apt is locked or the package name differs per distro.

ansible all -m ansible.builtin.apt -a 'name=nginx state=present update_cache=true' -b
Running ad-hoc changes on prod as the normal way of working

Nothing is recorded or reviewable; the next playbook run may undo it.

Use ad-hoc for checks and emergencies; put real changes in a playbook in Git
Forgetting -b for system changes

The module runs as the SSH user and fails with permission denied on root-owned files.

Add -b (and -K if sudo needs a password)
ansible all ... with the default 5 forks on 500 hosts during an incident

It processes 5 hosts at a time and takes ages.

-f 50 (within what the control node and SSH can handle)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that shows free disk space on every host in the db group.

  2. 2

    Write the command that makes sure the user deploy exists with shell /bin/bash on all web hosts.

  3. 3

    Read this output and explain it: 'web2 | UNREACHABLE! => ... "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey)."'

  4. 4

    If ansible is installed in your environment, run an ad-hoc command against localhost. Otherwise run the equivalent check by hand in the sandbox: uptime and free disk space.

Related commands

ansible-playbookRun Ansible playbooks to configure serverssshLog in to remote servers securelypuppetEnforce server configuration with PuppetsudoRun a command as root or another user
Previousterraform stateNextansible-playbook
Linux / IaC & Config Management / ansible

ansible

Run one task across many servers (ad-hoc)

Beginneransibleconfig-mgmtssh

The ansible command runs a single module against a group of hosts from your inventory, over SSH, with no agent on the servers: check they're reachable, run a command everywhere, copy a file, restart a service or read facts. Playbooks (ansible-playbook) are the repeatable version.

Syntaxansible PATTERN -m MODULE [-a 'ARGS'] [-i INVENTORY] [-b] [-u USER] [-f FORKS] [--limit HOSTS] [--check]

Watch it run

Before running anything, check that the control node can SSH into every host and run Python on it.

devops@devops-lab: ~/ansible — zsh — 96×28
➜ ~/ansible
Step 1 of 3
cat inventory.ini

An INI inventory: groups in brackets, one host per line with optional variables, and :children to nest groups. ansible.cfg in this directory sets inventory = inventory.ini, so -i is optional here.

Anatomy of a command

$
commandansible

Ad-hoc mode: run one module once against the matched hosts.

How it works

Ansible runs on a control node (your laptop or a CI runner); targets only need SSH and Python. It reads ansible.cfg and the inventory, expands the pattern into hosts and their variables, and starts up to -f forks. For each host it opens an SSH connection (reused through ControlPersist), and for a Python module it builds an AnsiballZ zip of the module and its arguments, copies it to a temporary directory on the target (or pipes it), runs it with the target's Python, optionally through sudo with -b, and deletes it. The module does its work idempotently, checking current state first, and prints JSON: changed true/false, and module-specific fields. Ansible prints one result per host: SUCCESS (green, nothing changed), CHANGED (yellow), FAILED (red) or UNREACHABLE (SSH failed). The raw module is the exception: it runs a command without Python, used to bootstrap Python.

Options you'll actually use

-i FILEInventory file or directory (or set it in ansible.cfg)
-m MODULEModule to run (default ansible.builtin.command)
-a 'ARGS'Module arguments
-b / -KBecome root via sudo / ask for the sudo password
-u USERSSH as this user
-f NParallel forks (default 5)
-l, --limitFurther restrict the matched hosts
-C, --checkDry run for modules that support it
-e 'k=v'Extra variables (highest precedence)
--list-hostsShow which hosts the pattern matches, run nothing
-v ... -vvvvMore output; -vvvv shows SSH details

Common mistakes

ansible all -m shell -a 'apt-get install -y nginx' -b

Not idempotent, no clear changed/ok status, and it breaks if apt is locked or the package name differs per distro.

ansible all -m ansible.builtin.apt -a 'name=nginx state=present update_cache=true' -b
Running ad-hoc changes on prod as the normal way of working

Nothing is recorded or reviewable; the next playbook run may undo it.

Use ad-hoc for checks and emergencies; put real changes in a playbook in Git
Forgetting -b for system changes

The module runs as the SSH user and fails with permission denied on root-owned files.

Add -b (and -K if sudo needs a password)
ansible all ... with the default 5 forks on 500 hosts during an incident

It processes 5 hosts at a time and takes ages.

-f 50 (within what the control node and SSH can handle)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that shows free disk space on every host in the db group.

  2. 2

    Write the command that makes sure the user deploy exists with shell /bin/bash on all web hosts.

  3. 3

    Read this output and explain it: 'web2 | UNREACHABLE! => ... "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey)."'

  4. 4

    If ansible is installed in your environment, run an ad-hoc command against localhost. Otherwise run the equivalent check by hand in the sandbox: uptime and free disk space.

Related commands

ansible-playbookRun Ansible playbooks to configure serverssshLog in to remote servers securelypuppetEnforce server configuration with PuppetsudoRun a command as root or another user
Previousterraform stateNextansible-playbook