The omnipotent with statement
Iterating plain lists, parsing dictionaries, looping a sequence of numbers, parsing through a path and selectively copying files, or just picking up a random item from a list could be achieved using the "Swiss knife" utility, with
statement. The with
statements take the following form:
with_xxx
Here, the xxx
parameter is the type of data that needs to be looped, for example, items, dictionaries, and so on.
The following table lists the types of data that the with
statement can iterate:
Construct |
Data type |
Description |
---|---|---|
|
Array |
This is used to loop array items. For example, this is used to create a group of users, directories, or to install a list of packages. |
|
Nested loops |
This is used to parse multidimensional arrays. For example, to create a list of MySQL users and grant them access to a group of databases. |
|
Hashes |
This is used to parse a dictionary of key-value pairs and create virtual hosts. |
|