4

I want to create a double loop in ansible. I have one things like this :

userslist:
  - name: user1
    primary : user1-group
    groups :
       - group1
       - group2
  - name: user2
    primary : user2-group
    groups :
       - group3
       - group4

- name : Creating Secondary group
  group :
    name : "{{ item.groups }}"
    state: present
  with_items: "{{ userslist}}"

Is it possible for each users create each secondary group ? I think for this i need to do a second with_items loop but i don't know how

p305
  • 125
  • 1
  • 1
  • 14

2 Answers2

8

There are two ways to make a nested (double) loop in Ansible.

  • with_nested. It allows you to have an inner iteration for object you iterate in the outer loop. Examples and explanation are provided in the official documentation: https://docs.ansible.com/ansible/2.5/plugins/lookup/nested.html

  • using with_items together with include_tasks. This is a complicated yet powerful construction. In theory there is no limit (except for the stack depth) on how nested this construction can be.

It requires to put inner code into a separate tasklist. I'll show it with outer.yaml and inner.yaml, outer perform loop over a list, and inner perform a loop over item (loop variable) of the outer loop.

outer.yaml:

- name: Loop over foo
  include_tasks: inner.yaml
  with_items: '{{ foo }}'
  loop_control:
     loop_var: inner_var_name
  vars:
   foo:
    - [1, 2, 3]
    - [a, b, c]

inner.yaml:

- name: Performing operation one
  debug: msg="Action one for {{ item }}"
  with_items: '{{ inner_var_name }}'

- name: Performing operation two
  debug: msg="Action two for {{item}}"
  with_items: '{{ inner_var_name }}'

The key advantage of this method is that inner.yaml can contain any number of statements, all of which will be processed in a loop from outer.yaml.

One important thing: all include things require a bit of caution with anything related to passing values (set_fact, register, etc) from included code. In is rather tricky and non-obvious, so my advice is never use variables set in include code outside of that inclusion.

George Shuklin
  • 6,952
  • 10
  • 39
  • 80
  • In my exemple the user1 hasn't the same groups that user2 with the nested loop i can only create users with the same groups right ? It's the same with the include_tasks no ? – p305 Nov 02 '18 at 11:22
  • It's up to you. You can use the same group, or different group. If you want, you can give users some 'groups' variable with list of groups they belong to, or you can have groups with users belonging to them. If you want to filter some users, there is a `when` statement. – George Shuklin Nov 02 '18 at 13:02
  • I have tested a lot of syntax but i can't do what i want :( Can you write a little example please ? – p305 Nov 02 '18 at 14:33
  • But I've provided examples for both cases. Aren't they clear enough, are they? – George Shuklin Nov 03 '18 at 14:32
4

I do this and it's work very well

---

- hosts: all
  become: yes
  vars:
    userslist:
      - name: user1
        primary : user1-group
        groups :
           - group1
           - group2
      - name: user2
        primary : user2-group
        groups :
           - group3
           - group4

  tasks:
  - name: Creating Secondary group
    group:
      name="{{ item.1 }}"
    with_subelements:
      - '{{ userslist }}'
      - groups

p305
  • 125
  • 1
  • 1
  • 14