summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/no_jinja_when.md
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:06:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:06:49 +0000
commit2fe34b6444502079dc0b84365ce82dbc92de308e (patch)
tree8fedcab52bbbc3db6c5aa909a88a7a7b81685018 /src/ansiblelint/rules/no_jinja_when.md
parentInitial commit. (diff)
downloadansible-lint-2fe34b6444502079dc0b84365ce82dbc92de308e.tar.xz
ansible-lint-2fe34b6444502079dc0b84365ce82dbc92de308e.zip
Adding upstream version 6.17.2.upstream/6.17.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/ansiblelint/rules/no_jinja_when.md')
-rw-r--r--src/ansiblelint/rules/no_jinja_when.md32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/ansiblelint/rules/no_jinja_when.md b/src/ansiblelint/rules/no_jinja_when.md
new file mode 100644
index 0000000..702e807
--- /dev/null
+++ b/src/ansiblelint/rules/no_jinja_when.md
@@ -0,0 +1,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.
+```