ansible (really) loves postgresql · 2019-10-24 · ansible (really) loves postgresql pgconf.eu...

62

Upload: others

Post on 27-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible (really) loves PostgreSQLPGConf.EU 2019

Cédric Villemain [email protected]

16 October 2019

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 1 / 46

Page 2: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Cédric Villemain

PostgreSQL Development & Expertise

Training

24x7 Support & Remote DBA

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 2 / 46

Page 3: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

2ndQuadrant

Major Sponsor

• 9.2 Refactoring checkpoint & group commit

• 9.3 Event Triggers

• 9.4 Replication Slot

• 9.5 Block Range INdex

• 9.6 Datawarehouse performance improvements

• 10 CREATE STATISTICS, Logical replication

• 11 Procedures, Partitionning

• 12 Generated Columns

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 3 / 46

Page 4: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Agenda

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 4 / 46

Page 5: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

PostgreSQL

• deploy

• con�gure

• recon�gure

• update

• upgrade

• backup

• restore

• replicate

• monitor

• test

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 5 / 46

Page 6: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

PostgreSQL

• deploy

• con�gure

• recon�gure

• update

• upgrade

• backup

• restore

• replicate

• monitor

• test

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 5 / 46

Page 7: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

PostgreSQL

• deploy

• con�gure

• recon�gure

• update

• upgrade

• backup

• restore

• replicate

• monitor

• test

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 5 / 46

Page 8: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Problem ?

• handmade scripts

• packages

• deployment tools

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 6 / 46

Page 9: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Problem ?

• handmade scripts

• packages

• deployment tools

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 6 / 46

Page 10: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

A Solution

https://slideshare.net/GulcinYildirim/

managing-postgres-with-ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 7 / 46

Page 11: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Automate

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 8 / 46

Page 12: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 9 / 46

Page 13: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Inventory

$ echo localhost > /etc/ansible/hosts

Command line

$ ansible all �-module-name postgresql_info \

�-args 'login_user=cedric \

filter=version'

Output

localhost | SUCCESS => {

[...]

"version": {

"major": 12,

"minor": 0

}

}

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 10 / 46

Page 14: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Inventory

$ echo localhost > /etc/ansible/hosts

Command line

$ ansible all �-module-name postgresql_info \

�-args 'login_user=cedric \

filter=version'

Output

localhost | SUCCESS => {

[...]

"version": {

"major": 12,

"minor": 0

}

}

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 10 / 46

Page 15: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Inventory

$ echo localhost > /etc/ansible/hosts

Command line

$ ansible all �-module-name postgresql_info \

�-args 'login_user=cedric \

filter=version'

Output

localhost | SUCCESS => {

[...]

"version": {

"major": 12,

"minor": 0

}

}

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 10 / 46

Page 16: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Inventory

$ echo localhost > /etc/ansible/hosts

Command line

$ ansible all �-module-name postgresql_info \

�-args 'login_user=cedric \

filter=version'

Output

localhost | SUCCESS => {

[...]

"version": {

"major": 12,

"minor": 0

}

}

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 10 / 46

Page 17: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Inventory

$ echo localhost > /etc/ansible/hosts

Command line

$ ansible all �-module-name postgresql_info \

�-args 'login_user=cedric \

filter=version'

Output

localhost | SUCCESS => {

[...]

"version": {

"major": 12,

"minor": 0

}

}

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 10 / 46

Page 18: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wait!

What about �Playbooks� ?

Command line

$ ansible-playbook my_playbook.yml

Better than

#!/bin/bash

# get info about PostgreSQL

ansible all ---module-name postgresql_info \\

---args 'login_user=cedric \\

filter=version'

# do something else

ansible all ---module-name .....

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 11 / 46

Page 19: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wait!

What about �Playbooks� ?

Command line

$ ansible-playbook my_playbook.yml

Better than

#!/bin/bash

# get info about PostgreSQL

ansible all ---module-name postgresql_info \\

---args 'login_user=cedric \\

filter=version'

# do something else

ansible all ---module-name .....

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 11 / 46

Page 20: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wait!

What about �Playbooks� ?

Command line

$ ansible-playbook my_playbook.yml

Better than

#!/bin/bash

# get info about PostgreSQL

ansible all ---module-name postgresql_info \\

---args 'login_user=cedric \\

filter=version'

# do something else

ansible all ---module-name .....

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 11 / 46

Page 21: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 12 / 46

Page 22: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 13 / 46

Page 23: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 13 / 46

Page 24: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 13 / 46

Page 25: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Command line

$ ansible-playbook pg_version.yml

Output

PLAY [all] *****************************************

TASK [Gathering Facts] *****************************

ok: [localhost]

TASK [postgresql_info] *****************************

ok: [localhost]

