X99
X99

Reputation: 915

Ansible conditional variable doesn't work

Here is a simple ansible playbook example that makes use of conditionals:

---

- name:
  hosts: web_servers
  gather_facts: false
  tasks:
  - name: test
    ansible.builtin.debug:
      msg: "{{ '-s' if silent_mode==true else '' }}"

I use it with this inventory:

[all:vars]
silent_mode=true

[web_servers]
srv1 ansible_host=1.2.3.4

My issue is the following: whichever the value of silent_mode in the inventory is, the debug message is always empty.

Even weirder: when I only output the silent_mode variable's content, the value is correct:

---

- name:
  hosts: web_servers
  gather_facts: false
  tasks:
  - name: test
    ansible.builtin.debug:
      #msg: "{{ '-s' if silent_mode==true else '' }}"
      msg: "{{ silent_mode }}"

What am I missing?

Upvotes: 0

Views: 69

Answers (1)

X99
X99

Reputation: 915

Ansible conditionals are evaluated using Python, so every boolean must be either True or False (with a capital first letter).

The following works:

[all:vars]
silent_mode=True

[web_servers]
srv1 ansible_host=1.2.3.4
---

- name:
  hosts: web_servers
  gather_facts: false
  tasks:
  - name: test
    ansible.builtin.debug:
      msg: "{{ '-s' if silent_mode==True else '' }}"

Thanks to u/anaumann on r/ansible for the tip.

Upvotes: 1

Related Questions