Ansible interview questions

28 questions from the Ansible: Automate Servers with Playbooks course, each with the correct answer and an explanation. Try to answer before you open the answer.

Ansible Basics

1. What does "agentless" mean for Ansible?

  1. It cannot use SSH
  2. It has no control node
  3. Nothing needs to be installed on managed hosts
  4. It only runs on one machine
Show answer

Answer: Nothing needs to be installed on managed hosts

Ansible connects over SSH and runs modules temporarily, so no resident agent is required.

Lesson: What Ansible Is

2. Where must Ansible be installed?

  1. Only on the control node
  2. On every managed host
  3. Inside every container image
  4. Nowhere, it is a website
Show answer

Answer: Only on the control node

Managed hosts need only SSH and Python, not Ansible itself.

Lesson: Installing and Your First Command

3. What does `[prod:children]` do in an INI inventory?

  1. Encrypts the group
  2. Deletes the prod group
  3. Creates a new host called children
  4. Makes the listed groups members of prod
Show answer

Answer: Makes the listed groups members of prod

The :children suffix means the entries are groups nested under the parent.

Lesson: Inventory: Hosts and Groups

4. How should a task that needs root normally get it?

  1. Logging in as root with a password
  2. become: true on that task or play
  3. Disabling SSH
  4. Sharing a private key in chat
Show answer

Answer: become: true on that task or play

Privilege escalation per task keeps the connection user unprivileged by default.

Lesson: SSH, Become and ansible.cfg

Playbooks

5. What does each task call?

  1. A whole play
  2. One module
  3. An inventory
  4. A vault
Show answer

Answer: One module

A task is a single module invocation with arguments.

Lesson: Anatomy of a Playbook

6. What does "changed" mean for a task result?

  1. The host is unreachable
  2. The task failed
  3. The task was skipped
  4. Ansible modified something on the host
Show answer

Answer: Ansible modified something on the host

"changed" reports that the module altered the system to reach the desired state.

Lesson: Running a Playbook and Reading the Output

7. Why is idempotency valuable?

  1. Playbooks can be re-run safely and repair drift
  2. It makes tasks slower
  3. It removes the need for inventory
  4. It only works on Windows
Show answer

Answer: Playbooks can be re-run safely and repair drift

Declared state means repeated runs converge to the same result.

Lesson: Idempotency

8. What does `--check --diff` do?

  1. Encrypts the playbook
  2. Deletes the target files
  3. Shows what would change without changing it
  4. Uploads logs to a server
Show answer

Answer: Shows what would change without changing it

Check mode simulates the run and diff displays the differences.

Lesson: Check Mode and Diff

Variables, Facts and Templates

9. Which source usually overrides almost everything else?

  1. A comment in YAML
  2. Role defaults
  3. -e extra vars on the command line
  4. The file name
Show answer

Answer: -e extra vars on the command line

Extra vars have the highest precedence, which is useful for overrides and risky if overused.

Lesson: Variables and Precedence

10. Which module collects facts?

  1. setup
  2. ping
  3. copy
  4. debug
Show answer

Answer: setup

The setup module gathers facts at the start of a play unless gather_facts is false.

Lesson: Facts About Hosts

11. What does `{{ http_port | default(80) }}` do?

  1. Raises an error if defined
  2. Always prints 80
  3. Deletes http_port
  4. Uses http_port if defined, otherwise 80
Show answer

Answer: Uses http_port if defined, otherwise 80

The default filter provides a fallback only when the variable is undefined.

Lesson: Jinja2 Templates

12. Where do you put variables that apply only to the web group?

  1. ansible.cfg only
  2. group_vars/web.yml
  3. A random hosts file
  4. A comment in the playbook
Show answer

Answer: group_vars/web.yml

Ansible loads group_vars/`<group>.yml` automatically for hosts in that group.

Lesson: group_vars and host_vars

Control Flow

13. What result do you see when a `when:` condition is false?

  1. changed
  2. skipping
  3. failed
  4. unreachable
Show answer

Answer: skipping

A task whose condition is false is skipped for that host.

Lesson: Loops and Conditionals

14. When does a handler run?

  1. Never
  2. On every run, always
  3. Before the first task
  4. When notified by a task that reported changed
Show answer

Answer: When notified by a task that reported changed

Handlers fire only on change, once at the end of the play.

Lesson: Handlers: Act Only on Change

15. What does `--tags info` do?

  1. Runs only tasks tagged info
  2. Deletes tasks tagged info
  3. Skips tasks tagged info
  4. Encrypts tags
Show answer

Answer: Runs only tasks tagged info

