# Check to see if a variable is true

**URL:** <https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022>\
**Category:** Get Help\
**Created:** [July 3, 2024, 5:02pm UTC](https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022 "2024-07-03T17:02:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kathyl](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kathyl](https://forum.ansible.com/u/kathyl)\
**Post date:** [July 3, 2024, 5:02pm UTC](https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022/1 "2024-07-03T17:02:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [July 3, 2024, 5:12pm UTC](https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022/2 "2024-07-03T17:12:31Z")

</div>

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

Playbook:

```auto
---
- 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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 3, 2024, 6:29pm UTC](https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022/3 "2024-07-03T18:29:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [July 5, 2024, 1:27am UTC](https://forum.ansible.com/t/check-to-see-if-a-variable-is-true/7022/4 "2024-07-05T01:27:00Z")

</div>

Maybe this example will help.

```auto
- 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

```
