Select Page

Ansible – Loop over multiple tasks

by | Nov 20, 2020 | Ansible, Automation, NETWAYS, Python

ansible logo

The last time I wrote about Ansible and the possibility to use blocks to group multiple tasks. Which you can read here. Sadly this feature does not work with loop, so there is no clean way to loop over multiple tasks in a play without writing the same loop statement at tasks over and over.

But when we come across the need of tasks which depend on each other, for example, we execute a script with a certain parameter and its result is necessary for the upcoming tasks.

Let’s go through a common example, creating a site consists of a few steps. Creating the directory, creating the vhost and then enabling the site.


- name: "create {{ site }} directory"
  file:
    ensure: directory
    dest: "/var/www/{{ site }}"
    
- name: "create {{ site }}"
  template:
    src: vhost.j2
    dest: "/etc/apache2/sites-available/{{ site }}"
  register: vhost

- name: "enable {{ site }}"
  command: /usr/sbin/a2ensite "{{ site }}"
  register: result
  when: vhost.changed
  changed_when: "'Enabling site' in result.stdout"
  notify: apache_reload

We could use a loop for each tasks and afterwards find the right result for the next task to depend on. But the styleguide will warn you if you try to use Jinja2 syntax in when statements.

So the best solution to this is to use include_tasks, which can include a file with tasks. This task is allowed to have a loop directive and so we can include it multiple times.
Lets see how this would apply to our scenario:


- set_fact:
    sites:
      - default
      - icingaweb2

- name: create vhosts
  include_tasks: create-vhosts.yml
  loop: "{{ sites }}"
  loop_control:
    loop_var: site


In the Result we can see clearly that all tasks are applied for each element in the sites variable.


TASK [set_fact] *********************************************
ok: [localhost]

TASK [create vhosts] ****************************************
included: /Users/twening/Documents/netways/ansible_test20/create-vhosts.yml for localhost => (item=default)
included: /Users/twening/Documents/netways/ansible_test20/create-vhosts.yml for localhost => (item=icingaweb2)

TASK [create default directory] *****************************
ok: [localhost]

TASK [create default] ***************************************
ok: [localhost]

TASK [enable default] ***************************************
ok: [localhost]

TASK [create icingaweb2 directory] **************************
ok: [localhost]

TASK [create icingaweb2] ************************************
ok: [localhost]

TASK [enable icingaweb2] ************************************
ok: [localhost]

PLAY RECAP **************************************************
localhost                  : ok=10   changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Check out our Blog for more awesome posts and if you need help with Ansible send us a message or sign up for one of our trainings!

Thilo Wening
Thilo Wening
Consultant

Thilo hat bei NETWAYS mit der Ausbildung zum Fachinformatiker, Schwerpunkt Systemadministration begonnen und unterstützt nun nach erfolgreich bestandener Prüfung tatkräftig die Kollegen im Consulting. In seiner Freizeit ist er athletisch in der Senkrechten unterwegs und stählt seine Muskeln beim Bouldern. Als richtiger Profi macht er das natürlich am liebsten in der Natur und geht nur noch in Ausnahmefällen in die Kletterhalle.
More posts on the topicAnsible | Automation | NETWAYS | Python

Monthly Snap March 2021

March is over and we are officially in spring mode! You can actually tell by reading our blogs: The NETWAYS Shop and NWS both have had a spring cleaning in the shape of make-overs!   NWS- NETWAYS Web Services   Our NWS customer center now has a fresh new...

Warum uns eine entfremdete Gesellschaft schadet?

Wenngleich uns die aktuellen Umstände aufgrund der Corona-Pandemie zu einer gewissen Isolation und Einschränkung von Kontakten zwingen, wurden viele bereits laufenden Prozesse dadurch lediglich beschleunigt. Jede Gesellschaft befindet sich in einem ständigen Wandel...

Was nehme ich aus meiner Ausbildung mit?

Unser drittes Lehrjahr (mich eingeschlossen) steht nach drei interessanten und lehrreichen Jahren kurz vor den Abschlussprüfungen. In wenigen Wochen geht es dann darum, dass erlernte Wissen in der Abschlussprüfung korrekt wieder zugeben. Neben dem schriftlichen Teil...

Veranstaltungen

Tue 20

Icinga 2 Advanced Training | Online

April 20 @ 09:00 - April 22 @ 17:00
Tue 20

InfluxDB & Grafana | Online

April 20 @ 09:00 - April 21 @ 17:00
Tue 27

Elastic Stack Training | Online

April 27 @ 09:00 - April 29 @ 17:00
Tue 27

Graylog Training | Online

April 27 @ 09:00 - April 28 @ 17:00
NETWAYS Headquarter | Nürnberg
May 04

GitLab Fundamentals Training | Online

May 4 @ 09:00 - May 5 @ 17:00