HomeAnsibleAnsible Basics & Core Concepts
Ansible Basics & Core Concepts

Ansible Basics & Core Concepts

  1. What is Ansible, and how does it differ from other configuration management tools (like Puppet, Chef, or SaltStack)?
  2. Explain the difference between Ad-hoc commands and Playbooks in Ansible.
  3. What are Inventory files in Ansible? How do you manage dynamic inventories?
  4. Can you explain the difference between static and dynamic inventory? Give examples of when youโ€™d use each.
  5. What is the difference between roles and playbooks?
  6. How does idempotency work in Ansible?

๐Ÿ”น Intermediate โ€“ Advanced Concepts

  1. How do you handle secrets management in Ansible (Vault, HashiCorp Vault, AWS Secrets Manager)?
  2. Explain handlers in Ansible and when they are triggered.
  3. How do you use conditionals and loops in Ansible playbooks? Give examples.
  4. What are facts in Ansible? How do you use custom facts?
  5. How do you implement error handling in Ansible (ignore_errors, failed_when, rescue blocks)?
  6. What is the difference between delegate_to, local_action, and run_once?

๐Ÿ”น Real-world Scenarios

  1. Suppose a task should run only if the previous one fails โ€” how do you implement it?
  2. You need to deploy the same configuration across multiple environments (Dev, QA, Prod). How do you design your playbooks and inventories?
  3. How would you ensure zero-downtime deployment using Ansible?
  4. How do you handle package version drift across multiple servers using Ansible?
  5. If you have 200+ servers, how would you optimize Ansible performance?

๐Ÿ”น Integration & DevOps Practices

  1. How do you integrate Ansible with Jenkins/GitLab CI/Azure DevOps pipelines?
  2. Have you used Ansible for cloud provisioning (AWS, Azure, GCP)? Can you give an example?
  3. How do you use Ansible with Docker and Kubernetes?
  4. How do you store and reuse Ansible playbooks across multiple projects?

๐Ÿ”น Troubleshooting & Best Practices

  1. If a playbook fails halfway, how do you resume execution from the failed task?
  2. How do you debug Ansible playbooks (-vvv, ansible-playbook –step, etc.)?
  3. What are some best practices you follow while writing playbooks?
  4. How do you test Ansible playbooks before applying them in production (Molecule, Ansible Lint, etc.)?
  5. Have you faced any Ansible pitfalls (e.g., large inventory execution, slow SSH connections)? How did you solve them?

โœ… With 4โ€“5 years of experience, interviewers will often mix theory + hands-on scenarios. They may ask you to:

  • Write a small playbook on the spot.
  • Debug a broken YAML snippet.
  • Explain how youโ€™d structure Ansible for multi-environment deployments.

Perfect ๐Ÿ‘ Since you asked for more scenario-based Ansible interview questions, Iโ€™ll focus on real-world problem-solving โ€” the type of challenges DevOps engineers face in 4โ€“5 years of experience.


๐Ÿ”น Scenario-Based Ansible Interview Questions

1. Multi-environment Setup

  • You need to configure Dev, QA, and Prod environments with slightly different variables.
    How will you structure your inventories, group_vars, and playbooks to avoid duplication?

2. Zero-Downtime Deployment

  • Youโ€™re deploying a web application to 100 servers. The deployment must be rolling (one batch at a time) to avoid downtime.
    ๐Ÿ‘‰ How would you write the playbook? (Hint: use serial and max_fail_percentage.)

3. Secrets & Security

  • You need to store database credentials and API keys securely in Ansible.
    ๐Ÿ‘‰ Would you use Ansible Vault, environment variables, or an external secret manager (e.g., AWS Secrets Manager)? Why?

4. Failover Handling

  • During deployment, one task fails on 5% of servers due to a package conflict. The rest should continue normally.
    ๐Ÿ‘‰ How will you handle partial failures while still completing the playbook run?

5. Performance Optimization

  • Running a playbook on 500+ servers is taking too long.
    ๐Ÿ‘‰ What would you do to improve speed? (e.g., forks, pipelining, ssh multiplexing, async + poll).

