Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ahochor committed Jan 24, 2019
1 parent 596d5d2 commit c46823c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 30 deletions.
10 changes: 8 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
ansible_user: user

postgresql_version: 10

postgresql_replication:
Expand All @@ -16,6 +18,9 @@ postgresql_users:

data_dir: /data/db

patroni_location: /usr/local/bin/patroni
patronictl_location: /usr/local/bin/patronictl

system_interface: eth0

locale: en_US.UTF-8
Expand All @@ -25,7 +30,7 @@ requirements:
- virtualenv
- urllib3>=1.19.1,!=1.21
- boto
- psycopg2-binary
- psycopg2>=2.5.4
- PyYAML
- requests
- six >= 1.7
Expand All @@ -36,5 +41,6 @@ requirements:
- prettytable>=0.7
- tzlocal
- python-dateutil
- psutil
- psutil>=2.0.0
- cdiff
- psycopg2-binary
32 changes: 23 additions & 9 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
- name: configure postgresql
template:
src: "{{ playbook_dir }}/../templates/postgresql.conf.j2"
src: postgresql.conf.j2
dest: "/etc/postgresql/{{ postgresql_version }}/main/postgresql.conf"

- name: configure pg_hba
template:
src: "{{ playbook_dir }}/../templates/pg_hba.conf.j2"
src: pg_hba.conf.j2
dest: "/etc/postgresql/{{ postgresql_version }}/main/pg_hba.conf"

- name: configure etcd
template:
src: "{{ playbook_dir }}/../templates/etcd.j2"
src: etcd.j2
dest: /etc/default/etcd
when: "inventory_hostname == groups['postgresql'][0]"
notify:
Expand All @@ -34,28 +34,42 @@
file:
path: "{{ data_dir }}"
state: directory
owner: postgres
group: postgres
owner: soi
group: soi
mode: 0700
recurse: yes
changed_when: false

- name: locate patroni
find:
paths: /
patterns: "patroni"
recurse: yes
register: location

- name: set location var
set_fact:
patroni_location: "{{ location.files[0].path }}"

- name: configure patroni
template:
src: "{{ playbook_dir }}/../templates/patroni.yml.j2"
src: patroni.yml.j2
dest: /etc/patroni.yml
owner: root
group: root
mode: 0644

- name: copy patroni systemd service file
template:
src: "{{ playbook_dir }}/../templates/patroni.service.j2"
src: patroni.service.j2
dest: /etc/systemd/system/patroni.service
owner: root
group: root
mode: 0644
notify:
- start postgres cluster

- meta: flush_handlers
- name: trigger patroni restart
command: /bin/true
notify: start postgres cluster

- meta: flush_handlers
30 changes: 20 additions & 10 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@
when: ansible_distribution == 'Ubuntu' or ansible_distribution == 'Debian'

- name: install postgresql
become: yes
package:
name: "postgresql-{{ postgresql_version }}"
state: present

- name: install pip dependencies
become: yes
pip:
name: "{{ requirements }}"
state: present

- name: install etcd
become: yes
package:
name: etcd
state: present
when: "inventory_hostname == groups['postgresql'][0]"

- name: install patroni
- name: install python,python-pip
become: yes
package:
name:
- python
- python-pip
state: present

- name: install pip dependencies
become_user: "{{ ansible_user }}"
pip:
name: patroni
name: "{{ requirements }}"
state: present
extra_args: --ignore-installed

- name: install patroni
become_user: "{{ ansible_user }}"
pip:
name: patroni[etcd]
state: present
extra_args: --ignore-installed
register: status
2 changes: 1 addition & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

- include: users.yml

- include: tests.yml
- include: tests.yml
19 changes: 16 additions & 3 deletions tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
---
- name: locate patronictl
find:
paths: /
patterns: "patronictl"
recurse: yes
register: locationctl

- name: set location var
set_fact:
patronictl_location: "{{ locationctl.files[0].path }}"

- name: check cluster
command: "patronictl -d etcd://127.0.0.1:2379 list cluster"
become_user: "{{ ansible_user }}"
shell: "{{ patronictl_location }} -d etcd://127.0.0.1:2379 list cluster"
when: "inventory_hostname == groups['postgresql'][0]"
changed_when: false
register: status
Expand All @@ -9,6 +21,7 @@
var: status

- name: get node master
become_user: "{{ ansible_user }}"
shell: "PGPASSWORD={{ postgresql_admin.pass }} psql -U {{ postgresql_admin.user }} -h {{ hostvars[inventory_hostname]['ansible_' + system_interface]['ipv4']['address'] }} -c 'SELECT pg_is_in_recovery()';"
register: status
changed_when: false
Expand All @@ -30,7 +43,7 @@
leader_node: "{{ inventory_hostname }}"

- name: test cluster - create database on leader
run_once: true
ignore_errors: yes
shell: "PGPASSWORD={{ postgresql_admin.pass }} psql -U {{ postgresql_admin.user }} -h {{ hostvars[inventory_hostname]['ansible_' + system_interface]['ipv4']['address'] }} -c 'CREATE DATABASE test';"
changed_when: false
when: "inventory_hostname == leader_node"
Expand All @@ -40,7 +53,7 @@
changed_when: false

- name: test cluster - drop database on leader
run_once: true
ignore_errors: yes
shell: "PGPASSWORD={{ postgresql_admin.pass }} psql -U {{ postgresql_admin.user }} -h {{ hostvars[inventory_hostname]['ansible_' + system_interface]['ipv4']['address'] }} -c 'DROP DATABASE test';"
changed_when: false
when: "inventory_hostname == leader_node"
2 changes: 2 additions & 0 deletions tasks/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
leader_node: "{{ inventory_hostname }}"

- name: create databases
become_user: "{{ ansible_user }}"
ignore_errors: yes
postgresql_db:
name: "{{ item.value.db }}"
Expand All @@ -32,6 +33,7 @@
when: "inventory_hostname == leader_node"

- name: create users
become_user: "{{ ansible_user }}"
ignore_errors: yes
postgresql_user:
name: "{{ item.key }}"
Expand Down
6 changes: 3 additions & 3 deletions templates/patroni.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ After=syslog.target network.target
[Service]
Type=simple

User=postgres
Group=postgres
User={{ ansible_user }}
Group={{ ansible_user }}

ExecStart=/usr/local/bin/patroni /etc/patroni.yml
ExecStart={{ patroni_location }} /etc/patroni.yml

Restart=always

Expand Down
2 changes: 0 additions & 2 deletions tests/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@
- hosts: all
roles:
- ansible-postgresql
vars:
cassandra_alter_password: "testpassword"

0 comments on commit c46823c

Please sign in to comment.