Whether you're prepping for an interview or improving your hands-on skills, these Ansible scenarios will help you apply automation best practices confidently.
1. You want to skip a task for a specific host while running a playbook across a group. How would you do this?
To skip a specific host from a task or playbook, use the --limit flag with the host excluded using !.
ansible-playbook --limit '!server1' playbook.yml
2. You have a playbook to install and configure a web server. You want to test changes before applying. How?
Use the --check flag to simulate a dry run of the playbook without making any actual changes.
ansible-playbook --check playbook.yml
3. You want to run a playbook only on a specific host within your inventory group. What do you use?
Use the --limit option with the hostname.
ansible-playbook playbook.yml --limit host1
4. You want to encrypt sensitive variables like passwords. How do you do it?
Use Ansible Vault to encrypt sensitive data.
ansible-vault encrypt secrets.yml
5. You need to update only 2 servers at a time during a rolling deployment. What Ansible option helps?
Use the serial keyword in your playbook.
- hosts: webservers
serial: 2
6. You need to pass a variable during playbook execution. How?
Use the --extra-vars option.
ansible-playbook playbook.yml --extra-vars "env=prod"
7. You want to prevent a task from running when a file doesn’t exist. What do you use?
Use the when condition with stat module.
- name: Check file
stat: path=/etc/myconfig.conf
register: file_check
- name: Do something
when: file_check.stat.exists
8. You want to run a playbook step-by-step for debugging. What’s the best way?
Use --step to confirm each task before it runs.
ansible-playbook playbook.yml --step
9. You need to stop execution if a task fails. How?
Tasks fail by default, but to ensure immediate failure, avoid ignore_errors.
- name: Critical task
command: /bin/false
10. You want to run only one specific task in a playbook. What do you use?
Tag the task and use --tags to run it.
- name: Install nginx
apt: name=nginx state=present
tags: nginx
ansible-playbook playbook.yml --tags nginx
11. How do you store host-specific variables?
Create a file in the host_vars directory with the hostname.
host_vars/
└── web01.yml
12. You need to loop over a list of packages to install them. How?
Use the loop directive.
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- curl
- git
13. How do you disable fact gathering to speed up playbook runs?
Set gather_facts: false in the playbook.
- hosts: all
gather_facts: false
14. You want to ensure a service is restarted only if a file was changed. How?
Use notify and handlers.
- name: Modify config
template: src=conf.j2 dest=/etc/app.conf
notify: Restart app
handlers:
- name: Restart app
service: name=myapp state=restarted
15. You want to conditionally run a task based on a variable. How?
Use the when keyword.
- name: Install nginx on prod
apt: name=nginx state=present
when: env == "prod"
16. How do you check Ansible version?
ansible --version
17. You want to run a shell script on a host. What do you use?
Use the script module.
- name: Run custom script
script: /scripts/install.sh
18. You need to fetch a file from a remote server to localhost. How?
Use the fetch module.
- name: Copy logs to local
fetch:
src: /var/log/syslog
dest: ./backup/
flat: yes
19. You want to include tasks from another YAML file. How?
Use the include_tasks module.
- include_tasks: security-hardening.yml
20. You need to remove a package from all hosts. What’s the easiest way?
- name: Remove nginx
apt:
name: nginx
state: absent
21. How do you retry a failed task a few times before marking it failed?
Use retries with until.
- name: Wait for port
wait_for: port=80
retries: 5
delay: 10
until: result is succeeded
✅ These 20 real-time Ansible scenarios cover core concepts, problem-solving, and best practices—ideal for interviews and daily automation work!
💬 Which ones have you used recently? Let me know in the comments!
Top comments (0)