DevOps / Ansible Interview questions
When should you use loops instead of duplicating tasks?
Whenever the same module is applied repeatedly with only the argument values changing, a loop keeps the playbook shorter and easier to maintain than writing out a near-identical task for each item.
- name: Install several packages ansible.builtin.package: name: "{{ item }}" state: present loop: - nginx - git - curl
This is the right tool when the list of items is either fixed and short, or comes from a variable (including one populated dynamically from facts or a registered result), and each iteration performs conceptually the same action. Duplicating the task manually instead means every future change (adding a package, adjusting an option) has to be repeated in multiple places, which is exactly the kind of copy-paste maintenance burden loops exist to eliminate. For more complex iteration, such as looping over a list of dictionaries with multiple related values, loop with structured data or the older with_items/with_dict family handles that cleanly as well.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
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.
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! Receive free stock by signing up using the link: Webull signup.
More Related questions...
