Repository: ansible/ansible

Issue:
Inconsistent behavior with quoted variable values in inventory file
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
n/a

##### ANSIBLE VERSION
```
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
None

##### OS / ENVIRONMENT
CentOS Linux release 7.3.1611 (Core) (but probably not platform specific)

##### SUMMARY
When using quoted variable values (using double quotes (")) in the inventory file, if the variable value contains a hash (#), quotes are added around the value. If the variable value does not contain a hash, no quotes are added.

It's possible this may also affect other characters. This is just the case I ran across.

##### STEPS TO REPRODUCE
inventory file:
```
[test]
127.0.0.1

[all:vars]
var1="aengiZ9chiP4SiuDah0phooW1oe~m$ua"
var2="Hae~n5OhcoongiuZ5SaiKohDe6oosh#e"
```

playbook:
```yaml
---

- name: "Test"
  hosts: test
  connection: local
  roles:
    - test
```

roles/tests/tasks/main.yml
```yaml
---

- name: "Test"
  debug:
    msg: "Var1: {{var1}}  :: Var2: {{var2}}"
```

##### EXPECTED RESULTS
I expected either both variables to have quotes added, or both variables to not have quotes added.

##### ACTUAL RESULTS
```
$ ansible-playbook playbook.yml -i hosts

PLAY [Test] ********************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [test : Test] *************************************************************
ok: [127.0.0.1] => {
    "msg": "Var1: aengiZ9chiP4SiuDah0phooW1oe~m$ua  :: Var2: \"Hae~n5OhcoongiuZ5SaiKohDe6oosh#e\""
}
```


Aside: I specifically encountered this issue creating users with the mysql_user module:
```
- name: "Create application user"
  mysql_user:
    name: "{{mysql_user_name}}"
    host: '%'
    password: "{{mysql_user_password}}"
    priv: "{{project_name}}.*:ALL"
    state: present
    update_password: always
    login_user: "root"
    login_password: "{{mysql_root_password}}"
```

The root password contained no hash. The mysql_user_password contained a hash. So ansible was able to create the user perfectly fine, but when I attempted to login (using the mysql commandline client), I was unable to do so unless I added a set of double quotes (") around the password.

##### WORKAROUND
Removing the quotes on both the variable values in the inventory file fixes the issue for me, but I still believe behavior should be consistent when the quotes are present.


Discussion:

Resolving patch:
diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py
--- a/lib/ansible/inventory/ini.py
+++ b/lib/ansible/inventory/ini.py
@@ -327,17 +327,16 @@ def _parse_value(v):
         Attempt to transform the string value from an ini file into a basic python object
         (int, dict, list, unicode string, etc).
         '''
-        if "#" not in v:
-            try:
-                v = ast.literal_eval(v)
-            # Using explicit exceptions.
-            # Likely a string that literal_eval does not like. We wil then just set it.
-            except ValueError:
-                # For some reason this was thought to be malformed.
-                pass
-            except SyntaxError:
-                # Is this a hash with an equals at the end?
-                pass
+        try:
+            v = ast.literal_eval(v)
+        # Using explicit exceptions.
+        # Likely a string that literal_eval does not like. We wil then just set it.
+        except ValueError:
+            # For some reason this was thought to be malformed.
+            pass
+        except SyntaxError:
+            # Is this a hash with an equals at the end?
+            pass
         return to_text(v, nonstring='passthru', errors='surrogate_or_strict')
 
     def get_host_variables(self, host):


Test patch: