How to create postgres db and user via ansible


#1

Hi

I have using ubuntu 14.04 is a control machine and my target server is ubuntu 14.04 I am trying to create postgres db and new user … am getting below error … I dont know … how to resolve this.

need a help … see my playbook below

  • hosts: postgres
    become: yes
    tasks:
    • name: Installing postgres
      apt: name={{ item }} state=present update_cache=yes
      with_items:
      • postgresql-9.3
      • libpq-dev
    • name: Ensure postgres Service up and running
      service: name=postgresql state=started enabled=yes
    • name : create database
      sudo_user: postgres
      postgresql_db: name=newdb encoding=‘UTF-8’ login_host=localhost login_user=postgres template=‘template0’
    • name: create database user
      sudo_user: postgres
      postgresql_user: db=newdb name=deva password=123 priv=ALL login_host=localhost

And my error when play the playbook

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File “/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 119, in run
res = self._execute()
File “/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 490, in _execute
result = self._handler.run(task_vars=variables)
File “/usr/lib/python2.7/dist-packages/ansible/plugins/action/normal.py”, line 33, in run
results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
File “/usr/lib/python2.7/dist-packages/ansible/plugins/action/init.py”, line 617, in _execute_module
self._fixup_perms2(remote_files, remote_user)
File “/usr/lib/python2.7/dist-packages/ansible/plugins/action/init.py”, line 394, in _fixup_perms2
’ see https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user’.format(res['rc’], res[‘stderr’]))
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u2018’ in position 29: ordinal not in range(128)

fatal: [52.32.42.38]: FAILED! => {
“failed”: true,
“msg”: “Unexpected failure during module execution.”,
“stdout”: “”
}


#2

TASK [create database] *********************************************************
task path: /home/deva/git/ansible_training/deploy_rails_app_ansible/playbook/postgres.yml:12
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/core/database/postgresql/postgresql_db.py
<52.32.42.38> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<52.32.42.38> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/deva/.ansible/cp/ansible-ssh-%h-%p-%r 52.32.42.38 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “echo $HOME/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803” && echo ansible-tmp-1478769800.61-279265144812803="echo $HOME/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803" ) && sleep 0’"’"’’
<52.32.42.38> PUT /tmp/tmp8FoAwn TO /home/ubuntu/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803/postgresql_db.py
<52.32.42.38> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/deva/.ansible/cp/ansible-ssh-%h-%p-%r ‘[52.32.42.38]’
<52.32.42.38> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<52.32.42.38> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/deva/.ansible/cp/ansible-ssh-%h-%p-%r 52.32.42.38 ‘/bin/sh -c ‘"’"‘chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803/ /home/ubuntu/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803/postgresql_db.py && sleep 0’"’"’’
<52.32.42.38> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<52.32.42.38> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/deva/.ansible/cp/ansible-ssh-%h-%p-%r -tt 52.32.42.38 ‘/bin/sh -c ‘"’"‘sudo -H -S -n -u root /bin/sh -c ‘"’"’"’"’"’"’"’"‘echo BECOME-SUCCESS-ugocvyhxatfpkbupkzpfvzutktjmkhih; /usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803/postgresql_db.py; rm -rf “/home/ubuntu/.ansible/tmp/ansible-tmp-1478769800.61-279265144812803/” > /dev/null 2>&1’"’"’"’"’"’"’"’"’ && sleep 0’"’"’’
fatal: [52.32.42.38]: FAILED! => {
“changed”: false,
“failed”: true,
“invocation”: {
“module_args”: {
“db”: “newdb”,
“encoding”: “”,
“lc_collate”: “”,
“lc_ctype”: “”,
“login_host”: “/var/run/postgresql”,
“login_password”: “”,
“login_unix_socket”: “”,
“login_user”: “postgres”,
“name”: “newdb”,
“owner”: “”,
“port”: “5432”,
“state”: “present”,
“template”: “template0”
},
“module_name”: “postgresql_db”
},
“msg”: “unable to connect to database: FATAL: Peer authentication failed for user “postgres”\n”
}
to retry, use: --limit @/home/deva/git/ansible_training/deploy_rails_app_ansible/playbook/postgres.retry

PLAY RECAP *********************************************************************
52.32.42.38 : ok=0 changed=0 unreachable=0 failed=1


#3

Hi Devaraju… did you fixed this ? I moved to some other project no able to concentrate on my above post.

If you have idea please share thanks.