Prev Next

DevOps / Ansible Interview questions

Explain the lifecycle of a task in an Ansible play?

A single task moves through templating, module dispatch, execution, and result handling before Ansible considers it complete for a given host.

flowchart LR A[Task defined in play/role] --> B[Evaluate when: condition] B -- False --> C[Mark skipped] B -- True --> D[Template module arguments with Jinja2] D --> E[Dispatch module to host, respecting become/delegate_to] E --> F[Module executes idempotent check-then-act logic] F --> G[Module returns changed/failed/ok + result data] G --> H{register set?} H -- Yes --> I[Store result in variable] H -- No --> J[Discard raw result] G --> K{notify set and changed?} K -- Yes --> L[Queue handler for end of play]

Before anything runs, Ansible evaluates the task's when condition (if any); if false, the task is marked skipped and nothing further happens for that host. Otherwise, module arguments are templated with Jinja2 using currently known variables and facts, and the module is dispatched to the host - respecting any become or delegate_to directives that change who executes it or where. The module runs its check-then-act logic and returns a result; if register is set, that result is stored in a variable for later tasks to reference, and if the task both notify-ed a handler and reported changed, that handler is queued to run at the end of the play. This entire sequence happens independently, per host, which is why a task can succeed on one host and fail on another within the same play.

What happens if a task's when condition evaluates to false?
What two conditions must both be true for a task to queue a handler?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Robinhood Logo

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Ansible? What are the key features of Ansible? What is a playbook in Ansible? Define an inventory file in Ansible? What is a module in Ansible? What are Ansible facts? Describe roles in Ansible? What are the types of variables in Ansible? List the components of an Ansible role directory? How do you use handlers in Ansible? What is Ansible Vault? Explain the purpose of tags in Ansible playbooks? What is ansible.cfg? How do you apply become for privilege escalation in Ansible? What is Ansible Galaxy? Why doesn't Ansible require agents on managed nodes? How does Ansible ensure idempotency in playbook runs? What is the difference between include and import in Ansible? When should you use loops instead of duplicating tasks? What happens when a task fails in an Ansible playbook? How is variable precedence resolved in Ansible? Why should you avoid hardcoding secrets in playbooks? What is the difference between Ansible and Chef/Puppet? How does Ansible handle privilege escalation with become? When would you choose dynamic inventory over static inventory? How can you optimize playbook execution speed? What is the difference between copy and template modules? Why do we use handlers instead of regular tasks for restarts? How does Ansible's Jinja2 templating work in playbooks? What is the difference between roles and collections? When should you use blocks with rescue in Ansible? How is check mode implemented in Ansible? Why doesn't Ansible require a central server by default? What is the difference between ansible-playbook and ansible ad-hoc commands? How do you troubleshoot a failing Ansible task? Explain the internal working of Ansible's execution model? Explain the execution flow of an Ansible playbook run? Explain the lifecycle of a task in an Ansible play? How does Ansible guarantee idempotent module behavior internally? What happens internally when Ansible gathers facts? How can you optimize a large-scale Ansible deployment for performance? Which is better and why: linear or free strategy for a large inventory? How does forking improve execution speed in Ansible? Why is idempotency critical to Ansible's module design? How do you troubleshoot slow fact gathering in Ansible? Explain the internal working of Ansible's variable precedence resolution? What happens when a handler is notified multiple times in a play? How does Ansible implement fact caching internally? Explain the execution flow of a rolling update using serial? How can you optimize role design to avoid duplicate work at scale?
Show more question and Answers...

Testing

Comments & Discussions