Repository: ansible/ansible

Issue:
Error when directory name is comma and space
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME

##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.4.2.0
  config file = /Users/XXX/Desktop/home/stady/Linux/Services/Deploy, Provisioning/Ansible/Sample/Ansible-CentOS/ansible.cfg
  configured module search path = [u'/Users/XXX/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.4.2.0_2/libexec/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
macOS High Sierra version 10.13.2

##### SUMMARY
```
$ cd /Users/XXX/Desktop/home/stady/Linux/Services/Deploy\,\ Provisioning/Ansible/Sample/Ansible-CentOS
$ ansible-playbook -i inventories/prd/hosts ./playbook-web.yml
...
TASK [zabbix/client : enable Server] *********************************************************************************************************************************
fatal: [prd-web001]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'zabbix' is undefined\n\nThe error appears to have been in '/Users/XXX/Desktop/home/stady/Linux/Services/Deploy, Provisioning/Ansible/Sample/Ansible-CentOS/roles/zabbix/client/tasks/conf.yml': line 27, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: enable Server\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'zabbix' is undefined"}
fatal: [prd-web002]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'zabbix' is undefined\n\nThe error appears to have been in '/Users/XXX/Desktop/home/stady/Linux/Services/Deploy, Provisioning/Ansible/Sample/Ansible-CentOS/roles/zabbix/client/tasks/conf.yml': line 27, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: enable Server\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'zabbix' is undefined"}
	to retry, use: --limit @/Users/XXX/Desktop/home/stady/Linux/Services/Deploy, Provisioning/Ansible/Sample/Ansible-CentOS/playbook-web.retry
```

##### STEPS TO REPRODUCE

##### EXPECTED RESULTS

##### ACTUAL RESULTS



Discussion:
I'm not sure there is enough information to go on here.  Where is the `zabbix` variable defined at?

The error alone doesn't directly indicate an issue with your path, only that the `zabbix` variable is not defined.  Providing us with a minimal reproducer to show this issue is needed.

needs_info
Excuse me.
I report that result.
```
$ tree /Users/XXX/Desktop/home/stady/Linux/Services/Deploy,\ Provisioning/Ansible/Sample/Ansible-CentOS
/Users/XXX/Desktop/home/stady/Linux/Services/Deploy,\ Provisioning/Ansible/Sample/Ansible-CentOS
├── ansible.cfg
├── inventories
│   ├── prd
│   │   ├── group_vars
│   │   │   └── all.yml
│   │   └── hosts
├── playbook-test.yml
└── roles
     └── zabbix
          └── client
               ├── files
               └── tasks
                     └── main.yml
```
```
$ cat all.yml
zabbix:
  proxy:
    ip:
      pri: monitor01.local
```
```
$ cat hosts
[web]
test01

[prd:children]
web
```
```
$ playbook-web.yml
---
- hosts: web
  name: CentOS
  become: true
  remote_user: centos
  roles:
    - { role: zabbix/client }
```
```
$ cat main.yml
- name: enable Server
  replace:
    dest: /etc/zabbix/zabbix_agentd.conf
    regexp: ^#Server=(.+)$
    replace: Server="{{ zabbix.proxy.ip.pri }}"
  become: true
```
My testing so far has led me to believe, this happens when the inventory and group_vars are in a subdirectory, and the parent directory has a `,` in the path.

Moving the inventory and group_vars out of the subdir, for whatever reason, doesn't suffer from this.

Still looking into a resolution.

Resolving patch:
diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py
--- a/lib/ansible/vars/manager.py
+++ b/lib/ansible/vars/manager.py
@@ -255,7 +255,7 @@ def _plugins_inventory(entities):
                 ''' merges all entities by inventory source '''
                 data = {}
                 for inventory_dir in self._inventory._sources:
-                    if ',' in inventory_dir:  # skip host lists
+                    if ',' in inventory_dir and not os.path.exists(inventory_dir):  # skip host lists
                         continue
                     elif not os.path.isdir(inventory_dir):  # always pass 'inventory directory'
                         inventory_dir = os.path.dirname(inventory_dir)


Test patch:
diff --git a/test/integration/targets/inventory_path_with_comma/aliases b/test/integration/targets/inventory_path_with_comma/aliases
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/inventory_path_with_comma/aliases
@@ -0,0 +1 @@
+posix/ci/group3
diff --git a/test/integration/targets/inventory_path_with_comma/playbook.yml b/test/integration/targets/inventory_path_with_comma/playbook.yml
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/inventory_path_with_comma/playbook.yml
@@ -0,0 +1,9 @@
+---
+- hosts: all
+  gather_facts: false
+  tasks:
+    - name: Ensure we can see group_vars from path with comma
+      assert:
+        that:
+          - inventory_var_from_path_with_commas is defined
+          - inventory_var_from_path_with_commas == 'here'
diff --git a/test/integration/targets/inventory_path_with_comma/runme.sh b/test/integration/targets/inventory_path_with_comma/runme.sh
new file mode 100755
--- /dev/null
+++ b/test/integration/targets/inventory_path_with_comma/runme.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+set -ux
+
+ansible-playbook -i this,path,has,commas/hosts playbook.yml -v "$@"
diff --git a/test/integration/targets/inventory_path_with_comma/this,path,has,commas/group_vars/all.yml b/test/integration/targets/inventory_path_with_comma/this,path,has,commas/group_vars/all.yml
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/inventory_path_with_comma/this,path,has,commas/group_vars/all.yml
@@ -0,0 +1 @@
+inventory_var_from_path_with_commas: 'here'
diff --git a/test/integration/targets/inventory_path_with_comma/this,path,has,commas/hosts b/test/integration/targets/inventory_path_with_comma/this,path,has,commas/hosts
new file mode 100644
--- /dev/null
+++ b/test/integration/targets/inventory_path_with_comma/this,path,has,commas/hosts
@@ -0,0 +1 @@
+localhost ansible_connect=local