Repository: ansible/ansible

Issue:
meta: clear_cache throws "coercing to Unicode: need string or buffer, Host found"
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
cache/redis.py

##### ANSIBLE VERSION
```
ansible 2.3.1.0
```

##### CONFIGURATION
fact_caching=redis

##### OS / ENVIRONMENT
N/A (Ubuntu 16.04)


##### SUMMARY
Using `meta: clear_cache` throws an Exception
<!--- Explain the problem briefly -->

##### STEPS TO REPRODUCE


<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: Gather all facts on all given hosts
  hosts: all
  sudo: true
  gather_facts: True

  tasks:
    - name: Clear gathered facts
      meta: clear_facts
```

```
ansible-playbook -i hosts gather_facts.yml -vvv
```
<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
Facts are re-gathered.

##### ACTUAL RESULTS

```
PLAY [Gather all facts on all given hosts] *********************************************************************************************
META: ran handlers
ERROR! Unexpected Exception: coercing to Unicode: need string or buffer, Host found
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-playbook", line 109, in <module>
    exit_code = cli.run()
  File "/usr/local/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 154, in run
    results = pbex.run()
  File "/usr/local/lib/python2.7/dist-packages/ansible/executor/playbook_executor.py", line 153, in run
    result = self._tqm.run(play=play)
  File "/usr/local/lib/python2.7/dist-packages/ansible/executor/task_queue_manager.py", line 284, in run
    play_return = strategy.run(iterator, play_context)
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/strategy/linear.py", line 221, in run
    results.extend(self._execute_meta(task, play_context, iterator, host))
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 883, in _execute_meta
    self._variable_manager.clear_facts(host)
  File "/usr/local/lib/python2.7/dist-packages/ansible/vars/__init__.py", line 664, in clear_facts
    del self._fact_cache[hostname]
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/cache/__init__.py", line 54, in __delitem__
    self._plugin.delete(key)
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/cache/redis.py", line 89, in delete
    self._cache.delete(self._make_key(key))
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/cache/redis.py", line 54, in _make_key
    return self._prefix + key
TypeError: coercing to Unicode: need string or buffer, Host found
```


This is very similar to  #18708 (which I experience with Ansible 2.2 and was fixed by upgrading to 2.3.1.


Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py
--- a/lib/ansible/plugins/strategy/__init__.py
+++ b/lib/ansible/plugins/strategy/__init__.py
@@ -866,7 +866,8 @@ def _evaluate_conditional(h):
         elif meta_action == 'clear_facts':
             if _evaluate_conditional(target_host):
                 for host in self._inventory.get_hosts(iterator._play.hosts):
-                    self._variable_manager.clear_facts(host)
+                    hostname = host.get_name()
+                    self._variable_manager.clear_facts(hostname)
                 msg = "facts cleared"
             else:
                 skipped = True


Test patch: