How to Install PostgreSQL with Ansible in RedHat-like systems?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Install PostgreSQL in RedHat-like systems
- Install server, client, utils =>
ansible.builtin.yum
- Initialize db =>
ansible.builtin.stat,ansible.builtin.shell
- Start and Enable at boot =>
ansible.builtin.service
In order to install PostgreSQL on a RedHat-like system, you need to perform three steps.
The first step is to install the packages to perform server, client, and utils. You are going to use the ansible.builtin.yum Ansible module.
These include the distribution-related binaries, libraries, and documentation for your RedHat-like system.
The second step is to initialize the PostgreSQL database. There is a command-line utility that you could execute using the Ansible ansible.builtin.shell module. The effective command executed is postgresql-setup initdb.
This code is executed only if needed, the conditional check was performed by the ansible.builtin.stat module.
The third step is to Start and Enable the PostgreSQL service at boot using the ansible.builtin.service Ansible module.
Links
- [
ansible.builtin.yum](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html)
- [
ansible.builtin.stat](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html)
- [
ansible.builtin.shell](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html)
- [
ansible.builtin.service](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html)
## Playbook
Let's jump into a real-life playbook to install PostgreSQL in RedHat-like systems with Ansible.
I'm going to show you how to install the PostgreSQL server, client utilities, and the Python libraries to manage the DBMS.
The second step is to perform the initial PostgreSQL database initialization, only if data were not present.
After these steps, you're able to start the service and enable it at boot time.
code
```bash
---
- name: postgresql Playbook
hosts: all
become: true
tasks:
- name: Install packages
ansible.builtin.yum:
name:
- postgresql
- postgresql-server
- postgresql-contrib
- postgresql-libs
- python3-psycopg2
state: present
- name: Check if PostgreSQL is initialized
a