6. Dynamic Inventory

  • You need to manage AWS EC2 instances that scale up and down dynamically.
    ๐Ÿ‘‰ How do you configure Ansible to always pick the latest hosts without manual editing of the inventory?

7. Compliance & Drift

  • Your security team wants to ensure all servers have a specific version of OpenSSL.
    ๐Ÿ‘‰ How would you enforce and regularly check compliance using Ansible?

8. CI/CD Integration

  • Youโ€™re using Jenkins/GitLab CI to run Ansible playbooks after each code commit.
    ๐Ÿ‘‰ How do you ensure playbooks run only on affected servers and not the entire inventory?

9. Handling Large Files

  • You need to distribute a 1 GB WAR file to 200 servers.
    ๐Ÿ‘‰ How would you optimize this process with Ansible to avoid bottlenecks?

10. Rolling Back

  • Your Ansible deployment fails in production after updating a config file.
    ๐Ÿ‘‰ How do you rollback to the previous version using Ansible?

11. Conditional Deployments

  • You want to install a package only if itโ€™s not already installed or update it only if a new version is available.
    ๐Ÿ‘‰ How would you write this playbook using when, register, and changed_when?

12. Delegation

  • A playbook needs to fetch files from all servers and store them in a central log server.
    ๐Ÿ‘‰ How do you use delegate_to and run_once?

13. Error Recovery

  • You want to retry failed tasks automatically with a wait time before re-execution.
    ๐Ÿ‘‰ How do you implement retries in Ansible?

14. Blue-Green Deployment

  • You need to implement a blue-green deployment strategy using Ansible for a web app cluster.
    ๐Ÿ‘‰ How would you structure the inventory and playbooks to handle traffic switching?

15. Ansible + Kubernetes

  • Youโ€™re using Ansible to deploy apps into a Kubernetes cluster.
    ๐Ÿ‘‰ How would you handle kubectl configuration, secrets, and namespaces across multiple clusters?

16. Troubleshooting

  • A playbook runs fine on Ubuntu servers but fails on RHEL servers.
    ๐Ÿ‘‰ How would you debug and fix such OS-specific issues?

17. Parallel vs Sequential Execution

  • Some tasks must run in parallel (like package installation), while others must run sequentially (like DB migrations).
    ๐Ÿ‘‰ How would you control execution order in Ansible?

18. Compliance Reports

  • Management wants a report of applied changes after each Ansible run.
    ๐Ÿ‘‰ How would you generate logs or reports from Ansible runs (callback plugins, JSON output, etc.)?

19. Inventory Groups

  • You have a group of 50 servers, but a playbook should run only on 10 randomly selected servers for testing.
    ๐Ÿ‘‰ How would you achieve this with Ansible?

20. Migrating Legacy Systems

  • You need to migrate apps from CentOS 7 to Ubuntu 22.04 using Ansible.
    ๐Ÿ‘‰ How do you handle OS-specific modules, package managers, and playbook structures?

๐Ÿ’ก Pro Tip: In interviews, when asked scenario-based questions, donโ€™t just say โ€œuse Ansible Vaultโ€ or โ€œuse serialโ€. Instead, explain:

  • Why you chose that approach
  • How youโ€™d implement it (with Ansible syntax if possible)
  • What trade-offs exist

1. Multi-environment Setup (Dev, QA, Prod)

๐Ÿ‘‰ Use inventory + group_vars

# inventory/hosts

[dev]

dev-app-01 ansible_host=10.0.1.10

[qa]

qa-app-01 ansible_host=10.0.2.10

[prod]

prod-app-01 ansible_host=10.0.3.10

# group_vars/dev.yml

app_port: 8080

# group_vars/prod.yml

app_port: 80

# site.yml

– hosts: all

  tasks:

    – name: Start app service

      service:

        name: myapp

        state: started

      vars:

        port: “{{ app_port }}”


๐Ÿ”น 2. Zero-Downtime Rolling Deployment

๐Ÿ‘‰ Use serial and max_fail_percentage

