ansible postgres - ghdrako/doc_snipets GitHub Wiki

Playbook

$ cat postgres.yml
- name: Install PostgreSQL
hosts: db
become: yes
become_user: root
tasks:
- name: Install PostgreSQL
apt: name=postgresql state=present
notify:
- Restart PostgreSQL
- name: Create database and user
hosts: db
become: yes
become_user: postgres
tasks:
- name: Create database
postgresql_db: name=mydb
- name: Create user
postgresql_user: name=myuser password=mypassword priv=ALL db=mydb
- name: Configure PostgreSQL
hosts: db
become: yes
become_user: postgres
tasks:
- name: Set shared memory
lineinfile:
path: /etc/sysctl.conf
line: "kernel.shmmax = 134217728"
 notify:
- Reload sysctl
- name: Set max connections
lineinfile:
path: /etc/postgresql/13/main/postgresql.conf
regexp: '^max_connections'
line: "max_connections = 100"
notify:
- Restart PostgreSQL
- name: Set logging settings
lineinfile:
path: /etc/postgresql/13/main/postgresql.conf
regexp: '^log_'
line: "log_destination = 'csvlog'"
notify:
- Restart PostgreSQL
- name: Restart PostgreSQL
hosts: db
become: yes
become_user: postgres
tasks:
- name: Restart PostgreSQL
service: name=postgresql state=restarted
- name: Reload sysctl
hosts: db
become: yes
become_user: root
tasks:
- name: Reload sysctl
command: sysctl -p

This playbook defines four main tasks:

  1. Install PostgreSQL.
  2. Create a database and user.
  3. Configure PostgreSQL.
  4. Restart PostgreSQL.

Inwentory

$cat hosts
[db]
ec2-instance ansible_host=<ec2-instance-ip> ansible_user=<user>
ansible-playbook -i inventory/hosts playbooks/postgres.yml
⚠️ **GitHub.com Fallback** ⚠️