forked from openedx-unsupported/configuration
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_pgsql_db_and_users.yml
80 lines (73 loc) · 2.37 KB
/
create_pgsql_db_and_users.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# This play will create databases and user for an application.
# It can be run like so:
#
# ansible-playbook -c local -i 'localhost,' create_pgsql_db_and_users.yml -e@./db.yml
#
# If running ansible from a python virtualenv you will need a command like the following
#
# ansible-playbook -c local -i 'localhost,' create_pgsql_db_and_users.yml -e@./db.yml -e "ansible_python_interpreter=$(which python)"
#
# the content of db.yml contains the following dictionaries
# database_connection: &default_connection
# login_host: "pgsql.example.org"
# login_user: "root"
# login_password: "super-secure-password"
# databases:
# - name: "example"
# state: "present"
# <<: *default_connection
# database_users:
# - name: "example"
# db: "example"
# state: "present"
# password: "user-password"
# privs: "ALL"
# db_objects: "ALL_DEFAULT"
# db_object_type: "default_privs"
# <<: *default_connection
- name: Create databases and users
hosts: all
gather_facts: False
tasks:
# Install required library, currently this needs to be available
# to system python.
- name: install PostgreSQL client
pip: name={{item}} state=present
with_items:
- psycopg2-binary
- name: create PostgreSQL databases
postgresql_db:
db: "{{ item.name}}"
state: "{{ item.state }}"
login_host: "{{ item.login_host }}"
login_user: "{{ item.login_user }}"
login_password: "{{ item.login_password }}"
with_items: "{{ databases }}"
tags:
- dbs
- name: Create PostgreSQL users
postgresql_user:
db: "{{ item.db }}"
name: "{{ item.name }}"
state: "{{ item.state | default('present') }}"
password: "{{ item.password }}"
login_host: "{{ item.login_host }}"
login_user: "{{ item.login_user }}"
login_password: "{{ item.login_password }}"
with_items: "{{ database_users }}"
tags:
- users
- name: Assign privileges
postgresql_privs:
roles: "{{ item.name }}"
db: "{{ item.db }}"
privs: "{{ item.privs }}"
objs: "{{ item.db_objects }}"
type: "{{ item.db_object_type }}"
login_host: "{{ item.login_host }}"
login_user: "{{ item.login_user }}"
login_password: "{{ item.login_password }}"
with_items: "{{ database_users }}"
tags:
- privileges