Example ansible playbook and role to set up postgres database Based on https://blog.apcelent.com/using-ansible-to-set-up-postgresql.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

  1. ---
  2. - name: Ensure bash, OpenSSl, and libssl are the latest versions
  3. apt:
  4. pkg:
  5. - bash
  6. - openssl
  7. - libssl-dev
  8. - libssl-doc
  9. state: latest
  10. tags: packages
  11. - name: Install PostgreSQL
  12. apt:
  13. pkg:
  14. - postgresql
  15. - postgresql-contrib
  16. - libpq-dev
  17. - python3-psycopg2
  18. state: present
  19. tags: packages
  20. - name: Ensure the PostgreSQL service is running
  21. service: name=postgresql state=started enabled=yes
  22. - name: Ensure database is created
  23. become: yes
  24. become_user: postgres
  25. postgresql_db: name={{ db_name }}
  26. encoding='UTF-8'
  27. template='template0'
  28. state=present
  29. - name: Ensure user has access to the database
  30. become: yes
  31. become_user: postgres
  32. postgresql_user: db={{ db_name }}
  33. name={{ db_user }}
  34. password={{ db_password }}
  35. priv=ALL
  36. state=present
  37. - name: Ensure user does not have unnecessary privileges
  38. become: yes
  39. become_user: postgres
  40. postgresql_user: name={{ db_user }}
  41. role_attr_flags=NOSUPERUSER,NOCREATEDB
  42. state=present