Installing EPAS 18 and EDB Failover Manager with the RedHat Ansible Automation Platform (AAP)
In my earlier posts I installed EDB Postgres Advanced Server 18 (EPAS) and EDB Failover Manager (EFM) on RHEL 10 by hand. This post does the same with the RedHat Ansible Automation Platform (AAP): three playbooks install EPAS on the primary, create the standby with pg_basebackup and set up EFM with a virtual IP. An AAP workflow runs them in the right order, and all passwords and the EDB repository token come from HashiCorp Vault.
This post builds on the first steps post (inventory, project, configuration as code) and the HashiCorp Vault post (Vault lookup credential and the credentials “EPAS Superuser” and “EDB Repository”).
The Lab Environment
- lin2 (RHEL 9.7): AAP 2.6, Gitea and HashiCorp Vault
- lin8 (11.1.1.196): EPAS primary
- lin7 (11.1.1.195): EPAS standby
- lin6 (11.1.1.186): EFM witness
- virtual IP 11.1.1.200 on the interface enp0s3, gateway 11.1.1.222
lin6, lin7 and lin8 are freshly installed with RHEL 10.1 and registered with the RedHat subscription. If the VMs were reinstalled after the first steps post, the step “Preparing the Managed Nodes” from that post has to be repeated (do not create a new ssh keypair for the user aap when you are asked to), so that AAP can log in as the user ansible.
Storing the Passwords of the Cluster Users in Vault
Like in the manual setup, the standby replicates with the user repuser and EFM connects with its own user efm. We generate their passwords in Vault. As the user aap on lin2, with VAULT_ADDR set and logged in with the root token as in the Vault post:
source ~/aap_env.sh
export VAULT_TOKEN=$(jq -r .root_token ~/vault_init.json)
vault kv put lab/epas/repuser username=repuser password=$(openssl rand -base64 18)
vault kv put lab/epas/efm username=efm password=$(openssl rand -base64 18)
unset VAULT_TOKEN
Playbook 1: Installing EPAS on the Primary
The first playbook prepares all three nodes (EPEL, EDB repository with the token from Vault and, if firewalld is running, the firewall ports; in my lab firewalld is disabled) and installs the EPAS packages on lin7 and lin8. On the primary it initializes the database cluster, configures postgresql.conf and pg_hba.conf, sets the password of enterprisedb and creates the users repuser and efm with the same privileges as in the manual EFM post. The passwords are passed as psql variables on stdin, so they never show up in the process list or in the job output. As in the manual setup, efm has the REPLICATION attribute, so it may also connect for replication. WAL archiving is already switched on with the dummy archive_command /bin/true. This way pgBackRest can be activated later with a reload instead of a restart, because restarting the primary while EFM is running could trigger a failover:
# run as aap on lin2
cd ~/aap-demo
cat > playbooks/epas_install.yml <<'EOF'
---
- name: Prepare all nodes (EPEL, EDB repository, firewall)
hosts: dbservers:witness
gather_facts: false
become: true
vars:
edb_repo_name: enterprise
tasks:
- name: Install the EPEL repository
ansible.builtin.dnf:
name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm
disable_gpg_check: true
state: present
- name: Set up the EDB repository with the token from Vault
ansible.builtin.shell:
cmd: curl -1sSLf "https://downloads.enterprisedb.com/{{ edb_repo_token }}/{{ edb_repo_name }}/setup.rpm.sh" | bash
creates: /etc/yum.repos.d/enterprisedb-{{ edb_repo_name }}.repo
no_log: true
- name: Check whether firewalld is running
ansible.builtin.service_facts:
- name: Open the ports for EPAS (5444) and EFM (7800, 7809) if firewalld is running
ansible.posix.firewalld:
port: "{{ item }}"
permanent: true
immediate: true
state: enabled
loop: [5444/tcp, 7800/tcp, 7809/tcp]
when: ansible_facts.services.get('firewalld.service', {}).get('state') == 'running'
- name: Install EPAS 18 and configure the primary
hosts: dbservers
gather_facts: false
become: true
vars:
pg_bin: /usr/edb/as18/bin
pg_data: /var/lib/edb/as18/data
lab_network: 11.1.1.0/24
tasks:
- name: Install the EPAS server package
ansible.builtin.dnf:
name: edb-as18-server
state: present
- name: Write .pgpass for the replication users (needed by the standby)
ansible.builtin.copy:
dest: /var/lib/edb/.pgpass
owner: enterprisedb
group: enterprisedb
mode: "0600"
content: |
*:5444:replication:repuser:{{ epas_repuser_password }}
*:5444:replication:efm:{{ epas_efm_password }}
no_log: true
- name: Configure the primary
when: inventory_hostname in groups['primary']
block:
- name: Initialize the database cluster
ansible.builtin.command:
cmd: "{{ pg_bin }}/edb-as-18-setup initdb"
creates: "{{ pg_data }}/PG_VERSION"
environment:
PGSETUP_INITDB_OPTIONS: "-E UTF-8"
- name: Set parameters in postgresql.conf
ansible.builtin.lineinfile:
path: "{{ pg_data }}/postgresql.conf"
regexp: "^#?{{ item.key }} ="
line: "{{ item.key }} = {{ item.value }}"
loop:
- {key: listen_addresses, value: "'*'"}
- {key: wal_log_hints, value: "on"}
- {key: archive_mode, value: "on"}
- {key: archive_command, value: "'/bin/true'"}
register: pg_conf
- name: Write pg_hba.conf
ansible.builtin.copy:
dest: "{{ pg_data }}/pg_hba.conf"
owner: enterprisedb
group: enterprisedb
mode: "0600"
content: |
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 scram-sha-256
host all all {{ lab_network }} scram-sha-256
host replication repuser {{ lab_network }} scram-sha-256
host replication efm {{ lab_network }} scram-sha-256
register: pg_hba
- name: Start EPAS (restart if the configuration was changed)
ansible.builtin.systemd:
name: edb-as-18
state: "{{ 'restarted' if pg_conf.changed or pg_hba.changed else 'started' }}"
enabled: true
- name: Set the passwords and create the users repuser and efm
ansible.builtin.command:
argv:
- "{{ pg_bin }}/psql"
- -d
- edb
- -v
- ON_ERROR_STOP=1
- -v
- "su_pw={{ epas_superuser_password }}"
- -v
- "rep_pw={{ epas_repuser_password }}"
- -v
- "efm_pw={{ epas_efm_password }}"
stdin: |
ALTER ROLE enterprisedb PASSWORD :'su_pw';
SELECT 'CREATE ROLE repuser' WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'repuser')\gexec
ALTER ROLE repuser WITH LOGIN REPLICATION PASSWORD :'rep_pw';
SELECT 'CREATE ROLE efm' WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'efm')\gexec
ALTER ROLE efm WITH LOGIN REPLICATION PASSWORD :'efm_pw';
GRANT pg_read_all_settings TO efm;
GRANT EXECUTE ON FUNCTION pg_current_wal_lsn() TO efm;
GRANT EXECUTE ON FUNCTION pg_last_wal_replay_lsn() TO efm;
GRANT EXECUTE ON FUNCTION pg_wal_replay_resume() TO efm;
GRANT EXECUTE ON FUNCTION pg_wal_replay_pause() TO efm;
GRANT EXECUTE ON FUNCTION pg_reload_conf() TO efm;
become_user: enterprisedb
no_log: true
EOF
Playbook 2: Creating the Standby
The second playbook copies the primary to the standby with pg_basebackup. The option -R writes the replication settings and creates standby.signal. If standby.signal already exists, the copy is skipped, so the playbook can safely run again:
# run as aap on lin2
cat > playbooks/epas_standby.yml <<'EOF'
---
- name: Create the standby with pg_basebackup
hosts: standby
gather_facts: false
become: true
vars:
pg_bin: /usr/edb/as18/bin
pg_data: /var/lib/edb/as18/data
tasks:
- name: Check whether the standby already exists
ansible.builtin.stat:
path: "{{ pg_data }}/standby.signal"
register: standby_signal
- name: Build the standby
when: not standby_signal.stat.exists
block:
- name: Stop EPAS
ansible.builtin.systemd:
name: edb-as-18
state: stopped
- name: Remove an old data directory
ansible.builtin.file:
path: "{{ pg_data }}"
state: absent
- name: Copy the primary with pg_basebackup
ansible.builtin.command:
cmd: >-
{{ pg_bin }}/pg_basebackup -h {{ groups['primary'][0] }} -p 5444 -U repuser
-D {{ pg_data }} -R -X stream -c fast
become_user: enterprisedb
- name: Start EPAS
ansible.builtin.systemd:
name: edb-as-18
state: started
enabled: true
- name: Check that the standby is in recovery mode
ansible.builtin.command:
argv: ["{{ pg_bin }}/psql", "-d", "edb", "-Atc", "select pg_is_in_recovery()"]
become_user: enterprisedb
register: recovery
changed_when: false
failed_when: recovery.stdout != 't'
EOF
Playbook 3: Installing EFM
The third playbook installs Java (OpenJDK 21, EFM 5.x needs Java 17 or later) and EFM 5.5 on all three nodes, encrypts the password of the user efm with efm encrypt –from-env, fills efm.properties and efm.nodes and starts the agents in the order primary, standby, witness. As in the manual setup, the notifications are written to a log file by a small script instead of being sent by email:
# run as aap on lin2
cat > playbooks/efm_install.yml <<'EOF'
---
- name: Install and configure EFM on all nodes
hosts: dbservers:witness
gather_facts: true
become: true
vars:
efm_version: "5.5"
efm_cluster: efm
efm_conf: /etc/edb/efm-{{ efm_version }}
efm_bin: /usr/edb/efm-{{ efm_version }}/bin
vip: 11.1.1.200
vip_interface: enp0s3
vip_prefix: 24
ping_ip: 11.1.1.222
tasks:
- name: Install Java and the EFM package (EFM 5.x needs Java 17 or later)
ansible.builtin.dnf:
name:
- java-21-openjdk-headless
- edb-efm{{ efm_version | replace('.', '') }}
state: present
- name: Notification script (writes the EFM notifications to a logfile)
ansible.builtin.copy:
dest: /usr/local/bin/efm_notification.sh
mode: "0755"
content: |
#!/bin/bash
echo "$(date '+%F %T') $1: $2" >> /var/log/efm-{{ efm_version }}/notifications.log
- name: Create the properties file from the template
ansible.builtin.copy:
src: "{{ efm_conf }}/efm.properties.in"
dest: "{{ efm_conf }}/{{ efm_cluster }}.properties"
remote_src: true
force: false
owner: efm
group: efm
mode: "0644"
- name: Encrypt the password of the user efm
ansible.builtin.command:
cmd: "{{ efm_bin }}/efm encrypt {{ efm_cluster }} --from-env"
environment:
EFMPASS: "{{ epas_efm_password }}"
register: efm_encrypt
changed_when: false
no_log: true
- name: Set the EFM properties (the file must be readable for enterprisedb)
ansible.builtin.lineinfile:
path: "{{ efm_conf }}/{{ efm_cluster }}.properties"
owner: efm
group: efm
mode: "0644"
regexp: "^{{ item.key | regex_escape }}="
line: "{{ item.key }}={{ item.value }}"
loop: "{{ efm_properties | dict2items }}"
loop_control:
label: "{{ item.key }}"
vars:
efm_properties:
db.user: efm
db.password.encrypted: "{{ efm_encrypt.stdout_lines[-1] }}"
db.port: 5444
db.database: edb
db.service.owner: enterprisedb
db.service.name: edb-as-18
db.bin: /usr/edb/as18/bin
db.data.dir: /var/lib/edb/as18/data
user.email: ""
script.notification: /usr/local/bin/efm_notification.sh
bind.address: "{{ ansible_facts.default_ipv4.address }}:7800"
is.witness: "{{ 'true' if inventory_hostname in groups['witness'] else 'false' }}"
ping.server.ip: "{{ ping_ip }}"
auto.allow.hosts: "true"
stable.nodes.file: "true"
virtual.ip: "{{ vip }}"
virtual.ip.interface: "{{ vip_interface }}"
virtual.ip.prefix: "{{ vip_prefix }}"
virtual.ip.single: "true"
application.name: "{{ inventory_hostname_short }}"
- name: Write the nodes file with all cluster members
ansible.builtin.copy:
dest: "{{ efm_conf }}/{{ efm_cluster }}.nodes"
owner: efm
group: efm
mode: "0640"
content: "{{ (groups['dbservers'] + groups['witness']) | map('extract', hostvars, ['ansible_facts', 'default_ipv4', 'address']) | map('regex_replace', '$', ':7800') | join(' ') }}"
- name: Start EFM on the primary, the standby and the witness (in this order)
ansible.builtin.systemd:
name: edb-efm-{{ efm_version }}
state: started
enabled: true
delegate_to: "{{ item }}"
run_once: true
loop: "{{ groups['primary'] + groups['standby'] + groups['witness'] }}"
- name: Get the cluster status
ansible.builtin.command: "{{ efm_bin }}/efm cluster-status {{ efm_cluster }}"
register: efm_status
changed_when: false
delegate_to: "{{ groups['primary'][0] }}"
run_once: true
- name: Show the cluster status
ansible.builtin.debug:
var: efm_status.stdout_lines
run_once: true
EOF
git add playbooks
git commit -m "Add EPAS and EFM playbooks"
git push
Configuring AAP
The playbook config/aap_epas.yml adds a credential type and a credential for the passwords of repuser and efm (looked up in Vault), the inventory groups primary and standby, the three job templates and a workflow which runs them one after the other. Before the job templates are created, the project is synchronized, so that AAP knows the new playbooks. At the end the workflow is launched:
# run as aap on lin2
cat > config/aap_epas.yml <<'EOF'
---
- name: Configure the EPAS / EFM installation in AAP
hosts: localhost
connection: local
gather_facts: false
vars:
org: JBITC
templates:
- {name: EPAS - Install, playbook: playbooks/epas_install.yml}
- {name: EPAS - Create Standby, playbook: playbooks/epas_standby.yml}
- {name: EFM - Install, playbook: playbooks/efm_install.yml}
tasks:
- name: Credential type for the passwords of repuser and efm
ansible.controller.credential_type:
name: EPAS Cluster Users
kind: cloud
inputs:
fields:
- {id: repuser_password, type: string, label: Password of repuser, secret: true}
- {id: efm_password, type: string, label: Password of efm, secret: true}
injectors:
extra_vars:
epas_repuser_password: !unsafe '{{ repuser_password }}'
epas_efm_password: !unsafe '{{ efm_password }}'
- name: Create the credential
ansible.controller.credential:
name: EPAS Cluster Users
organization: "{{ org }}"
credential_type: EPAS Cluster Users
inputs: {}
- name: Read the passwords from Vault
ansible.controller.credential_input_source:
input_field_name: "{{ item.field }}"
target_credential: EPAS Cluster Users
source_credential: Lab Vault
metadata:
secret_backend: lab
secret_path: "{{ item.path }}"
secret_key: password
loop:
- {field: repuser_password, path: epas/repuser}
- {field: efm_password, path: epas/efm}
- name: Create the groups primary and standby
ansible.controller.group:
name: "{{ item.name }}"
inventory: Lab
hosts: "{{ item.hosts }}"
loop:
- {name: primary, hosts: [lin8.fritz.box]}
- {name: standby, hosts: [lin7.fritz.box]}
- name: Synchronize the project (new playbooks in Git)
ansible.controller.project_update:
project: aap-demo
wait: true
- name: Create the job templates
ansible.controller.job_template:
name: "{{ item.name }}"
organization: "{{ org }}"
project: aap-demo
playbook: "{{ item.playbook }}"
inventory: Lab
credentials: [Lab SSH, EPAS Superuser, EPAS Cluster Users, EDB Repository]
loop: "{{ templates }}"
- name: Create the workflow
ansible.controller.workflow_job_template:
name: EPAS - Install Cluster with EFM
organization: "{{ org }}"
inventory: Lab
destroy_current_nodes: true
workflow_nodes:
- identifier: epas
unified_job_template: {name: EPAS - Install, type: job_template, organization: {name: "{{ org }}"}}
related:
success_nodes: [{identifier: standby}]
- identifier: standby
unified_job_template: {name: EPAS - Create Standby, type: job_template, organization: {name: "{{ org }}"}}
related:
success_nodes: [{identifier: efm}]
- identifier: efm
unified_job_template: {name: EFM - Install, type: job_template, organization: {name: "{{ org }}"}}
- name: Launch the workflow and wait until it has finished
ansible.controller.workflow_launch:
workflow_template: EPAS - Install Cluster with EFM
wait: true
timeout: 3600
EOF
git add config
git commit -m "Add AAP configuration for EPAS and EFM"
git push
# load the settings (needed in a new shell) and run it
source ~/aap_env.sh
aap_play config/aap_epas.yml
Sample Output (click to expand):
[aap@lin2 aap-demo]$ # run as aap on lin2
cat > config/aap_epas.yml <<'EOF'
---
- name: Configure the EPAS / EFM installation in AAP
hosts: localhost
connection: local
gather_facts: false
vars:
org: JBITC
templates:
- {name: EPAS - Install, playbook: playbooks/epas_install.yml}
- {name: EPAS - Create Standby, playbook: playbooks/epas_standby.yml}
- {name: EFM - Install, playbook: playbooks/efm_install.yml}
tasks:
- name: Credential type for the passwords of repuser and efm
ansible.controller.credential_type:
name: EPAS Cluster Users
kind: cloud
inputs:
fields:
- {id: repuser_password, type: string, label: Password of repuser, secret: true}
- {id: efm_password, type: string, label: Password of efm, secret: true}
injectors:
extra_vars:
epas_repuser_password: !unsafe '{{ repuser_password }}'
epas_efm_password: !unsafe '{{ efm_password }}'
- name: Create the credential
ansible.controller.credential:
name: EPAS Cluster Users
organization: "{{ org }}"
credential_type: EPAS Cluster Users
inputs: {}
- name: Read the passwords from Vault
ansible.controller.credential_input_source:
input_field_name: "{{ item.field }}"
target_credential: EPAS Cluster Users
source_credential: Lab Vault
metadata:
secret_backend: lab
secret_path: "{{ item.path }}"
secret_key: password
loop:
- {field: repuser_password, path: epas/repuser}
- {field: efm_password, path: epas/efm}
- name: Create the groups primary and standby
ansible.controller.group:
name: "{{ item.name }}"
inventory: Lab
hosts: "{{ item.hosts }}"
loop:
- {name: primary, hosts: [lin8.fritz.box]}
- {name: standby, hosts: [lin7.fritz.box]}
- name: Synchronize the project (new playbooks in Git)
ansible.controller.project_update:
project: aap-demo
wait: true
- name: Create the job templates
ansible.controller.job_template:
name: "{{ item.name }}"
organization: "{{ org }}"
project: aap-demo
playbook: "{{ item.playbook }}"
inventory: Lab
credentials: [Lab SSH, EPAS Superuser, EPAS Cluster Users, EDB Repository]
loop: "{{ templates }}"
- name: Create the workflow
ansible.controller.workflow_job_template:
name: EPAS - Install Cluster with EFM
organization: "{{ org }}"
inventory: Lab
destroy_current_nodes: true
workflow_nodes:
- identifier: epas
unified_job_template: {name: EPAS - Install, type: job_template, organization: {name: "{{ org }}"}}
related:
success_nodes: [{identifier: standby}]
- identifier: standby
unified_job_template: {name: EPAS - Create Standby, type: job_template, organization: {name: "{{ org }}"}}
related:
success_nodes: [{identifier: efm}]
- identifier: efm
unified_job_template: {name: EFM - Install, type: job_template, organization: {name: "{{ org }}"}}
- name: Launch the workflow and wait until it has finished
ansible.controller.workflow_launch:
workflow_template: EPAS - Install Cluster with EFM
wait: true
timeout: 3600
EOF
git add config
git commit -m "Add AAP configuration for EPAS and EFM"
git push
# load the settings (needed in a new shell) and run it
source ~/aap_env.sh
aap_play config/aap_epas.yml
[main 0cb4e07] Add AAP configuration for EPAS and EFM
1 file changed, 92 insertions(+)
create mode 100644 config/aap_epas.yml
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.33 KiB | 1.33 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To http://lin2.fritz.box:3000/gitadmin/aap-demo.git
0c109ba..0cb4e07 main -> main
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Configure the EPAS / EFM installation in AAP] ****************************
TASK [Credential type for the passwords of repuser and efm] ********************
changed: [localhost]
TASK [Create the credential] ***************************************************
changed: [localhost]
TASK [Read the passwords from Vault] *******************************************
changed: [localhost] => (item={'field': 'repuser_password', 'path': 'epas/repuser'})
changed: [localhost] => (item={'field': 'efm_password', 'path': 'epas/efm'})
TASK [Create the groups primary and standby] ***********************************
changed: [localhost] => (item={'name': 'primary', 'hosts': ['lin8.fritz.box']})
changed: [localhost] => (item={'name': 'standby', 'hosts': ['lin7.fritz.box']})
TASK [Synchronize the project (new playbooks in Git)] **************************
changed: [localhost]
TASK [Create the job templates] ************************************************
changed: [localhost] => (item={'name': 'EPAS - Install', 'playbook': 'playbooks/epas_install.yml'})
changed: [localhost] => (item={'name': 'EPAS - Create Standby', 'playbook': 'playbooks/epas_standby.yml'})
changed: [localhost] => (item={'name': 'EFM - Install', 'playbook': 'playbooks/efm_install.yml'})
TASK [Create the workflow] *****************************************************
changed: [localhost]
TASK [Launch the workflow and wait until it has finished] **********************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=8 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[aap@lin2 aap-demo]$