– hosts: webservers

  serial: 10

  max_fail_percentage: 20

  tasks:

    – name: Deploy new version

      copy:

        src: myapp.war

        dest: /opt/tomcat/webapps/

      notify: restart tomcat

  handlers:

    – name: restart tomcat

      service:

        name: tomcat

        state: restarted


๐Ÿ”น 3. Secrets Management with Ansible Vault

๐Ÿ‘‰ Encrypt secrets with ansible-vault encrypt

# vars/secret.yml (vault encrypted)

db_user: admin

db_pass: !vault |

          $ANSIBLE_VAULT;1.1;AES256

          3834343333… (encrypted data)

– hosts: db

  vars_files:

    – vars/secret.yml

  tasks:

    – name: Configure DB

      template:

        src: db.conf.j2

        dest: /etc/db.conf


๐Ÿ”น 4. Handle Partial Failures

๐Ÿ‘‰ Use ignore_errors and rescue

– hosts: all

  tasks:

    – name: Install package

      yum:

        name: httpd

        state: present

      ignore_errors: yes

    – block:

        – name: Risky task

          command: /bin/false

      rescue:

        – name: Recovery action

          debug:

            msg: “Task failed, executing rollback”


๐Ÿ”น 5. Performance Optimization

๐Ÿ‘‰ Use async + poll for long tasks

– hosts: all

  tasks:

    – name: Run long job in parallel

      shell: /usr/bin/long_script.sh

      async: 600

      poll: 0


๐Ÿ”น 6. Dynamic Inventory for AWS

๐Ÿ‘‰ Use aws_ec2 plugin

# inventory/aws_ec2.yml

plugin: aws_ec2

regions:

  – ap-south-1

filters:

  instance-state-name: running

keyed_groups:

  – key: tags.Environment

    prefix: env

ansible-inventory -i inventory/aws_ec2.yml –list


๐Ÿ”น 7. Enforce Compliance (OpenSSL version check)

– hosts: all

  tasks:

    – name: Get OpenSSL version

      command: openssl version

      register: openssl_ver

    – name: Fail if wrong version

      fail:

        msg: “OpenSSL not up to date!”

      when: “‘OpenSSL 1.1.1’ not in openssl_ver.stdout”


๐Ÿ”น 8. Distribute Large WAR File Efficiently

๐Ÿ‘‰ Use delegate_to + rsync

– hosts: deploy-master

  tasks:

    – name: Copy WAR to master node

      copy:

        src: myapp.war

        dest: /tmp/myapp.war

– hosts: appservers

  tasks:

    – name: Sync WAR from master

      synchronize:

        src: /tmp/myapp.war

        dest: /opt/tomcat/webapps/

      delegate_to: deploy-master


๐Ÿ”น 9. Rollback on Failure

๐Ÿ‘‰ Keep backup before deployment

– hosts: webservers

  tasks:

    – name: Backup old config

      copy:

        src: /etc/myapp/config.yml

        dest: /etc/myapp/config.yml.bak

        remote_src: yes

    – block:

        – name: Deploy new config

          copy:

            src: config.yml

            dest: /etc/myapp/config.yml

      rescue:

        – name: Rollback config

          copy:

            src: /etc/myapp/config.yml.bak

            dest: /etc/myapp/config.yml

            remote_src: yes


๐Ÿ”น 10. Delegate Tasks (Central Log Collection)

๐Ÿ‘‰ Use delegate_to + run_once

– hosts: all

  tasks:

    – name: Collect logs

      fetch:

        src: /var/log/app.log

        dest: logs/{{ inventory_hostname }}/

        flat: yes

    – name: Compress logs

      archive:

        path: logs/

        dest: logs/all-logs.tar.gz

      run_once: yes

      delegate_to: log-server

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Azure DevOps Key Benefits: Advanced Interview Questions: advanced DevOps SME interview questions: These questions cover key aspects of DevSecOps, including...
Building a career in AWS, Azure, and DevOps requires a blend of technical skills, certifications, practical experience, and strategic learning....
The roles of DevOps Engineer and Site Reliability Engineer (SRE) are closely related, but they have distinct focuses and responsibilities....