PLAY RECAP *****************************************

localhost : ok=2 changed=0 unreachable=0 failed=0 [...]

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 14 / 46

Page 26: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: get pg version

Command line

$ ansible-playbook pg_version.yml

Output

PLAY [all] *****************************************

TASK [Gathering Facts] *****************************

ok: [localhost]

TASK [postgresql_info] *****************************

ok: [localhost]

PLAY RECAP *****************************************

localhost : ok=2 changed=0 unreachable=0 failed=0 [...]

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 14 / 46

Page 27: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Serialized tasks execution

• wait for task completion for each host

• execute tasks on all hosts, in parallel

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 15 / 46

Page 28: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: really get pg version !

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

register: pg_version

- debug: var=pg_version.version.major

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 16 / 46

Page 29: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: really get pg version !

Output

PLAY [all] *****************************************

TASK [Gathering Facts] *****************************

ok: [localhost]

TASK [postgresql_info] *****************************

ok: [localhost]

TASK [debug] ***************************************

ok: [localhost] => {

"pg_version.version.major": "12"

}

PLAY RECAP *****************************************

localhost : ok=3 changed=0 unreachable=0 failed=0 [...]

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 17 / 46

Page 30: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: De�ne fact

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

register: pg_version

- set_fact:

pg_major: "{{pg_version.version.major}}"

- debug: var=pg_major

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 18 / 46

Page 31: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: De�ne fact

The Playbook: pg_version.yml��-

- hosts: all

tasks:

- postgresql_info:

login_user: cedric

filter: version

register: pg_version

- set_fact:

pg_major: "{{pg_version.version.major}}"

- debug: var=pg_major

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 18 / 46

Page 32: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: De�ne fact

Output

PLAY [all] *****************************************

[...]

TASK [set_fact] ************************************

ok: [localhost]

TASK [debug] ***************************************

ok: [localhost] => {

"pg_major": "12"

}

PLAY RECAP *****************************************

localhost : ok=4 changed=0 unreachable=0 failed=0 [...]

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 19 / 46

Page 33: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: Use fact

The Playbook: pg_version.yml

[...]

- name: Ensure we do not recycle WAL file (COW FS)

postgresql_set:

login_user: cedric

name: wal_recycle

value: False|string

when:

pg_major|int > 11

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 20 / 46

Page 34: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: Use fact

The Playbook: pg_version.yml

- name: Reload PostgreSQL if 12+

postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_reload_conf()

when:

pg_major|int > 11

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 21 / 46

Page 35: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: Use fact

Output

PLAY [all] *****************************************

[...]

TASK [Ensure we do not recycle WAL file (COW FS)] **

changed: [localhost]

TASK [Reload PostgreSQL if 12+] ********************

ok: [localhost]

PLAY RECAP *****************************************

localhost : ok=5 changed=1 unreachable=0 failed=0 [...]

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 22 / 46

Page 36: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wonderful...

c©2014 Shelly •• . Licensed under

CC-BY

• If not in a CoW FS, we don't want to

apply

• If parameter has not changed, we don't

want to reload

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 23 / 46

Page 37: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wonderful...

c©2014 Shelly •• . Licensed under

CC-BY

• If not in a CoW FS, we don't want to

apply

• If parameter has not changed, we don't

want to reload

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 23 / 46

Page 38: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Variables

De�ne Variable- hosts: all

vars:

wal_recycle: True

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 24 / 46

Page 39: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Handler & Notify

De�ne Handler & Call Notify

handlers:

- name: reload PostgreSQL

postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_reload_conf()

tasks:

- name: Ensure we do not recycle WAL file (COW FS)

postgresql_set:

login_user: cedric

name: wal_recycle

value: "{{wal_recycle|string}}"

when:

pg_major|int > 11

notify: reload PostgreSQL

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 25 / 46

Page 40: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Handler & Notify

De�ne Handler & Call Notify

handlers:

- name: reload PostgreSQL

postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_reload_conf()

tasks:

- name: Ensure we do not recycle WAL file (COW FS)

postgresql_set:

login_user: cedric

name: wal_recycle

value: "{{wal_recycle|string}}"

when:

pg_major|int > 11

notify: reload PostgreSQL

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 25 / 46

Page 41: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 26 / 46

Page 42: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

About Roles

• Variables

• Tasks

• Handlers

• Templates

• ...

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 27 / 46

Page 43: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Simpler playbook

pg_version.yml---

- hosts: all

vars:

wal_recycle: False

roles:

- pg_cow

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 28 / 46

Page 44: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Role pg_cow (vars)

roles/pg_cow/vars/main.yml---

vars:

wal_recycle: True

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 29 / 46

Page 45: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Role pg_cow (tasks)

