summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/no_jinja_when.md
blob: 702e80738114f93f38d57f62eb83d339bdb9b5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# no-jinja-when

This rule checks conditional statements for Jinja expressions in curly brackets `{{ }}`.
Ansible processes conditionals statements that use the `when`, `failed_when`, and `changed_when` clauses as Jinja expressions.

An Ansible rule is to always use `{{ }}` except with `when` keys.
Using `{{ }}` in conditionals creates a nested expression, which is an Ansible
anti-pattern and does not produce expected results.

## Problematic Code

```yaml
---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Shut down Debian systems
      ansible.builtin.command: /sbin/shutdown -t now
      when: "{{ ansible_facts['os_family'] == 'Debian' }}" # <- Nests a Jinja expression in a conditional statement.
```

## Correct Code

```yaml
---
- name: Example playbook
  hosts: localhost
  tasks:
    - name: Shut down Debian systems
      ansible.builtin.command: /sbin/shutdown -t now
      when: ansible_facts['os_family'] == "Debian" # <- Uses facts in a conditional statement.
```