Postgres user creation with ansible¶
Imported from Confluence
Content may be outdated. Verify before following any procedures. View original | Last updated: October 2025
Prerequisites¶
The following packages should be installed ansible,ansible-playbook and ansible-vault (password encruption),openssl,psycopg2.
For the latest macos version we have to follow the below approach :As we can't install psycopg2-binary in latest macos for latest version of ansible with default python 3.9
brew install python, openssl , ansible
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig"
#### Install psycopg2-binary
/opt/homebrew/opt/python@3.13/bin/python3.13 -m pip install --user psycopg2-binary --break-system-packages
Configuration¶
We are maintaining two repo's for the purpose of DB credentials management via ansible.
Here 1st repository is used for all ansible configuration file and 2nd repository is used for assigning values for different database instances we have.
- acp-edge-db-prod
- acp-edge-db-prod-replica
- dt-growth-db-prod
- dt-growth-db-stg-dev
- supply-db-master
- supply-db-replica
- unified-demand-prod-db
- unified-demand-stg-db
How to run play-book to manage DB users.¶
We have to download both repository in a single directory. And then we can run ansible-playbook command for different DB instance we have. In the below example we will run/test ansible for acp-edge-db.
ansible-playbook -v -i bln-ansible-shared-inventories/inventories/production/hosts bln-ansible/acpedge_gcp_master_users.yml -e 'ansible_python_interpreter=/opt/homebrew/bin/python3' --check --diff
Info
More information on ansible postgres_user module: Postgresql User Module
Password hash generation¶
To generate password hash the following algorithm is used:
Bash example:
Where:
- username: JOE
- password: verysecretpassword