DEV Community

Latchu@DevOps
Latchu@DevOps

Posted on

⚙️ 20 Real-World Ansible Scenarios with Practical Answers

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

6. You need to pass a variable during playbook execution. How?

Use the --extra-vars option.

ansible-playbook playbook.yml --extra-vars "env=prod"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
ansible-playbook playbook.yml --tags nginx
Enter fullscreen mode Exit fullscreen mode

11. How do you store host-specific variables?

Create a file in the host_vars directory with the hostname.

host_vars/
└── web01.yml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

13. How do you disable fact gathering to speed up playbook runs?

Set gather_facts: false in the playbook.

- hosts: all
  gather_facts: false
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

16. How do you check Ansible version?

ansible --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

19. You want to include tasks from another YAML file. How?

Use the include_tasks module.

- include_tasks: security-hardening.yml
Enter fullscreen mode Exit fullscreen mode

20. You need to remove a package from all hosts. What’s the easiest way?

- name: Remove nginx
  apt:
    name: nginx
    state: absent
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

✅ 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)