Newbie variables problem while upgrading from 1.9 to 2.x

In the snippet below, if state is absent, then obviously permissions can’t be set on something that is not present. My objective is to run “permissions” only when state is present; else, do nothing.

acl:
name={{secrets_dir}}/{{item.0.file}}
state=“{{‘present’ if item.0.pods is not defined or machine_pod in item.0.pods else ‘absent’}}”
entity=“{{item.1.entity}}”
etype=“{{item.1.etype}}”
permissions=r
default=no
with_subelements:

  • “{{ secrets }}”
  • ACL

When I run this task, I get the message which is helpful, but I have been unable to use if-else or omit to solve this problem.

“msg”: “‘permissions’ MUST NOT be set when ‘state=absent’.”

Thanks !

Answering my own question:

In Ansible 2.x, when state is absent, the permissions field can’t be set. The way out here is to have two clauses (set ACL, remove ACL) each with a “when” like:

name: task(set ACLs)


when item.0.pods is not defined

name: task(remove ACLs)


when not(item.0.pods is not defined)