Checking the Cluster
On lin8 we check the EFM cluster, the virtual IP and the replication (as root):
/usr/edb/efm-5.5/bin/efm cluster-status efm
ip -br addr show enp0s3
sudo -iu enterprisedb psql -d edb -c "select application_name, client_addr, state, sync_state from pg_stat_replication"
Sample Output (click to expand):
[root@lin8 ~]# /usr/edb/efm-5.5/bin/efm cluster-status efm
ip -br addr show enp0s3
sudo -iu enterprisedb psql -d edb -c "select application_name, client_addr, state, sync_state from pg_stat_replication"
Cluster Status: efm
Agent Type Address DB VIP
----------------------------------------------------------------
Witness 11.1.1.186 N/A 11.1.1.200
Standby 11.1.1.195 UP 11.1.1.200
Primary 11.1.1.196 UP 11.1.1.200*
Allowed node host list:
11.1.1.186 11.1.1.195 11.1.1.196
Membership coordinator: 11.1.1.186
Standby priority host list:
11.1.1.195
Promote Status:
DB Type Address WAL Received LSN WAL Replayed LSN Info
---------------------------------------------------------------------------
Primary 11.1.1.196 0/5004068
Standby 11.1.1.195 0/5004068 0/5004068
Standby database(s) in sync with primary. It is safe to promote.
enp0s3 UP 11.1.1.196/24 11.1.1.200/24 fe80::a00:27ff:fe2e:2dd8/64
application_name | client_addr | state | sync_state
------------------+-------------+-----------+------------
walreceiver | 11.1.1.195 | streaming | async
(1 row)
[root@lin8 ~]#
Short-lived Database Users from Vault
Now that EPAS is running, we can also use the database secrets engine of Vault. It creates database users on demand which expire automatically. Vault connects with enterprisedb through the virtual IP, so it always reaches the current primary. As the user aap on lin2 (with the root token):
# run as aap on lin2
source ~/aap_env.sh
export VAULT_TOKEN=$(jq -r .root_token ~/vault_init.json)
vault secrets enable database
# connection to EPAS (the user and password are taken from the KV secret)
vault write database/config/epas plugin_name=postgresql-database-plugin allowed_roles=readonly connection_url="postgresql://{{username}}:{{password}}@11.1.1.200:5444/edb" username=enterprisedb password="$(vault kv get -field=password lab/epas/enterprisedb)"
# role readonly: users which can read all data and expire after 1 hour
cat > /tmp/readonly.sql <<'EOF'
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';
GRANT pg_read_all_data TO "{{name}}";
EOF
vault write database/roles/readonly db_name=epas creation_statements=@/tmp/readonly.sql default_ttl=1h max_ttl=24h
# get a new user with password
vault read database/creds/readonly
unset VAULT_TOKEN
Sample Output (click to expand):
[aap@lin2 aap-demo]$ # run as aap on lin2
source ~/aap_env.sh
export VAULT_TOKEN=$(jq -r .root_token ~/vault_init.json)
vault secrets enable database
# connection to EPAS (the user and password are taken from the KV secret)
vault write database/config/epas plugin_name=postgresql-database-plugin allowed_roles=readonly connection_url="postgresql://{{username}}:{{password}}@11.1.1.200:5444/edb" username=enterprisedb password="$(vault kv get -field=password lab/epas/enterprisedb)"
# role readonly: users which can read all data and expire after 1 hour
cat > /tmp/readonly.sql <<'EOF'
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';
GRANT pg_read_all_data TO "{{name}}";
EOF
vault write database/roles/readonly db_name=epas creation_statements=@/tmp/readonly.sql default_ttl=1h max_ttl=24h
# get a new user with password
vault read database/creds/readonly
unset VAULT_TOKEN
Success! Enabled the database secrets engine at: database/
Success! Data written to: database/config/epas
Success! Data written to: database/roles/readonly
Key Value
--- -----
lease_id database/creds/readonly/OsI7HnFotAaTPbKpOH9wThpS
lease_duration 1h
lease_renewable true
password <generated password>
username <generated username>
[aap@lin2 aap-demo]$
We can log in with the generated user, e.g. on lin8 with psql -h 11.1.1.200 -p 5444 -d edb -U <username>. When the lease expires, Vault drops the user again. In a later step AAP or an application could request such a user for each job instead of using a fixed password.
In the next post we use AAP to patch this cluster: first the standby, then a switchover with EFM, and finally the old primary.

Leave a Reply