-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |