# Loops in Ansible

**URL:** <https://forum.ansible.com/t/loops-in-ansible/23975>\
**Category:** Ansible Project\
**Created:** [March 15, 2017, 6:33am UTC](https://forum.ansible.com/t/loops-in-ansible/23975 "2017-03-15T06:33:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![user20](https://avatars.discourse-cdn.com/v4/letter/u/a587f6/32.png) [@user20](https://forum.ansible.com/u/user20)\
**Post date:** [March 15, 2017, 6:33am UTC](https://forum.ansible.com/t/loops-in-ansible/23975/1 "2017-03-15T06:33:51Z")

</div>

Hi, everyone.

Would it be possible to somehow merge those two tasks in one? As you can see, the only difference is in **default: [yes|no]** key.  
I have to install both default and non-default ACL on a filesystem object, but I am am stuck with (perceived) deficiencies of Ansible loops. Can I somehow alternate between those two boolean values, whilst also being able to loop _with\_subelements_?

`  
vars:  
deploy\_username: deploy-a  
directories:

- path: /var/www  
owner: www-data  
group: www-data  
permissions: “0770”  
recursive\_perms: yes  
acl:

- etype: user  
permissions: rwX  
entity: www-data

- etype: user  
permissions: rwX  
entity: “{{ deploy\_username }}”

- name: Set ACL on directories  
acl:  
path: “{{ item.0.path }}”  
entity: “{{ item.1.entity }}”  
etype: “{{ item.1.etype }}”  
permissions: “{{ item.1.permissions }}”  
state: present  
default: no  
recursive: “{{ item.0.recursive\_perms }}”  
with\_subelements:

- “{{ directories }}”

- acl

- name: Set default ACL on directories  
acl:  
path: “{{ item.0.path }}”  
entity: “{{ item.1.entity }}”  
etype: “{{ item.1.etype }}”  
permissions: “{{ item.1.permissions }}”  
state: present  
default: yes  
recursive: “{{ item.0.recursive\_perms }}”  
with\_subelements:

- “{{ directories }}”

- acl

`

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 30, 2018, 7:16pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/2 "2018-01-30T19:16:29Z")

</div>

I’m trying to do the same thing. What version of Ansible are you using? I use v2.4.2.0 and the acl module does NOT have a recursive parameter, but I see you use it?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 30, 2018, 7:23pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/3 "2018-01-30T19:23:35Z")

</div>

> I'm trying to do the same thing. What version of Ansible are you using? I  
> use v2.4.2.0 and the acl module does NOT have a recursive parameter, but I  
> see you use it?

Where to you get that from?

> From the docs [https://docs.ansible.com/ansible/latest/acl\_module.html#options](https://docs.ansible.com/ansible/latest/acl_module.html#options)

recursive (added in 2.0) - Recursively sets the specified ACL (added in Ansible 2.0). Incompatible with state=query.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 30, 2018, 7:25pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/4 "2018-01-30T19:25:19Z")

</div>

I was looking here [http://ansible-manual.readthedocs.io/en/latest/acl\_module.html](http://ansible-manual.readthedocs.io/en/latest/acl_module.html)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 30, 2018, 7:27pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/5 "2018-01-30T19:27:55Z")

</div>

I would suggest to use the official documentation and not some random site on the Internet.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 30, 2018, 7:34pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/6 "2018-01-30T19:34:38Z")

</div>

It looked official to me.

(Sorry to hijack the thread)

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [January 30, 2018, 8:26pm UTC](https://forum.ansible.com/t/loops-in-ansible/23975/7 "2018-01-30T20:26:56Z")

</div>

You can try the loop\_control and loop\_var technique discussed in [http://docs.ansible.com/ansible/latest/playbooks\_loops.html#loops-and-includes-in-2-0](http://docs.ansible.com/ansible/latest/playbooks_loops.html#loops-and-includes-in-2-0)

So in one file you may have

`  
vars:  
deploy\_username: deploy-a  
directories:

- path: /var/www  
owner: www-data  
group: www-data  
permissions: “0770”  
recursive\_perms: yes  
acl:

- etype: user  
permissions: rwX  
entity: www-data

- etype: user  
permissions: rwX  
entity: “{{ deploy\_username }}”

- incolude\_tasks: set-acl.yml  
with\_items:

- yes

- no  
loop\_control:  
loop\_var: default\_bool  
`

The, in set-acl.yml you have your code:

`

- name: Set ACL on directories  
acl:  
path: “{{ item.0.path }}”  
entity: “{{ item.1.entity }}”  
etype: “{{ item.1.etype }}”  
permissions: “{{ item.1.permissions }}”  
state: present  
default: “{{ default\_bool }}”  
recursive: “{{ item.0.recursive\_perms }}”  
with\_subelements:
- “{{ directories }}”
- acl  
`
