O'Reilly logo

Ansible Configuration Management - Second Edition by Daniel Hall

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looping

Ansible allows you to repeat a module several times with different inputs, for example, if you had several files that should have similar permissions set. This can save you a lot of repetition and allows you to iterate over facts and variables.

To do this, you can use the with_items key on an action and set the value to the list of items that you are going to iterate over. This will create a variable for the module named item, which will be set to each item in turn as your module is iterated over. Some modules such as yum will optimize this so that instead of doing a separate transaction for each package, they will operate on all of them at once.

Using with_items, the code looks like this:

tasks: - name: Secure config files file: path: "/etc/{{ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required