Ansible Truth Table
Everything in an Ansible playbook is first YAML, then it's Ansible.
Rules
Values specified in YAML go through the following stages:
-
Parse as YAML as specified by YAML syntax;
-
true
,True
,TRUE
,false
,False
,FALSE
-> boolean -
tRUE
,TrUe
,fALSE
,FaLsE
, … -> string -
"true"
,"True"
,"TRUE"
,"false"
,"False"
,"FALSE"
, … -> string -
yes
,Yes
,YES
,no
,No
,NO
-> boolean -
yES
,YeS
,nO
, … -> string -
"yes"
,"Yes"
,"YES"
,"no"
,"No"
,"NO"
, … -> string -
on
,On
,ON
,off
,Off
,OFF
-> boolean -
oN
,oFF
,oFf
, … -> string -
"on"
,"On"
,"ON"
,"off"
,"Off"
,"OFF"
, … -> string -
y
,Y
,n
,N
-> string -
"y"
,"Y"
,"n"
,"N"
-> string -
0
,1
-> number -
"0"
,"1"
-> string
-
-
Parse as Ansible syntax.
This step converts the following string (case insensitively) to boolean:
"true"
"false"
"yes"
"no"
-
-
If variable goes through bool filter, it additionaly converts the following string (case-insensitively) and numbers to boolean:
-
"on"
-> boolean True -
"off"
-> boolean False -
"1"
-> boolean True -
"0"
-> boolean False -
Any other string -> boolean False
-
1
-> boolean True -
0
-> boolean False - Any other number -> boolean False
-
-
If variable is prefixed with
not
operator:-
all non empty string is treated as
True
, withnot
operator, the result becomesFalse
; -
0 treated as
False
, 1 and any other number asTrue
; withnot
operator, they becomeTrue
andFalse
respectively.
-
-
Truth Table
The following is a truth table for the rules above:
var |
{{ var }} |
{{ var|bool }} |
{{ not var }} |
1 | 1 | true | false |
"1" | "1" | true | false |
true, True, TRUE | true | true | false |
"true", "True", "TRUE", ... | true | true | false |
yes, Yes, YES | true | true | false |
"yes", "Yes", "YES", ... | true | true | false |
on, On, ON | true | true | false |
"on", "On", "ON", ... | "on", "On", "ON", ... | true | false |
y, Y | "y", "Y" | false | false |
"y", "Y" | "y", "Y" | false | false |
"True\n" | "True\n" | false | false |
var |
{{ var }} |
{{ var|bool }} |
{{ not var }} |
0 | 0 | false | true |
"0" | "0" | false | false |
false, False, FALSE | false | false | true |
"false", "False", "FALSE", ... | false | false | true |
no, No, NO | false | false | true |
"no", "No", "NO", ... | false | false | true |
off, Off, OFF | false | false | true |
"off", "Off", "OFF", ... | "off", "Off", "OFF", ... | false | false |
n, N | "n", "N" | false | false |
"n", "N" | "n", "N" | false | false |
"False\n" | "False\n" | false | false |
var |
{{ var }} |
{{ var|bool }} |
{{ not var }} |
Test Playbook
Test it yourself with the following ansible playbook:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
t0: 1
t1: "1"
t2: true
t3: "true"
t4: yes
t5: "yes"
t6: on
t7: "on"
t8: y
t9: "y"
f0: 0
f1: "0"
f2: false
f3: "false"
f4: no
f5: "no"
f6: off
f7: "off"
f8: n
f9: "n"
- name: "var"
debug: var=item
loop: "{{ [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9] }}"
- name: "{{ var|bool }}"
debug: msg={{ item|bool }}
loop: "{{ [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9] }}"
- name: "{{ not var }}"
debug: msg={{ not item }}
loop: "{{ [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9] }}"