Have you ever wondered how life would be if you had to install several packages on your remote systems manually? We saw how Ansible helped us in such a situation by automating the package installation. However, it would be still a nightmare if you have to write the same task multiple times with different package names. To overcome such situations and to make your life easier, Ansible provides you with loops.
Using standard loops, you can pass a list of packages to install and Ansible will run that task for all packages listed. Confused? Let's now go through an example playbook.
In the preceding playbook, Ansible will run the Installing packages
task multiple times (in this case, three times): once for each of the packages that are defined under the with_items
construct. Each of the packages is exposed as item
, which is a default variable that Ansible creates. Ansible then assigns a package name to item
, based on the iteration it is currently part of. So, for...