Repeating tasks with loops
Oftentimes, we will want to perform a single task but use it to iterate over a set of data. Perhaps you are creating new 15 new user groups on a server for various teams to utilize. It would be incredibly inefficient to have to write 15 individual tasks within an Ansible play to achieve this – and Ansible is all about efficiency and saving the user time. To enable this level of efficiency, Ansible supports looping over datasets to ensure that you can perform large-scale operations using tightly defined code. In this section, we will explore how to make practical use of loops in your Ansible playbooks.
As ever, we must start with an inventory to work against, and we will use our by-now familiar inventory, which we have consistently used throughout this chapter:
[frontends] web01.example.org https_port=8443 web02.example.org http_proxy=proxy.example.org [frontends:vars] ntp_server=ntp.web.example.org proxy=proxy.web.example.org [apps] app01.example...