Loops in Ansible

56 views
Skip to first unread message

Андрей Климентьев

unread,
Mar 15, 2017, 9:50:31 AM3/15/17
to Ansible Project
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






ZillaYT

unread,
Jan 30, 2018, 2:16:29 PM1/30/18
to Ansible Project
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?

Kai Stian Olstad

unread,
Jan 30, 2018, 2:23:53 PM1/30/18
to ansible...@googlegroups.com
On Tuesday, 30 January 2018 20.16.29 CET ZillaYT wrote:
> 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

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


--
Kai Stian Olstad

ZillaYT

unread,
Jan 30, 2018, 2:25:19 PM1/30/18
to Ansible Project

Kai Stian Olstad

unread,
Jan 30, 2018, 2:28:27 PM1/30/18
to ansible...@googlegroups.com
On Tuesday, 30 January 2018 20.25.19 CET ZillaYT wrote:
> I was looking
> here http://ansible-manual.readthedocs.io/en/latest/acl_module.html

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

--
Kai Stian Olstad

ZillaYT

unread,
Jan 30, 2018, 2:34:38 PM1/30/18
to Ansible Project
It looked official to me.

(Sorry to hijack the thread)

ZillaYT

unread,
Jan 30, 2018, 3:26:56 PM1/30/18
to Ansible Project
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

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
On Wednesday, March 15, 2017 at 9:50:31 AM UTC-4, Андрей Климентьев wrote:
Reply all
Reply to author
Forward
0 new messages