# postgresql\_user module failed to authenticate as postgres when set ansible\_become

**URL:** <https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028>\
**Category:** Ansible Project\
**Created:** [March 31, 2016, 4:51am UTC](https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028 "2016-03-31T04:51:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cuong\_Nguyen1](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@Cuong\_Nguyen1](https://forum.ansible.com/u/Cuong_Nguyen1)\
**Post date:** [March 31, 2016, 4:51am UTC](https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028/1 "2016-03-31T04:51:26Z")

</div>

Hi guys,

I have a task to create postgresql user as follow:

`

- name: Create database user  
postgresql\_user: \>  
name=“{{ db\_user }}”  
password=“{{ db\_pass }}”  
encrypted=yes  
role\_attr\_flags=“NOSUPERUSER,NOCREATEDB,NOCREATEROLE,INHERIT,LOGIN”  
sudo\_user: postgres  
sudo: yes  
tags:
- postgres

`

This task can successfully create user as expected. I also captured a syslog:

`2016-03-30T11:46:01.690552+02:00 db sudo: root : TTY=pts/2 ; PWD=/root ; USER=postgres ; COMMAND=/bin/sh -c echo BECOME-SUCCESS-peojtdazxizsqojlbrozgxmwzmztqcwa; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /tmp/ansible-tmp-1459397861.99-46152503652180/postgresql_user`

But when I set these variables in group\_vars/all:

`ansible_become: yes ansible_become_user: root ansible_become_method: sudo`

Above task failed to run with message from postgresql:

`msg: unable to connect to database: FATAL: Peer authentication failed for user "postgres"`

Syslog message as follow:

`2016-03-30T11:11:08.739388+02:00 db sudo: root : TTY=pts/2 ; PWD=/root ; USER=root ; COMMAND=/bin/sh -c echo BECOME-SUCCESS-vvxlieenoiilppjbzaodouktzfzonxpf; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1459398169.76-216558803101428/postgresql_user; rm -rf /root/.ansible/tmp/ansible-tmp-1459398169.76-216558803101428/ >/dev/null 2>&1`

As you can see, the difference is **`USER`** variable between two runs.

Current setting of pg\_hba.conf file:

`  
local all postgres peer  
local all all peer  
host all all 127.0.0.1/32 md5  
host all all ::1/128 md5

`

Environment:

Ansible host: Debian Jessie  
Ansible version:1.9.4  
Remote host: SLES 12

Can anyone explain for me this case?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 31, 2016, 9:07pm UTC](https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028/2 "2016-03-31T21:07:43Z")

</div>

Expected as this `ansible_become_user: root` overrides your `sudo_user: postgres` so you are running it as root.

---

<div class="post-metadata">

**Author:** ![Cuong\_Nguyen1](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@Cuong\_Nguyen1](https://forum.ansible.com/u/Cuong_Nguyen1)\
**Post date:** [April 1, 2016, 4:31am UTC](https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028/3 "2016-04-01T04:31:00Z")

</div>

Hi Brian,  
Is there any way to make directives set in task level like mine gets override such directives set in `group_vars` or `host_vars`?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 1, 2016, 2:25pm UTC](https://forum.ansible.com/t/postgresql-user-module-failed-to-authenticate-as-postgres-when-set-ansible-become/21028/4 "2016-04-01T14:25:12Z")

</div>

directives and vars are different things, connection vars override directives. connection vars do follow normal var precedence, so you can override the inventory values in the play several ways.
