Premier commit

This commit is contained in:
gitea committed 2026-10-18 16:41:04 +02:00
commit 518c7ab04b
49 files changed
+1120

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
---
- name: Postgres
hosts: all
become: yes
vars_files:
- vars.yml
pre_tasks:
- name: Debug Environnement
debug:
msg: "ansible_os_family : {{ansible_os_family}} ; ansible_distribution : {{ansible_distribution}} ; ansible_distribution_major_version : {{ansible_distribution_major_version}} ; ansible_distribution_release : {{ansible_distribution_release}}"
- name: Debian postgresql server installation
block:
- name: Update repository
ansible.builtin.shell: apt update
- name: PostgreSQL installation
ansible.builtin.apt:
name: "{{item}}"
state: present
loop:
- postgresql
- postgresql-contrib
- python3-psycopg2
when: (ansible_os_family == "Debian")
- name: Redhat postgresql server installation
block:
- name: Update repository
ansible.builtin.shell: dnf update -y
- name: PostgreSQL installation
ansible.builtin.dnf:
name: "{{item}}"
state: present
loop:
- postgresql
- postgresql-server
- python3-psycopg2
# - python3-pip
# - acl # Only for Docker containers
when: (ansible_os_family == "RedHat")
- name: Suse postgresql server installation
zypper:
name: "{{item}}"
state: present
loop:
- postgresql
- postgresql-server
- python3-psycopg2
# - python3-pip
# - acl # Only for Docker containers
when: (ansible_os_family == "Suse")
- name: Start PostgreSQL
ansible.builtin.service:
name: postgresql
state: started
enabled: yes
- name: Create {{ db_name }} database
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ db_name }}"
state: present
- name: Create table into {{ db_name }} database
become_user: postgres
community.postgresql.postgresql_query:
db: "{{ db_name }}"
query: CREATE TABLE IF NOT EXISTS mytable (index_id INT PRIMARY KEY, field1 VARCHAR(64))
- name: View tables
become_user: postgres
community.postgresql.postgresql_query:
db: "{{ db_name }}"
query: SELECT table_name,table_catalog,table_schema,table_type FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema') ORDER BY table_name;
- name: Create transfert directory if it does not exist
ansible.builtin.file:
path: /var/lib/postgresql/FILES
state: directory
owner: postgres
group: postgres
mode: '0770'
- name: Copy Script.sql
ansible.builtin.copy:
src: Script.sql
dest: "/var/lib/postgresql/FILES/"
owner: postgres
# Comment if duplicated keys
- name: Insert row into table
become_user: postgres
community.postgresql.postgresql_script:
db: "{{ db_name }}"
path: "/var/lib/postgresql/FILES/Script.sql"
- name: View table data
become_user: postgres
community.postgresql.postgresql_query:
db: "{{ db_name }}"
query: SELECT * FROM mytable;
post_tasks:
- name: Dump database
block:
- name: Set timestamp
set_fact:
now: "{{ lookup('pipe', 'date +%Y%m%d-%H:%M:%S') }}"
tags: "dump"
- name: Back up in progres...
become_user: postgres
community.postgresql.postgresql_db:
state: dump
name: "{{ db_name }}"
target: "/var/lib/postgresql/FILES/{{ db_name }}_{{ now }}.dump.gz"
tags: "dump"
- name: Restore the database
block:
- name: Find files in the target directory
become_user: postgres
find:
paths: "/var/lib/postgresql/FILES"
file_type: file
age: 0
register: found_files
tags: "restore"
- name: Sort files by modification time and get the latest
set_fact:
latest_file: "{{ found_files.files | sort(attribute='mtime') | last }}"
tags: "restore"
- name: Display the latest file
debug:
msg: "Latest file: {{ latest_file.path }}"
tags: "restore"
- name: Restoration in progress...
become_user: postgres
postgresql_db:
state: restore
name: "{{db_name}}"
target: "{{ latest_file.path }}"
# target: "/var/lib/postgresql/FILES/{{ db_name }}_{{ now }}.dump.gz"
tags: "restore"
+4
View File
@@ -0,0 +1,4 @@
virtualmachines:
hosts:
MaVM:
ansible_host: 192.168.1.206
+50
View File
@@ -0,0 +1,50 @@
---
- name: Remove Postgres
hosts: Ubuntu
become: yes
vars_files:
- vars.yml
tasks:
- name: PostgreSQL uninstallation (Debian)
apt:
name: "{{ item }}"
state: absent
purge: true
loop:
- postgresql
- postgresql-contrib
- python3-psycopg2
when: (ansible_os_family == "Debian")
- name: PostgreSQL uninstallation (RedHat)
apt:
name: "{{ item }}"
state: absent
purge: true
loop:
- postgresql
- postgresql-server
- python3-psycopg2
# - python3-pip
# - acl # Only for Docker containers
when: (ansible_os_family == "RedHat")
- name: Remove database
ansible.builtin.file:
path: "/var/lib/postgresql"
state: absent
- name: Create timestamp for log file
set_fact:
now: "{{ lookup('pipe', 'date +%Y%m%d-%H:%M:%S') }}"
handlers:
- name: Trace operation into local log file
delegate_to: localhost
ansible.builtin.lineinfile:
create: true
path: /var/log/Ansible.log
line: "{{ now }} - database {{ db_name }} destroyed!"
+1
View File
@@ -0,0 +1 @@
INSERT INTO mytable(index_id, field1) VALUES (0, 'my_first_string'),(1, '2nd_string'),(2, '3rd_string');
+5
View File
@@ -0,0 +1,5 @@
db_user: dbuser
db_password: dbpasswd
db_name: mydb
user_app: ansible
app_name: PostgreSQL