roles/pg_cow/tasks/main.yml---

- postgresql_info:

login_user: cedric

filter: version

register: pg_version

- set_fact:

pg_major: "{{pg_version.version.major}}"

- name: Ensure we do not recycle WAL file (COW FS)

postgresql_set:

login_user: cedric

name: wal_recycle

value: "{{wal_recycle|string}}"

when:

pg_major|int > 11

notify: reload PostgreSQL

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 30 / 46

Page 46: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Role pg_cow (one handler)

roles/pg_cow/handlers/main.yml---

- name: reload PostgreSQL

postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_reload_conf()

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 31 / 46

Page 47: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Role pg_cow (another handler)

roles/pg_cow/handlers/main.yml---

- name: reload PostgreSQL

service:

name: postgresql

state: reloaded

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 32 / 46

Page 48: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Wait !

What is a Module ?

• External Library

• Compliant with Ansible

• Called and con�gured inside tasks.

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 33 / 46

Page 49: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 34 / 46

Page 50: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: start several PostgreSQL dockers

The Playbook: pg_docker.yml---

- hosts: all

vars:

n: 6

tasks:

- name: Create & Start container on port '943[1-n]'

docker_container:

name: "db_test_{{ item|string }}"

image: "postgres:latest"

published_ports: "943{{ item|int }}:5432"

networks:

- name: bridge

volumes:

- /tmp/docker:/tmp/docker

loop: "{{ range(1, (n|int+1))|list }}"

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 35 / 46

Page 51: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: start several PostgreSQL docker

Command Line and Variableansible-playbook --extra-vars n='2' pg_docker.yml

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 36 / 46

Page 52: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: �Naive� Rolling Upgrade

The Playbook: pg_rolling.yml---

- hosts: all

serial: 1

tasks:

- postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_is_in_recovery() p(pg_is_in)

register: x

- set_fact:

is_standby: "{{x.query_result[0].pg_is_in|bool}}"

- package:

name: postgresql-12

state: latest

when: is_standby == true

[...]Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 37 / 46

Page 53: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Demo: �Naive� Rolling Upgrade

The Playbook: pg_rolling.yml

[...]

- hosts: all

tasks:

- postgresql_query:

login_user: cedric

db: postgres

query: SELECT pg_is_in_recovery() p(pg_is_in)

register: x

- set_fact:

is_standby: "{{x.query_result[0].pg_is_in|bool}}"

- package:

name: postgresql-12

state: latest

when: is_standby == false

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 38 / 46

Page 54: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

More than once

Introduction to Ansible

Writing a Playbook

Writing a Role

More Examples

Contributing to Ansible

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 39 / 46

Page 55: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible 2.6 modules

Cluster

• postgresql_user

• postgresql_db

Database

• postgresql_lang

• postgresql_ext

• postgresql_schema

• postgresql_privs

https://docs.ansible.com/ansible/2.6/modules/list_of_

database_modules.html?highlight=postgresql

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 40 / 46

Page 56: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible 2.8 modules

Cluster

• postgresql_ping

• postgresql_info

• postgresql_pg_hba

• postgresql_set

• postgresql_tablespace

• postgresql_user

• postgresql_membership

• postgresql_db

• postgresql_slot

Database

• postgresql_lang

• postgresql_ext

• postgresql_schema

• postgresql_table

• postgresql_idx

• postgresql_owner

• postgresql_privs

• postgresql_query

https://docs.ansible.com/ansible/latest/

modules/list_of_database_modules.html?

highlight=postgresql

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 41 / 46

Page 57: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible model

• Core application

• Modules

• Plugins

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 42 / 46

Page 58: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible Galaxy

A place for sharing:

• Modules

• Plugins

• Playbooks

• Roles

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 43 / 46

Page 59: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Ansible Galaxy

A place for sharing:

• Modules

• Plugins

• Playbooks

• Roles

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 43 / 46

Page 60: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Search in Ansible Galaxy

• PostgreSQL: 1 collection (debops) & 555 roles !

• PostGIS: 21 roles

• Barman: 10 roles

• pgBouncer: 10 roles

• BDR: 4 roles

• pglogical: 2 roles

• pgBackRest: 1 role

• pgpool: 1 role

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 44 / 46

Page 61: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Your own way

You can also share

anywhere, at least

playbooks and roles.

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 45 / 46

Page 62: Ansible (really) loves PostgreSQL · 2019-10-24 · Ansible (really) loves PostgreSQL PGConf.EU 2019 Cédric Villemain cedric@2ndQuadrant.com 16 October 2019 Cédric Villemain cedric@2ndQuadrant.com

Questions ?

now or later!

Cédric Villemain [email protected] Ansible (really) loves PostgreSQL 16 October 2019 46 / 46