diff --git a/defaults/main.yml b/defaults/main.yml index 10a53ec..f52c657 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,6 @@ --- +ansible_user: user + postgresql_version: 10 postgresql_replication: @@ -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 @@ -25,7 +30,7 @@ requirements: - virtualenv - urllib3>=1.19.1,!=1.21 - boto - - psycopg2-binary + - psycopg2>=2.5.4 - PyYAML - requests - six >= 1.7 @@ -36,5 +41,6 @@ requirements: - prettytable>=0.7 - tzlocal - python-dateutil - - psutil + - psutil>=2.0.0 - cdiff + - psycopg2-binary diff --git a/tasks/configure.yml b/tasks/configure.yml index 63b1bc0..1877ff6 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -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: @@ -34,15 +34,26 @@ 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 @@ -50,12 +61,15 @@ - 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 \ No newline at end of file +- name: trigger patroni restart + command: /bin/true + notify: start postgres cluster + +- meta: flush_handlers diff --git a/tasks/install.yml b/tasks/install.yml index 54a7671..f542368 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index fa6885a..caf857b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -5,4 +5,4 @@ - include: users.yml -- include: tests.yml \ No newline at end of file +- include: tests.yml diff --git a/tasks/tests.yml b/tasks/tests.yml index cc370c6..2c1f5b9 100644 --- a/tasks/tests.yml +++ b/tasks/tests.yml @@ -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 @@ -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 @@ -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" @@ -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" diff --git a/tasks/users.yml b/tasks/users.yml index f0780da..56429c2 100644 --- a/tasks/users.yml +++ b/tasks/users.yml @@ -21,6 +21,7 @@ leader_node: "{{ inventory_hostname }}" - name: create databases + become_user: "{{ ansible_user }}" ignore_errors: yes postgresql_db: name: "{{ item.value.db }}" @@ -32,6 +33,7 @@ when: "inventory_hostname == leader_node" - name: create users + become_user: "{{ ansible_user }}" ignore_errors: yes postgresql_user: name: "{{ item.key }}" diff --git a/templates/patroni.service.j2 b/templates/patroni.service.j2 index 03993f7..6ec96ea 100644 --- a/templates/patroni.service.j2 +++ b/templates/patroni.service.j2 @@ -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 diff --git a/tests/playbook.yml b/tests/playbook.yml index 73ddb19..44bd62a 100644 --- a/tests/playbook.yml +++ b/tests/playbook.yml @@ -18,5 +18,3 @@ - hosts: all roles: - ansible-postgresql - vars: - cassandra_alter_password: "testpassword"