diff options
author | Paul B <paul+gh@bonaud.fr> | 2019-01-29 15:45:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-29 15:45:25 +0100 |
commit | 0c83dd00b4110d55b1602ca8d732a8cb414cfd0a (patch) | |
tree | 5191176c7f7226def631feb0f6cdc2a12045f0e4 | |
parent | 2bedf1bbf762903d955e4a76f674799a6acb2258 (diff) | |
parent | 7b241a44e602d70c97e9b9e5af4b600ff7eb3651 (diff) | |
download | ansible-postgresql-role-1.4.0.tar.gz ansible-postgresql-role-1.4.0.tar.zst ansible-postgresql-role-1.4.0.zip |
Merge pull request #6 from paulRbr/pg-extensions1.4.0
feat(extensions): adds creation of extension on databases if needed
-rw-r--r-- | .travis.yml | 11 | ||||
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | tasks/main.yml | 1 | ||||
-rw-r--r-- | tasks/postgres-common-postinstall.yml | 14 | ||||
-rw-r--r-- | tasks/postgres-database-extensions.yml | 23 | ||||
-rw-r--r-- | tasks/postgres-database.yml | 7 | ||||
-rw-r--r-- | test/main.yml | 41 | ||||
-rw-r--r-- | test/requirements.yml | 2 |
8 files changed, 85 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml index 7a72289..ddf6fb0 100644 --- a/.travis.yml +++ b/.travis.yml | |||
@@ -11,9 +11,13 @@ addons: | |||
11 | packages: | 11 | packages: |
12 | - python-pip | 12 | - python-pip |
13 | 13 | ||
14 | # Tests are using docker to spawn fake instances | ||
15 | services: | ||
16 | - docker | ||
17 | |||
14 | install: | 18 | install: |
15 | # Install ansible | 19 | # Install ansible |
16 | - pip install ansible | 20 | - pip install ansible docker-py docker |
17 | # Check ansible version | 21 | # Check ansible version |
18 | - ansible --version | 22 | - ansible --version |
19 | - ansible-galaxy install -r test/requirements.yml -p test/roles/ | 23 | - ansible-galaxy install -r test/requirements.yml -p test/roles/ |
@@ -21,6 +25,11 @@ install: | |||
21 | script: | 25 | script: |
22 | # Basic role syntax check | 26 | # Basic role syntax check |
23 | - ansible-playbook test/main.yml -i test/hosts --syntax-check | 27 | - ansible-playbook test/main.yml -i test/hosts --syntax-check |
28 | # Run the tests | ||
29 | # FAILS - ansible-playbook test/main.yml -i test/hosts | ||
30 | # Even if Docker and docker-py module are present, the tests fail on Travis | ||
31 | # The error on Travis is: | ||
32 | # => {"changed": false, "item": {"image": "python:3.7-stretch", "links": ["postgres_barman", "postgres_one"], "name": "postgres_three"}, "msg": "Failed to import docker or docker-py - No module named docker. Try `pip install docker` or `pip install docker-py` (Python 2.6)"} | ||
24 | 33 | ||
25 | notifications: | 34 | notifications: |
26 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ | 35 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ |
@@ -14,7 +14,6 @@ To install: | |||
14 | ansible-galaxy install trainline-eu.ansible_postgresql_role | 14 | ansible-galaxy install trainline-eu.ansible_postgresql_role |
15 | ``` | 15 | ``` |
16 | 16 | ||
17 | |||
18 | #### Dependencies | 17 | #### Dependencies |
19 | 18 | ||
20 | No dependencies | 19 | No dependencies |
@@ -36,7 +35,6 @@ This table lists the tested version of OS/PostgreSQL couples. | |||
36 | - :interrobang: - maybe works, not tested | 35 | - :interrobang: - maybe works, not tested |
37 | - :no_entry: - PostgreSQL has reached EOL | 36 | - :no_entry: - PostgreSQL has reached EOL |
38 | 37 | ||
39 | |||
40 | #### Variables | 38 | #### Variables |
41 | 39 | ||
42 | ```yaml | 40 | ```yaml |
@@ -60,6 +58,9 @@ postgres_clusters: # Mandatory | |||
60 | databases: | 58 | databases: |
61 | - dbname: my_database # Mandatory | 59 | - dbname: my_database # Mandatory |
62 | owner: john # Mandatory | 60 | owner: john # Mandatory |
61 | extensions: # Optional | ||
62 | - names: [ 'postgis', 'postgis_topology' ] | ||
63 | apt_deps: [ 'postgresql-11-postgis' ] | ||
63 | 64 | ||
64 | # Postgres config (Optional) | 65 | # Postgres config (Optional) |
65 | postgres_log_line_prefix: '%m [%p] database: %d host: %h user: %u ' | 66 | postgres_log_line_prefix: '%m [%p] database: %d host: %h user: %u ' |
diff --git a/tasks/main.yml b/tasks/main.yml index 2fa7883..3d1892c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml | |||
@@ -46,6 +46,7 @@ | |||
46 | - include: postgres-database.yml | 46 | - include: postgres-database.yml |
47 | postgres_dbname={{ item.1.dbname }} | 47 | postgres_dbname={{ item.1.dbname }} |
48 | postgres_owner={{ item.1.owner }} | 48 | postgres_owner={{ item.1.owner }} |
49 | postgres_extensions={{ item.1.extensions|default([]) }} | ||
49 | postgres_port={{ item.0.port }} | 50 | postgres_port={{ item.0.port }} |
50 | with_subelements: | 51 | with_subelements: |
51 | - "{{ postgres_clusters }}" | 52 | - "{{ postgres_clusters }}" |
diff --git a/tasks/postgres-common-postinstall.yml b/tasks/postgres-common-postinstall.yml index 277fafd..715782c 100644 --- a/tasks/postgres-common-postinstall.yml +++ b/tasks/postgres-common-postinstall.yml | |||
@@ -15,13 +15,25 @@ | |||
15 | src: /var/lib/postgresql/.ssh/id_rsa.pub | 15 | src: /var/lib/postgresql/.ssh/id_rsa.pub |
16 | register: db_server_pub_key | 16 | register: db_server_pub_key |
17 | 17 | ||
18 | - name: Check if barman is installed on barman server | ||
19 | getent: | ||
20 | database: passwd | ||
21 | key: "barman" | ||
22 | delegate_to: "{{ postgres_barman_server_public|default(postgres_barman_server) }}" | ||
23 | when: postgres_barman_server is defined | ||
24 | register: barman_user_exists | ||
25 | ignore_errors: true | ||
26 | changed_when: false | ||
27 | |||
18 | - name: Allow SSH access on barman server | 28 | - name: Allow SSH access on barman server |
19 | authorized_key: | 29 | authorized_key: |
20 | user: "barman" | 30 | user: "barman" |
21 | key: "{{ db_server_pub_key['content']|b64decode }}" | 31 | key: "{{ db_server_pub_key['content']|b64decode }}" |
22 | state: present | 32 | state: present |
23 | delegate_to: "{{ postgres_barman_server_public|default(postgres_barman_server) }}" | 33 | delegate_to: "{{ postgres_barman_server_public|default(postgres_barman_server) }}" |
24 | when: postgres_barman_server is defined | 34 | when: |
35 | - postgres_barman_server is defined | ||
36 | - barman_user_exists is succeeded | ||
25 | 37 | ||
26 | - name: Copy logrotate configuration for postgresql | 38 | - name: Copy logrotate configuration for postgresql |
27 | template: src=logrotate-postgresql-common.j2 dest=/etc/logrotate.d/postgresql-common owner=root group=root mode=0644 | 39 | template: src=logrotate-postgresql-common.j2 dest=/etc/logrotate.d/postgresql-common owner=root group=root mode=0644 |
diff --git a/tasks/postgres-database-extensions.yml b/tasks/postgres-database-extensions.yml new file mode 100644 index 0000000..edc4e8d --- /dev/null +++ b/tasks/postgres-database-extensions.yml | |||
@@ -0,0 +1,23 @@ | |||
1 | --- | ||
2 | - name: "Install required packages for 'postgres_ext' ansible module" | ||
3 | apt: | ||
4 | name: | ||
5 | - libpq-dev | ||
6 | - python-psycopg2 | ||
7 | |||
8 | - name: Install system dependencies of extensions | ||
9 | apt: | ||
10 | name: "{{ postgres_extension.apt_deps | default([]) }}" | ||
11 | cache_valid_time: 86400 | ||
12 | |||
13 | - name: "CREATE EXTENSION on database {{ postgres_dbname }}" | ||
14 | become: true | ||
15 | become_user: postgres | ||
16 | become_method: su | ||
17 | postgresql_ext: | ||
18 | name: "{{ postgres_extension_name }}" | ||
19 | db: "{{ postgres_dbname }}" | ||
20 | port: "{{ postgres_port }}" | ||
21 | loop: "{{ postgres_extension.names | default([]) }}" | ||
22 | loop_control: | ||
23 | loop_var: postgres_extension_name | ||
diff --git a/tasks/postgres-database.yml b/tasks/postgres-database.yml index c7b09a8..c3537ea 100644 --- a/tasks/postgres-database.yml +++ b/tasks/postgres-database.yml | |||
@@ -18,3 +18,10 @@ | |||
18 | owner: "{{ postgres_owner }}" | 18 | owner: "{{ postgres_owner }}" |
19 | when: | 19 | when: |
20 | - postgres_up.rc == 0 | 20 | - postgres_up.rc == 0 |
21 | |||
22 | - name: Create database extensions | ||
23 | include_tasks: postgres-database-extensions.yml | ||
24 | loop: "{{ postgres_extensions| default([]) }}" | ||
25 | loop_control: | ||
26 | loop_var: postgres_extension | ||
27 | when: postgres_up.rc == 0 | ||
diff --git a/test/main.yml b/test/main.yml index 6258199..a4834be 100644 --- a/test/main.yml +++ b/test/main.yml | |||
@@ -49,12 +49,25 @@ | |||
49 | archive_enabled: True | 49 | archive_enabled: True |
50 | barman_directory: "{{ postgres_barman_directory }}" | 50 | barman_directory: "{{ postgres_barman_directory }}" |
51 | users: | 51 | users: |
52 | - username: tester | ||
53 | password: tester | ||
52 | - username: barman | 54 | - username: barman |
53 | password: "secret_cocktail" | 55 | password: "secret_cocktail" |
54 | permissions: 'SUPERUSER' | 56 | permissions: 'SUPERUSER' |
55 | - username: replicator | 57 | - username: replicator |
56 | password: "secret_repli" | 58 | password: "secret_repli" |
57 | permissions: 'REPLICATION' | 59 | permissions: 'REPLICATION' |
60 | databases: | ||
61 | - dbname: test_database | ||
62 | owner: tester | ||
63 | extensions: | ||
64 | - description: PostGis | ||
65 | apt_deps: | ||
66 | - "postgresql-{{ postgres_version }}-postgis" | ||
67 | names: | ||
68 | - postgis | ||
69 | - postgis_topology | ||
70 | - btree_gist | ||
58 | 71 | ||
59 | with_items: | 72 | with_items: |
60 | - postgres_one | 73 | - postgres_one |
@@ -104,18 +117,19 @@ | |||
104 | - name: "{{ postgres_barman_directory }}" | 117 | - name: "{{ postgres_barman_directory }}" |
105 | description: 'Main database' | 118 | description: 'Main database' |
106 | primary_host: postgres_one | 119 | primary_host: postgres_one |
107 | primary_barman_password: "secret_cocktail" | 120 | postgres_barman_password: "secret_cocktail" |
108 | backup_method: rsync | 121 | backup_method: rsync |
109 | ansible_group: db-all | 122 | standby_hosts: "{{ groups['db-all'][1:] }}" |
110 | retention_policy: 'RECOVERY WINDOW OF 7 DAYS' | 123 | retention_policy: 'RECOVERY WINDOW OF 7 DAYS' |
111 | with_items: | 124 | with_items: |
112 | - postgres_barman | 125 | - postgres_barman |
113 | tags: provision_docker | 126 | tags: provision_docker |
114 | 127 | ||
115 | - name: Test installing Barman role | 128 | - name: Test installing Postgresql role |
116 | hosts: barman | 129 | hosts: primary:secondary |
117 | connection: docker | 130 | connection: docker |
118 | vars: {} | 131 | vars: |
132 | postgres_listen_addresses: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}" | ||
119 | pre_tasks: &pre_tasks | 133 | pre_tasks: &pre_tasks |
120 | - name: "Build hosts file" | 134 | - name: "Build hosts file" |
121 | shell: 'echo "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }} {{inventory_hostname}}" >> /etc/hosts' | 135 | shell: 'echo "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }} {{inventory_hostname}}" >> /etc/hosts' |
@@ -123,15 +137,6 @@ | |||
123 | 137 | ||
124 | - debug: msg="Running on host {{inventory_hostname}} ({{ hostvars[inventory_hostname].ansible_default_ipv4.address }})" | 138 | - debug: msg="Running on host {{inventory_hostname}} ({{ hostvars[inventory_hostname].ansible_default_ipv4.address }})" |
125 | roles: | 139 | roles: |
126 | - { role: barman, tags: ['barman'] } | ||
127 | |||
128 | - name: Test installing Postgresql role | ||
129 | hosts: primary:secondary | ||
130 | connection: docker | ||
131 | vars: | ||
132 | postgres_listen_addresses: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}" | ||
133 | pre_tasks: *pre_tasks | ||
134 | roles: | ||
135 | - { role: ansible-postgresql-role, tags: ['postgres'] } | 140 | - { role: ansible-postgresql-role, tags: ['postgres'] } |
136 | tasks: | 141 | tasks: |
137 | - name: Start postgres cluster | 142 | - name: Start postgres cluster |
@@ -144,6 +149,14 @@ | |||
144 | command: psql --port=5432 --command='select NOW()' | 149 | command: psql --port=5432 --command='select NOW()' |
145 | 150 | ||
146 | 151 | ||
152 | - name: Test installing Barman role | ||
153 | hosts: barman | ||
154 | connection: docker | ||
155 | vars: {} | ||
156 | pre_tasks: *pre_tasks | ||
157 | roles: | ||
158 | - { role: barman, tags: ['barman'] } | ||
159 | |||
147 | - name: Test running Postgresql role when database is started | 160 | - name: Test running Postgresql role when database is started |
148 | connection: docker | 161 | connection: docker |
149 | hosts: primary:secondary | 162 | hosts: primary:secondary |
diff --git a/test/requirements.yml b/test/requirements.yml index 4c72838..f9d7099 100644 --- a/test/requirements.yml +++ b/test/requirements.yml | |||
@@ -3,4 +3,4 @@ | |||
3 | version: 7a6243dfcf69f9d262877eca487bc14b62ba6960 | 3 | version: 7a6243dfcf69f9d262877eca487bc14b62ba6960 |
4 | - src: trainline-eu.ansible_barman_role | 4 | - src: trainline-eu.ansible_barman_role |
5 | name: barman | 5 | name: barman |
6 | version: 1.0.0 | 6 | version: 1.1.0 |