Check to see if a variable is true

I thought this was going to be an easy thing to accomplish but nothing seems to work. No matter what I do, the role runs. I have a variable set to “true” and I want to run this role only if is_at is false. Here is what I have and what I have tried.

in hosts file…

is_at=True

in playbook file:

role: test
when: not is_at

also tried:

role: test
when: is_at | bool == False

what should the proper syntax be?

I think the variable has to be set to true, all lowercase, no quotes, for it to be used this way.

Playbook:

---
- name: Test true value
  hosts: localhost
  vars:
    new_var: true
  tasks:
    - name: Print "It works" if new_var is true
      ansible.builtin.debug:
        msg: "It works"
      when: new_var

Results:

PLAY [Test true value] ******************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [localhost]

TASK [Print "It works" if new_var is true] **********************************************************************************
ok: [localhost] => 
  msg: It works

PLAY RECAP ******************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Any of those could work. Or not.
You haven’t given us enough context to say either way.
Is this hosts file an .ini inventory?
How are you using the role: keyword in your playbook?
We need more context!

Maybe this example will help.

- hosts: localhost
  connection: local
  tasks:
  
     - name: Do this when true
       ansible.builtin.debug:
         msg: "is true"
       when: myvar | bool
      vars:
          myvar: true

     - name: Skip because not true
       ansible.builtin.debug:
         msg: "skip"
       when: myvar | bool
      vars:
          myvar: false
     
     - name:  Do when false
       ansible.builtin.debug:
         msg: "is false"
       when: not myvar | bool
      vars:
          myvar: false

2 Likes