ansible-postgresql icon indicating copy to clipboard operation
ansible-postgresql copied to clipboard

How to create user and database with this role?

Open fesor opened this issue 10 years ago • 2 comments

I have a small question, how to add user and database within this role? I always used someting like...

- name: postgresql - create user
  sudo_user: postgres
  postgresql_user:  user={{ app_database_user }} password={{ app_database_password }}

- name: postgresql - create db
  sudo_user: postgres
  postgresql_db: name={{ app_database_name }} owner={{app_database_user}}

but maybe i don't know something? For now i added user and database in my playbook, and add in group_vars something like this:

postgresql_authentication:
 - type: local
   user: "{{app_user}}"
   method: ident
   database: all

postgresql_user_map:
 - name: "app_user"
   user: "{{app_user}}"
   pg_user: "{{app_user}}"

Maybe there is some best practices?

fesor avatar Apr 19 '15 08:04 fesor

You're correct.

I had a list of users at one point, with a task iterating over it that was basically a wrapper over postgresql_user, something like:

postgresql_roles:
  - state: present
    name: appuser
    flags:
      - NOSUPERUSER
      - NOCREATEROLE

etc.

But it broke down whenever something sufficiently complicated was needed that couldn't be done with just variables and inline conditionals, so I just left it out until I could think of something better.

I still think something like that could be useful for simple deploys, so I'd accept a PR with a sane implementation.

I'm not happy with the pg_hba list in this role either, for the same reasons above, and also the fact that it is replaced with each deploy. I'll remove it when I can find a good replacement for it, or even a custom module shipped with the role.

zenoamaro avatar Apr 19 '15 09:04 zenoamaro

Do you have a working example? I can't get the snippet in the op to work.

aodj avatar Aug 16 '16 17:08 aodj