summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/empty_string_compare.md
diff options
context:
space:
mode:
Diffstat (limited to 'src/ansiblelint/rules/empty_string_compare.md')
-rw-r--r--src/ansiblelint/rules/empty_string_compare.md44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/ansiblelint/rules/empty_string_compare.md b/src/ansiblelint/rules/empty_string_compare.md
new file mode 100644
index 0000000..c20bc51
--- /dev/null
+++ b/src/ansiblelint/rules/empty_string_compare.md
@@ -0,0 +1,44 @@
+# empty-string-compare
+
+This rule checks for empty string comparison in playbooks.
+To ensure code clarity you should avoid using empty strings in conditional statements with the `when` clause.
+
+- Use `when: var | length > 0` instead of `when: var != ""`.
+- Use `when: var | length == 0` instead of `when: var == ""`.
+
+This is an opt-in rule.
+You must enable it in your Ansible-lint configuration as follows:
+
+```yaml
+enable_list:
+ - empty-string-compare
+```
+
+## Problematic Code
+
+```yaml
+---
+- name: Example playbook
+ hosts: all
+ tasks:
+ - name: Shut down
+ ansible.builtin.command: /sbin/shutdown -t now
+ when: ansible_os_family == "" # <- Compares with an empty string.
+ - name: Shut down
+ ansible.builtin.command: /sbin/shutdown -t now
+ when: ansible_os_family !="" # <- Compares with an empty string.
+```
+
+## Correct Code
+
+```yaml
+---
+- name: Example playbook
+ hosts: all
+ tasks:
+ - name: Shut down
+ ansible.builtin.shell: |
+ /sbin/shutdown -t now
+ echo $var ==
+ when: ansible_os_family
+```