Repository: ansible/ansible
Pull request: Fix issue with empty cow_whitelist
##### SUMMARY
Fix issue with empty cow_whitelist. Fixes #45631

##### ISSUE TYPE
- Bugfix Pull Request

##### COMPONENT NAME
lib/ansible/utils/display.py

##### ADDITIONAL INFORMATION
<!--- Include additional information to help people understand the change here -->
<!--- A step-by-step reproduction of the problem is helpful if there is no related issue -->

<!--- Paste verbatim command output below, e.g. before and after your change -->
```paste below

```

Problem statement:
An empty cow_whitelist while cow_selection is random causes no cows to be selected
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
When using random cow selection, and custom cows, the random selection of cows does not include new cows, and produces an error.

In previous versions, an empty `cow_whitelist` caused any cow to be used instead of no cows to be used.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below -->
lib/ansible/utils/display.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.6.4
  config file = /ansible/ansible.cfg
  configured module search path = ['/ansible/library', '/usr/share/ansible']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.5 (default, May  5 2018, 03:09:35) [GCC 4.9.2]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
ANSIBLE_COW_SELECTION(/ansible/ansible.cfg) = random
ANSIBLE_COW_WHITELIST(/ansible/ansible.cfg) = ['']
```

In addition the `ansible.cfg` file is as follows:
```
cow_selection = random
cow_whitelist =
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
Python docker container, on debian base image

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

Set the `cow_selection` and `cow_whitelist` accordingly. Make any play.

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->

To be delighted and entranced by the display of unique pictographs illuminating my screen in a spontaneous and random fashion.

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

Boring display and errors that just tell me things are ok when they are not.

<!--- Paste verbatim command output between quotes -->
```paste below
 [WARNING]: Failure using method (v2_playbook_on_play_start) in callback plugin
(<ansible.plugins.callback.skippy.CallbackModule object at 0x7ff2230aa860>): Cannot choose from an empty
sequence

 [WARNING]: Failure using method (v2_playbook_on_task_start) in callback plugin
(<ansible.plugins.callback.skippy.CallbackModule object at 0x7ff2230aa860>): Cannot choose from an empty
sequence

included: /ansible/example_play.yml for 127.0.0.1
ok: [127.0.0.1]
ok: [127.0.0.1]
ok: [127.0.0.1]
 [WARNING]: Failure using method (v2_playbook_on_stats) in callback plugin
(<ansible.plugins.callback.skippy.CallbackModule object at 0x7ff2230aa860>): Cannot choose from an empty
sequence
```

Hints from issue discussion:
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
!component lib/ansible/utils/display.py
Files identified in the description:
* [lib/ansible/utils/display.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/utils/display.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Commit message: Fix issue with empty cow_whitelist. Fixes #45631

Patch to review:
diff --git a/changelogs/fragments/45631-empty-cow-whitelist.yml b/changelogs/fragments/45631-empty-cow-whitelist.yml
new file mode 100644
index 00000000000000..dd556c63253b94
--- /dev/null
+++ b/changelogs/fragments/45631-empty-cow-whitelist.yml
@@ -0,0 +1,2 @@
+bugfixes:
+- cowsay - Fix issue with an empty cow_whitelist (https://github.com/ansible/ansible/issues/45631)
diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py
index 6735cf66f45b8f..c9db5446071f34 100644
--- a/lib/ansible/utils/display.py
+++ b/lib/ansible/utils/display.py
@@ -110,7 +110,7 @@ def __init__(self, verbosity=0):
                 cmd = subprocess.Popen([self.b_cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                 (out, err) = cmd.communicate()
                 self.cows_available = set([to_text(c) for c in out.split()])
-                if C.ANSIBLE_COW_WHITELIST:
+                if any(C.ANSIBLE_COW_WHITELIST):
                     self.cows_available = set(C.ANSIBLE_COW_WHITELIST).intersection(self.cows_available)
             except Exception:
                 # could not execute cowsay for some reason


Code review comments:
diff_hunk: @@ -110,7 +110,7 @@ def __init__(self, verbosity=0):
                 cmd = subprocess.Popen([self.b_cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                 (out, err) = cmd.communicate()
                 self.cows_available = set([to_text(c) for c in out.split()])
-                if C.ANSIBLE_COW_WHITELIST:
+                if any(C.ANSIBLE_COW_WHITELIST):
path: lib/ansible/utils/display.py
text: @user1:
shouldn't we keep the previous check also (make it an `and`) to avoid `None` values instead of list or list of empty items?

@author:
Probably

@author:
Done.