Tag selection narrows a run to chosen tasks.

Lesson: register, Tags and Blocks

Roles and Reuse

16. Which role directory holds the low-priority variables users can override?

  1. handlers/
  2. files/
  3. defaults/
  4. tests/
Show answer

Answer: defaults/

defaults/main.yml sets the weakest variables, designed to be overridden.

Lesson: Role Structure

17. Why pin collection versions in requirements.yml?

  1. Collections cannot be installed otherwise
  2. Pinning makes modules faster
  3. So every run uses the same, reproducible content
  4. It hides the code
Show answer

Answer: So every run uses the same, reproducible content

Unpinned updates can change behaviour unexpectedly between runs.

Lesson: Collections and Ansible Galaxy

18. Which one is evaluated at run time?

  1. include_tasks
  2. import_tasks
  3. Neither
  4. Both, always
Show answer

Answer: include_tasks

include_* is dynamic; import_* is processed when the playbook is parsed.

Lesson: Including and Importing Tasks

Security and Secrets

19. Where should the Vault password be stored?

  1. In a public chat
  2. In the same Git repo as the playbook
  3. In the playbook name
  4. Outside the repository, in a password manager or CI secret
Show answer

Answer: Outside the repository, in a password manager or CI secret

Keeping the key next to the locked data defeats the purpose of encryption.

Lesson: Ansible Vault

20. What does `no_log: true` do?

  1. Disables the task
  2. Hides the task's arguments and results from output and logs
  3. Encrypts the playbook
  4. Skips error checking
Show answer

Answer: Hides the task's arguments and results from output and logs

It prevents sensitive values from appearing in console or log output.

Lesson: Least Privilege and no_log

21. Why run production playbooks through CI or a controller?

  1. Controllers make playbooks shorter
  2. It records who ran what and enforces access control
  3. Because laptops cannot run Ansible
  4. It removes the need for review
Show answer

Answer: It records who ran what and enforces access control

Central execution gives auditability, consistent environments and permission control.

Lesson: Safe Automation Practices

Production Practices

22. What is a good idempotence test?

  1. Run it on production first
  2. Run once and hope
  3. Delete the host
  4. Run twice and expect zero changes on the second run
Show answer

Answer: Run twice and expect zero changes on the second run

If a second run still changes things, the automation is not converging to a stable state.

Lesson: Linting and Testing

23. Why use a dynamic inventory in the cloud?

  1. Servers change, so the host list should be generated from the provider
  2. It removes the need for SSH
  3. Static files are illegal
  4. It makes playbooks shorter automatically
Show answer

Answer: Servers change, so the host list should be generated from the provider

Generated inventories stay accurate as instances are created and removed.

Lesson: Environments and Dynamic Inventory

24. What does `serial: 2` do?

  1. Limits the play to two seconds
  2. Runs only two tasks
  3. Runs the play on two hosts at a time
  4. Creates two inventories
Show answer

Answer: Runs the play on two hosts at a time

serial controls batch size for rolling updates across hosts.

Lesson: Rolling Updates, Speed and Controllers

Putting It Together

25. Why deploy to a canary host first in production?

  1. It avoids needing health checks
  2. Canary hosts are faster
  3. To catch problems while only one machine is affected
  4. Production rejects batches
Show answer

Answer: To catch problems while only one machine is affected

A small first step limits the blast radius of a bad change.

Lesson: Case Study: Deploying a Web App Across Environments

26. A task using `shell: apt install nginx` shows changed on every run. What is the best fix?

  1. Use the apt module with state: present
  2. Add more shell commands
  3. Ignore the output
  4. Run it twice
Show answer

Answer: Use the apt module with state: present

A purpose-built module is idempotent and reports changed only when it installs something.

Lesson: Revision: Cheat Sheet and Self-Check

27. Which command previews file changes without applying them?

  1. ansible-doc copy
  2. ansible-vault encrypt
  3. ansible-galaxy role init
  4. ansible-playbook site.yml --check --diff
Show answer

Answer: ansible-playbook site.yml --check --diff

Check mode simulates and diff displays what would be modified.

Lesson: Revision: Cheat Sheet and Self-Check

28. You must update 200 web servers with minimal risk. What is the best approach?

  1. Update all 200 simultaneously
  2. Canary one host, then serial batches with health checks and a failure threshold
  3. Skip testing
  4. Do it manually over SSH
Show answer

Answer: Canary one host, then serial batches with health checks and a failure threshold

Gradual rollout limits the impact of a bad change and gives time to detect and stop it.

Lesson: Revision: Cheat Sheet and Self-Check

← Back to the course