OR logic state in when: statement Ansible

49 views Asked by At

I'm creating a playbook that generates and set a random password for GRUB.

I want the playbook to be skipped when an extra-var is entered OR when there is already a password.

    - name: Merge variable set_grubpassword__skip_jobs
      ansible.builtin.set_fact:
        set_grubpassword_skip_jobs: "{{ lookup('community.general.merge_variables', '__skip_jobs', pattern_type='suffix') }}"

    - name: Check if a GRUB password exists
      ansible.builtin.lineinfile:
        dest: /etc/grub.d/00_header
        line: 'set superusers="root"'
        state: present
      check_mode: true
      register: password_dontexist

  tasks:
    - name: Set GRUBPassword
      block:
    [...]
      when: "'set_grubpassword' not in set_grubpassword_skip_jobs" or password_dontexist.changed

When I set only of the 2 conditions (no matter which one), the tasks part get skipped correctly, but when i set the 2 conditions separated by "or", the playbook still works but the skip won't work, even if I launch with the extra-var or if there is already a password in.

Any ideas ?

Thanks!

Different syntaxes I tried :

when: "'set_grubpassword' not in set_grubpassword_skip_jobs" or password_dontexist.changed when: ("'set_grubpassword' not in set_grubpassword_skip_jobs") or (password_dontexist.changed) when: (("'set_grubpassword' not in set_grubpassword_skip_jobs") or (password_dontexist.changed)) when: ("'set_grubpassword' not in set_grubpassword_skip_jobs") or password_dontexist.changed

and maybe others I can't tell, in any case it does the same.

1

There are 1 answers

2
Alexander Pletnev On

Wrap the whole condition in the double quotes. You need them to escape the leading single quote:

when: "'set_grubpassword' not in set_grubpassword_skip_jobs or password_dontexist.changed"

Or don't wrap it at all (but you'll have to move the single-quoted condition from the leading position):

when: password_dontexist.changed or 'set_grubpassword' not in set_grubpassword_skip_jobs