Scenario

🔹 Ansible Scenario-Based Questions & Answers

Here are real-world scenario/requirement-based questions with solutions, answered like a Senior DevOps Engineer. These are the types of questions you can expect in a DevOps Consultant interview at Infosys.


1️⃣ Q: You need to copy files from one target server to another using Ansible. How will you do it?

💡 Scenario: The application team needs to transfer a configuration file from server1 to server2 using Ansible.

Solution: As a Senior DevOps Engineer, I would use the fetch and copy modules in Ansible to achieve this in a secure and automated way.

Approach:

  1. Use fetch to get the file from server1.

  2. Use copy to transfer it to server2.

Ansible Playbook:

- name: Copy file from one target to another
  hosts: server1
  tasks:
    - name: Fetch the file from server1
      fetch:
        src: /etc/myapp/config.yaml
        dest: /tmp/config.yaml
        flat: yes  # To avoid creating extra directories

- name: Copy file to server2
  hosts: server2
  tasks:
    - name: Transfer the file to server2
      copy:
        src: /tmp/config.yaml
        dest: /etc/myapp/config.yaml
        owner: root
        group: root
        mode: '0644'

Alternative: Use synchronize (if SSH connectivity exists between servers)

If direct SSH access is available between servers, rsync can be leveraged using Ansible:


2️⃣ Q: How do you ensure only one Ansible playbook execution at a time?

💡 Scenario: You have multiple engineers running playbooks, and you want to avoid concurrent execution to prevent race conditions.

Solution: As a Senior DevOps Engineer, I would use file-based locking with the flock command to ensure only one execution at a time.

Approach:

  1. Use a lock file to prevent parallel runs.

  2. Ensure that if another execution is ongoing, the new one waits.

Ansible Playbook:

  • If another playbook is running, it will exit immediately instead of running in parallel.


3️⃣ Q: How do you run a playbook only if a specific service is running?

💡 Scenario: You need to execute a playbook only if Nginx is running on the target server.

Solution: As a Senior DevOps Engineer, I would use conditionals and service facts to check if the service is active before running the playbook.

Ansible Playbook:

  • If Nginx is running, it restarts.

  • If not, it does nothing, avoiding unnecessary actions.


4️⃣ Q: You need to rollback an Ansible deployment. How would you handle it?

💡 Scenario: A deployment broke production. You need a rollback strategy with Ansible.

Solution: As a Senior DevOps Engineer, I would ensure:

  1. Backup current state before deployment.

  2. Rollback to the last stable version if an issue occurs.

Ansible Playbook with Rollback Strategy:

  • If deployment fails, Ansible automatically rolls back to the last backup.


5️⃣ Q: How do you manage secrets in Ansible securely?

💡 Scenario: You need to store and use API keys/passwords securely in Ansible playbooks.

Solution: As a Senior DevOps Engineer, I would use Ansible Vault to encrypt sensitive data.

Steps to Secure Secrets:

  1. Encrypt a file using Ansible Vault:

  2. Use the encrypted file in playbooks:

  3. Decrypt when running the playbook:

  • This ensures sensitive data is never stored in plain text.


6️⃣ Q: How do you ensure idempotency in Ansible playbooks?

💡 Scenario: A junior engineer wrote an Ansible playbook that keeps making changes even when they aren’t needed.

Solution: As a Senior DevOps Engineer, I ensure idempotency using:

  • State parameters (state: present)

  • changed_when conditions

  • Use stat module before making changes

Example: Installing Nginx (Idempotent Approach)

  • If Nginx is already installed, it does nothing.

  • If not installed, it installs it.


🔹 Key Takeaways to Crack Your Infosys DevOps Consultant Interview

Think like a Senior DevOps Engineer – focus on automation, efficiency, and security. ✅ Use best practices – idempotency, rollback strategies, secure secrets. ✅ Answer with structured solutionsApproach → Playbook → Explanation. ✅ Be ready for scenario-based problem-solving questions.

🔥 Need more scenarios? Let me know, and I'll add them! 🚀

🔹 More Ansible Scenario-Based Interview Questions & Answers (Short & Precise)


1️⃣ Q: How do you handle dynamic inventories in Ansible?

Use an inventory script or cloud provider plugin like AWS EC2 dynamic inventory:

  • Define aws_ec2.yml with the AWS plugin for dynamic inventory fetching.


2️⃣ Q: How do you run only specific tasks in a playbook?

✅ Use tags and run with --tags or --skip-tags:

Run only install tasks:


3️⃣ Q: How do you execute a playbook in a dry run mode?

✅ Use the --check flag to simulate execution without making changes:


4️⃣ Q: How do you handle errors in Ansible?

✅ Use ignore_errors: yes or failed_when:

✅ Use rescue for rollback:


5️⃣ Q: How do you pass variables to an Ansible playbook?

✅ Pass variables via CLI, inventory, or a file:

Inside playbook:


6️⃣ Q: How do you run an Ansible task as a different user?

✅ Use become_user:


7️⃣ Q: How do you handle dependencies between tasks?

✅ Use when conditions or handlers:


8️⃣ Q: How do you fetch logs from multiple servers using Ansible?

✅ Use the fetch module:


9️⃣ Q: How do you make Ansible run in parallel across multiple servers?

✅ Use forks in ansible.cfg:

Or set it dynamically:


🔟 Q: How do you check if a file exists before performing an action?

✅ Use the stat module:


1️⃣1️⃣ Q: How do you limit execution to a single server in an inventory group?

✅ Use --limit flag:

This runs the playbook only on web1, ignoring other servers.


1️⃣2️⃣ Q: How do you remove sensitive data from Ansible output?

✅ Use no_log: yes:


1️⃣3️⃣ Q: How do you troubleshoot when Ansible can't reach a target server?

Check connectivity manually:

Ensure Ansible can reach the server:

Verify SSH access:

  • Make sure port 22 is open

  • Add Ansible server's public key to target's ~/.ssh/authorized_keys


1️⃣4️⃣ Q: How do you run an Ansible playbook only on a specific OS?

✅ Use ansible_facts:


1️⃣5️⃣ Q: How do you ensure a task runs only once in a multi-node setup?

✅ Use run_once:


1️⃣6️⃣ Q: How do you handle large file transfers in Ansible?

✅ Use the synchronize module (rsync-based) for efficiency:


1️⃣7️⃣ Q: How do you reboot a server and wait for it to come back up?

✅ Use reboot with wait_for_connection:


1️⃣8️⃣ Q: How do you execute only specific tasks from a playbook?

✅ Use --start-at-task:


1️⃣9️⃣ Q: How do you execute a playbook on a subset of inventory?

✅ Use --limit to restrict execution:


2️⃣0️⃣ Q: How do you update all packages on a server using Ansible?

✅ Use package module:


🔥 Final Tips for Your Infosys DevOps Consultant Interview

Master Troubleshooting – Be ready to debug connectivity, SSH, playbook failures. ✅ Know Real-World Scenarios – Be prepared to discuss Ansible best practices. ✅ Practice Writing Playbooks – They might ask you to write or modify one in the interview. ✅ Stay Confident! – Answer like a Senior DevOps Engineer with structured, real-world solutions.

🚀 Need more questions or detailed answers? Let me know! 🚀

Last updated