Repository: ansible/ansible

Issue:
`postgresql_user` module does not treat quoted identifier well
_From @aleksandr-vin on October 31, 2016 14:29_

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
`postgresql_user` module

##### ANSIBLE VERSION
```
ansible 2.1.2.0
  config file = /Users/aleksandr.vinokurov/.ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
`$ cat ~/.ansible.cfg`:
```
[ssh_connection]
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r

[defaults]
callback_plugins = plugins/callbacks/
vault_password_file = ~/the_ansible_vault.sh
nocows = 1
ansible_managed = This file is managed by Ansible, all changes will be lost.
```

`env | grep ANSIBLE_`:
```
ANSIBLE_VAULT_PASSWORD_FILE=/Users/aleksandr.vinokurov/the_ansible_vault.sh
ANSIBLE_HOME=/Users/aleksandr.vinokurov/ansible
```

##### OS / ENVIRONMENT
OS running:
```
Darwin Vinokurovs-Mac.local 15.6.0 Darwin Kernel Version 15.6.0: Mon Aug 29 20:21:34 PDT 2016; root:xnu-3248.60.11~1/RELEASE_X86_64 x86_64
```

OS managing:
```
Linux all-in-one-services.test 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
```

##### SUMMARY
When using `postgresql_user` module with quoted identifier, then checks fail (because of regexp) and the task is always *changed* even when you rerun.

##### STEPS TO REPRODUCE
You need a POSTGRES_BOX with Postgresql (checked on 9.6) and 'foo-bar' db on it. Then run next commands:
```
echo "First run"
ansible POSTGRES_BOX -i inventory -m postgresql_user -a "name=foo-bar db=foo-bar port=5432 priv='ALL' state=present" --one-line

echo "Second run"
ansible POSTGRES_BOX -i inventory -m postgresql_user -a "name=foo-bar db=foo-bar port=5432 priv='ALL' state=present" --one-line
```

##### EXPECTED RESULTS
```
First run
POSTGRES_BOX | SUCCESS => {"changed": true, "user": "foo-bar"}
Second run
POSTGRES_BOX | SUCCESS => {"changed": false, "user": "foo-bar"}
```

##### ACTUAL RESULTS
```
First run
POSTGRES_BOX | SUCCESS => {"changed": true, "user": "foo-bar"}
Second run
POSTGRES_BOX | SUCCESS => {"changed": true, "user": "foo-bar"}
```


_Copied from original issue: ansible/ansible-modules-core#5441_


Discussion:
_From @ansibot on October 31, 2016 14:49_

<!-- @aleksandr-vin: Thanks for filing this bug report! -->

@ansible ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

_From @ansibot on November 16, 2016 9:31_

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

_From @ansibot on December 7, 2016 19:57_

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the <a href="http://docs.ansible.com/ansible/dev_guide/repomerge.html#move-issues-and-prs-to-new-repo">repomerge</a> page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

<!-- boilerplate: repomerge -->

Resolving patch:
diff --git a/lib/ansible/modules/database/postgresql/postgresql_user.py b/lib/ansible/modules/database/postgresql/postgresql_user.py
--- a/lib/ansible/modules/database/postgresql/postgresql_user.py
+++ b/lib/ansible/modules/database/postgresql/postgresql_user.py
@@ -400,7 +400,7 @@ def get_database_privileges(cursor, user, db):
     datacl = cursor.fetchone()[0]
     if datacl is None:
         return set()
-    r = re.search('%s=(C?T?c?)/[a-z]+\,?' % user, datacl)
+    r = re.search('%s\\\\?\"?=(C?T?c?)/[^,]+\,?' % user, datacl)
     if r is None:
         return set()
     o = set()


Test patch: