Premier commit
This commit is contained in:
commit
518c7ab04b
49 files changed
+1120
No files matched your search
@@ -0,0 +1,28 @@
|
||||
# Hôte hors groupe (rattaché directement à "all" / "ungrouped")
|
||||
aloneServer.compagny.com
|
||||
|
||||
[all:vars]
|
||||
DNS1="2606:4700:4700::53"
|
||||
DNS2="2606:4700:4700::54"
|
||||
DNS3=172.20.0.53
|
||||
DNS4=1.1.1.1
|
||||
|
||||
[VM]
|
||||
FreeBSD ansible_host=10.11.12.13
|
||||
|
||||
[LXC]
|
||||
ansible.rocky
|
||||
ansible.suse
|
||||
ansible.ubuntu
|
||||
|
||||
[Docker]
|
||||
ansible.debian ansible_host=localhost ansible_port=50022
|
||||
ansible.alpine ansible_host=192.168.1.5 ansible_port=65000 ansible_user=ansible
|
||||
|
||||
[France]
|
||||
ansible.alpine
|
||||
FreeBSD
|
||||
|
||||
[France:vars]
|
||||
timezone=Europe/Paris
|
||||
ntp_server=0.fr.pool.ntp.org
|
||||
@@ -0,0 +1,34 @@
|
||||
all:
|
||||
hosts:
|
||||
aloneServer.compagny.com:
|
||||
vars:
|
||||
DNS1: "2606:4700:4700::53"
|
||||
DNS2: "2606:4700:4700::54"
|
||||
DNS3: 172.20.0.53
|
||||
DNS4: 1.1.1.1
|
||||
children:
|
||||
VM:
|
||||
hosts:
|
||||
FreeBSD:
|
||||
ansible_host: 10.11.12.13
|
||||
LXC:
|
||||
hosts:
|
||||
ansible.rocky:
|
||||
ansible.suse:
|
||||
ansible.ubuntu:
|
||||
Docker:
|
||||
hosts:
|
||||
ansible.debian:
|
||||
ansible_host: localhost
|
||||
ansible_port: 50022
|
||||
ansible.alpine:
|
||||
ansible_host: 192.168.1.5
|
||||
ansible_port: 65000
|
||||
ansible_user: ansible
|
||||
France:
|
||||
hosts:
|
||||
ansible.alpine:
|
||||
FreeBSD:
|
||||
vars:
|
||||
timezone: Europe/Paris
|
||||
ntp_server: 0.fr.pool.ntp.org
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr-FR">
|
||||
<head>
|
||||
<title>Machine {{inventory_hostname}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Distribution : {{ansible_distribution}}</h1>
|
||||
<p>Major version : {{ansible_distribution_major_version}}</p>
|
||||
<p>Distribution version : {{ansible_distribution_version}}</p>
|
||||
<p>Révision : {{ansible_distribution_release}}</p>
|
||||
<p>IP : {{ ansible_default_ipv4.address }} - {{ ansible_ens33.ipv4.address }}
|
||||
{% for interface_name in ansible_interfaces %}
|
||||
<p>IP ({{ interface_name }}): {{ hostvars[inventory_hostname]['ansible_' + interface_name]['ipv4']['address'] }}</p>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p>RAM : {{ ansible_memtotal_mb }} Mb</p>
|
||||
{% for cpu in ansible_processor %}
|
||||
<p>CPU : {{ cpu }}</p>
|
||||
{% endfor %}
|
||||
</body>
|
||||
@@ -0,0 +1,28 @@
|
||||
all:
|
||||
hosts:
|
||||
children:
|
||||
Containers:
|
||||
hosts:
|
||||
ansible.alpine:
|
||||
ansible_connection=ssh
|
||||
ansible_user=ansible
|
||||
ansible.debian:
|
||||
ansible.rocky:
|
||||
ansible.suse:
|
||||
ansible.ubuntu:
|
||||
France:
|
||||
hosts:
|
||||
ansible.alpine:
|
||||
freebsd:
|
||||
vars:
|
||||
timezone : 'Europe/Paris'
|
||||
ntp_server : '0.fr.pool.ntp.org'
|
||||
VM:
|
||||
hosts:
|
||||
Freebsd:
|
||||
ansible_host: 10.11.12.13
|
||||
vars:
|
||||
DNS1: 192.168.0.254
|
||||
DNS2: 2606:4700:4700::1111
|
||||
DNS3: 2606:4700:4700::1001
|
||||
DNS4: 1.1.1.1
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
- name: "Install & start Apache"
|
||||
hosts: debian
|
||||
|
||||
# Les lignes de commentaires commencent par un croisillon
|
||||
tasks:
|
||||
- name: "Apache installation"
|
||||
become: yes
|
||||
apt:
|
||||
name: "apache2"
|
||||
state: "present"
|
||||
update_cache: true
|
||||
|
||||
- name: "start Apache"
|
||||
become: yes
|
||||
service:
|
||||
name: apache2
|
||||
state: started
|
||||
# Activation au boot :
|
||||
enabled: yes
|
||||
|
||||
# Sauvegarde du fichier distant
|
||||
# - name: "Save default file"
|
||||
# become: yes
|
||||
# copy:
|
||||
# remote_src: true
|
||||
# src: "/var/www/html/index.html"
|
||||
# dest: "/var/www/html/index.html.old"
|
||||
# owner: "www-data"
|
||||
# group: "www-data"
|
||||
|
||||
# index.html est une page web que l’on aura créé dans ~ansible
|
||||
- name: "Copy Homepage"
|
||||
become: yes
|
||||
copy:
|
||||
src: "/home/ansible/index.html"
|
||||
dest: "/var/www/html/"
|
||||
# backup évite la précédente task
|
||||
backup: true
|
||||
owner: "www-data"
|
||||
group: "www-data"
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
- name: "uninstall Apache"
|
||||
hosts: ansible.debian-Formateur
|
||||
|
||||
tasks:
|
||||
- name: "stop Apache"
|
||||
become: yes
|
||||
ansible.builtin.service:
|
||||
name: apache2
|
||||
state: stopped
|
||||
|
||||
- name: "Remove Apache"
|
||||
become: yes
|
||||
ansible.builtin.apt:
|
||||
name: "apache2"
|
||||
state: "absent"
|
||||
|
||||
- name: "Delete index.html"
|
||||
become: yes
|
||||
ansible.builtin.file:
|
||||
path: /var/www/html/index.html
|
||||
state: absent
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
- name: "how to use block"
|
||||
hosts: ansible.debian ansible.ubuntu
|
||||
|
||||
tasks:
|
||||
- name: "install nginx"
|
||||
# Ce qui est prévu
|
||||
block:
|
||||
|
||||
- name: "install nginx"
|
||||
ansible.builtin.apt:
|
||||
name: nginx
|
||||
state: present
|
||||
- name: configure nginx
|
||||
template:
|
||||
src: nginx.conf.j2
|
||||
dest: /etc/nginx/nginx.conf
|
||||
|
||||
- name: restart nginx
|
||||
ansible.builtin.service:
|
||||
name: nginx
|
||||
state: restarted
|
||||
|
||||
# Ce qui est prévu en cas d’erreur
|
||||
rescue:
|
||||
- name: remove nginx conf file
|
||||
ansible.builtin.file:
|
||||
path: /etc/nginx/nginx.conf
|
||||
state: absent
|
||||
|
||||
- name: remove nginx
|
||||
ansible.builtin.apt:
|
||||
name: nginx
|
||||
state: absent
|
||||
|
||||
# Dans tous les cas
|
||||
always:
|
||||
- name: display playbook finished message
|
||||
ansible.builtin.debug:
|
||||
msg: 'your play was played :)'
|
||||
|
||||
# Commun pour toutes les tâches
|
||||
become: yes
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
- name: Installation selon distribution
|
||||
hosts: all
|
||||
become: yes
|
||||
|
||||
tasks:
|
||||
|
||||
- name: "Debug Environnement"
|
||||
ansible.builtin.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: "Install python3-dnf"
|
||||
ansible.builtin.shell:
|
||||
cmd: dnf -y install python3-dnf
|
||||
when: (ansible_distribution == "Fedora")
|
||||
|
||||
- name: "Update RedHat family"
|
||||
block:
|
||||
- name: "Update system"
|
||||
ansible.builtin.dnf:
|
||||
update_cache: yes
|
||||
name: '*'
|
||||
state: latest
|
||||
- name: "Display message"
|
||||
ansible.builtin.debug:
|
||||
msg: "RedHat family"
|
||||
when: (ansible_distribution == "RedHat") or
|
||||
(ansible_distribution == "Rocky") or
|
||||
(ansible_distribution == "Fedora") or
|
||||
(ansible_distribution == "AlmaLinux")
|
||||
|
||||
- name: "Update Debian family"
|
||||
ansible.builtin.apt:
|
||||
update_cache: yes
|
||||
name: "*"
|
||||
state: latest
|
||||
when: (ansible_distribution == " Debian ") or
|
||||
(ansible_distribution == "Debian")
|
||||
|
||||
- name: "Alpine update"
|
||||
apk:
|
||||
update_cache: yes
|
||||
available: yes
|
||||
upgrade: yes
|
||||
when: (ansible_distribution == "Alpine")
|
||||
|
||||
- name: "OpenSUSE update"
|
||||
zypper:
|
||||
update_cache: yes
|
||||
name: "*"
|
||||
state: latest
|
||||
when: (ansible_distribution == "openSUSE Leap")
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
- name: Installation selon distribution
|
||||
hosts: all
|
||||
become: yes
|
||||
|
||||
tasks:
|
||||
- name: "Set when conditions"
|
||||
set_fact:
|
||||
is_dnf: "{{ ansible_distribution in ['RedHat', 'Rocky', 'Fedora', 'AlmaLinux'] }}"
|
||||
is_apt: "{{ ansible_os_family == 'Debian' }}"
|
||||
is_apk: "{{ ansible_os_family == 'Alpine' }}"
|
||||
is_zypper: "{{ ansible_distribution == 'openSUSE Leap' }}"
|
||||
|
||||
- name: "Update RedHat family"
|
||||
ansible.builtin.dnf:
|
||||
update_cache: yes
|
||||
name: '*'
|
||||
state: latest
|
||||
when: is_dnf
|
||||
|
||||
- name: "Update Debian family"
|
||||
ansible.builtin.apt:
|
||||
update_cache: yes
|
||||
name: "*"
|
||||
state: latest
|
||||
when: is_apt
|
||||
|
||||
- name: "Alpine update"
|
||||
apk:
|
||||
update_cache: yes
|
||||
available: yes
|
||||
upgrade: yes
|
||||
when: is_apk
|
||||
|
||||
- name: "OpenSUSE update"
|
||||
zypper:
|
||||
update_cache: yes
|
||||
name: "*"
|
||||
state: latest
|
||||
when: is_zypper
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
- hosts: all
|
||||
become: yes
|
||||
|
||||
vars:
|
||||
MyUser: pierre
|
||||
timezone: 'Europe/Paris'
|
||||
|
||||
tasks:
|
||||
|
||||
- name: Create user {{ MyUser }}
|
||||
user: name={{ MyUser }}
|
||||
comment="{{ MyUser }} user"
|
||||
check_mode: yes
|
||||
|
||||
- name: Set local timezone
|
||||
timezone:
|
||||
name: Europe/Paris
|
||||
check_mode: no
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
- name: Facts
|
||||
hosts: all
|
||||
|
||||
# Ne collecte rien, sauf ce qui est défini dans gather_subset
|
||||
gather_facts: no
|
||||
|
||||
tasks:
|
||||
|
||||
- name: "gather_facts"
|
||||
ansible.builtin.setup:
|
||||
gather_subset:
|
||||
- '!all'
|
||||
- 'network'
|
||||
|
||||
- name: "IPv4 address"
|
||||
ansible.builtin.debug:
|
||||
msg: 'Mon ip V4 {{ ansible_default_ipv4.address }}'
|
||||
|
||||
- name: "Variable locale"
|
||||
set_fact:
|
||||
MonPrenom: "Pierre"
|
||||
|
||||
- name: "Debug variable inventaire"
|
||||
ansible.builtin.debug:
|
||||
msg: "Mon prénom : {{MonPrenom}} ; mon serveur NTP : {{ntp_server}}"
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
- name: "Create empty files"
|
||||
hosts: ansible.debian
|
||||
become: true
|
||||
gather_facts: no
|
||||
|
||||
tasks:
|
||||
- name: "Create empty file"
|
||||
ansible.builtin.file:
|
||||
path: /tmp/TEST1
|
||||
state: touch
|
||||
notify:
|
||||
- Handler1
|
||||
|
||||
- name: "Create empty file"
|
||||
ansible.builtin.file:
|
||||
path: /tmp/TEST2
|
||||
state: touch
|
||||
notify:
|
||||
- Handler3
|
||||
- Handler2
|
||||
|
||||
handlers:
|
||||
- name: "Handler1"
|
||||
ansible.builtin.debug:
|
||||
msg: " Le fichier TEST1 est créé"
|
||||
|
||||
- name: "Handler2"
|
||||
ansible.builtin.debug:
|
||||
msg: " Le fichier TEST2 est créé"
|
||||
|
||||
- name: "Handler3"
|
||||
ansible.builtin.shell:
|
||||
cmd: ls -l /tmp/TEST*
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- name: "Variables"
|
||||
hosts: localhost
|
||||
|
||||
tasks:
|
||||
|
||||
- name: "Variable locale"
|
||||
set_fact:
|
||||
MonPrenom: "Pierre"
|
||||
|
||||
- name: "Debug variable inventaire"
|
||||
debug:
|
||||
msg: "Mon prénom : {{MonPrenom}} ; mon serveur NTP : {{ntp_server}}""
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
- name: "Inventaire des machines"
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
- name: "HTML files generation"
|
||||
ansible.builtin.template:
|
||||
src: "Inventory.html.j2"
|
||||
dest: "{{playbook_dir}}/{{inventory_hostname}}.html"
|
||||
delegate_to: localhost
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
- name: "Install & start Apache"
|
||||
hosts: all
|
||||
become: true
|
||||
gather_facts: no
|
||||
|
||||
tasks:
|
||||
- name: "Updates"
|
||||
ansible.builtin.package:
|
||||
name: '*'
|
||||
state: latest
|
||||
update_cache: true
|
||||
|
||||
- name: "Install Apache"
|
||||
ansible.builtin.package:
|
||||
name: apache2
|
||||
state: present
|
||||
update_cache: true
|
||||
notify: "Install & start"
|
||||
|
||||
handlers:
|
||||
- name: "Copy Homepage"
|
||||
ansible.builtin.copy:
|
||||
src: "/home/ansible/index.html"
|
||||
dest: "/var/www/html/"
|
||||
owner: "www-data"
|
||||
group: "www-data"
|
||||
listen: "Install & start"
|
||||
|
||||
- name: "Restart Apache"
|
||||
ansible.builtin.service:
|
||||
name: apache2
|
||||
state: started
|
||||
listen: "Install & start"
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
- name: Installation groupées
|
||||
hosts: all
|
||||
become: yes
|
||||
become_user: root
|
||||
tasks:
|
||||
|
||||
- name: "install NTP & VI on Debian"
|
||||
when: ansible_distribution == "Debian"
|
||||
ansible.builtin.apt: pkg={{ item }}
|
||||
loop:
|
||||
- ntp
|
||||
- vim
|
||||
|
||||
- name: "Uninstall NTP & VI on RedHat with vars"
|
||||
when: ansible_distribution == "Rocky" or ansible_distribution == 'Red Hat Enterprise Linux'
|
||||
vars:
|
||||
ServiceName:
|
||||
- chrony
|
||||
- vi
|
||||
ansible.builtin.dnf:
|
||||
name: "{{ ServiceName }}"
|
||||
state: absent
|
||||
# state: present
|
||||
loop:
|
||||
- "{{ ServiceName }}"
|
||||
- "{{ ServiceName }}"
|
||||
@@ -0,0 +1,4 @@
|
||||
---
|
||||
mariadb_pwd: "Pa$$w0rd1"
|
||||
PostGreSQL_pwd: "Pa$$w0rd2"
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
- hosts: all
|
||||
|
||||
post_tasks:
|
||||
|
||||
- name: Check all port numbers are accessible from current host
|
||||
wait_for:
|
||||
host: "{{inventory_hostname}}"
|
||||
port: "{{ item }}"
|
||||
state: started
|
||||
delay: 0
|
||||
timeout: 1
|
||||
ignore_errors: yes
|
||||
with_items:
|
||||
- 443
|
||||
- 80
|
||||
- 8080
|
||||
pre_tasks:
|
||||
- name: "debug"
|
||||
debug:
|
||||
msg: "PRE task played at {{ansible_date_time.date}} {{ansible_date_time.time}}"
|
||||
|
||||
tasks:
|
||||
- name: "Main tasks :"
|
||||
debug:
|
||||
msg: " server {{inventory_hostname}}"
|
||||
- name: "Main task"
|
||||
become: true
|
||||
apt:
|
||||
name: apache2
|
||||
state: present
|
||||
- name: "Start Apache"
|
||||
become: true
|
||||
ansible.builtin.service:
|
||||
name: apache2
|
||||
state: started
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
- hosts: localhost
|
||||
vars_prompt:
|
||||
|
||||
- name: username
|
||||
prompt: "What is your username?"
|
||||
private: no
|
||||
|
||||
- name: password
|
||||
prompt: "What is your password?"
|
||||
|
||||
tasks:
|
||||
|
||||
- ansible.builtin.debug:
|
||||
msg: 'Logging in as {{ username }}'
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- name: "Use Tags"
|
||||
hosts: localhost
|
||||
|
||||
tasks:
|
||||
- name: "Tache non executée"
|
||||
ansible.builtin.debug:
|
||||
msg: "Non tagé"
|
||||
|
||||
- name: "Tache executée"
|
||||
tags: "OK"
|
||||
ansible.builtin.debug:
|
||||
msg: "Tagé"
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: "Upgrade & patch RedHat"
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
- name: upgrade dnf packages
|
||||
dnf:
|
||||
name: '*'
|
||||
state: latest
|
||||
update_cache: yes
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
- name: Variables et tableaux
|
||||
hosts: all
|
||||
gather_facts: no
|
||||
|
||||
vars:
|
||||
VarPI: 3.1415927
|
||||
TableauA:
|
||||
- 'variableT1'
|
||||
- 'variableT2'
|
||||
VarB:
|
||||
Var1: variable B1
|
||||
Var2: variable B2
|
||||
|
||||
tasks:
|
||||
|
||||
- name: "Debug variable locale"
|
||||
ansible.builtin.debug:
|
||||
msg: "{{VarPI}}"
|
||||
|
||||
- name: "Tableau"
|
||||
ansible.builtin.debug:
|
||||
msg: "{{TableauA[0]}} / {{TableauA[1]}}"
|
||||
|
||||
- name: "Debug variable dictionnaire"
|
||||
ansible.builtin.debug:
|
||||
msg: "{{VarB.Var1}} / {{VarB.Var2}}"
|
||||
|
||||
- name: "Variable locale"
|
||||
set_fact:
|
||||
MonPrenom: "Pierre"
|
||||
|
||||
- name: "Variable locale"
|
||||
debug:
|
||||
msg: "Mon prénom : {{MonPrenom}}"
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- name: "Vault password"
|
||||
hosts: localhost
|
||||
gather_facts: no
|
||||
# Inclusion du fichier pour le Playbook
|
||||
vars_files:
|
||||
- "/home/ansible/PB-Password.yml"
|
||||
|
||||
tasks:
|
||||
- name: " Display Password "
|
||||
debug:
|
||||
msg: "{{mariadb_pwd}}"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
- name: "Verbose"
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
- name: "ls"
|
||||
become: yes
|
||||
shell: "ls /"
|
||||
@@ -0,0 +1,12 @@
|
||||
- name: Register
|
||||
hosts: all
|
||||
tasks:
|
||||
- name: "DNS configuration"
|
||||
ansible.builtin.shell:
|
||||
cmd: cat /etc/resolv.conf
|
||||
register: dns_conf
|
||||
|
||||
- name: Afficher la sortie de la commande
|
||||
ansible.builtin.debug:
|
||||
msg: "La configuration DNS est : {{ dns_conf.stdout }}"
|
||||
|
||||
@@ -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"
|
||||
@@ -0,0 +1,4 @@
|
||||
virtualmachines:
|
||||
hosts:
|
||||
MaVM:
|
||||
ansible_host: 192.168.1.206
|
||||
@@ -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!"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
INSERT INTO mytable(index_id, field1) VALUES (0, 'my_first_string'),(1, '2nd_string'),(2, '3rd_string');
|
||||
@@ -0,0 +1,5 @@
|
||||
db_user: dbuser
|
||||
db_password: dbpasswd
|
||||
db_name: mydb
|
||||
user_app: ansible
|
||||
app_name: PostgreSQL
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# playbook.yml
|
||||
- name: Deploy MariaDB
|
||||
hosts: all
|
||||
become: yes
|
||||
roles:
|
||||
- MariaDB
|
||||
@@ -0,0 +1,42 @@
|
||||
MariaDB installation with Ansible
|
||||
=========
|
||||
|
||||
A brief description of the role goes here.
|
||||
|
||||
Requirements
|
||||
------------
|
||||
|
||||
Ansible server + clients.
|
||||
|
||||
Role Variables
|
||||
--------------
|
||||
|
||||
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
|
||||
|
||||
Example Playbook
|
||||
----------------
|
||||
|
||||
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
|
||||
|
||||
- hosts: servers
|
||||
roles:
|
||||
- { role: username.rolename, x: 42 }
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
BSD
|
||||
|
||||
Author Information
|
||||
------------------
|
||||
|
||||
https://pierreau.fr/pro/
|
||||
|
||||
https://www.linkedin.com/in/pierreau/
|
||||
|
||||
https://www.facebook.com/pierreau.pro
|
||||
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# defaults file for MariaDB
|
||||
@@ -0,0 +1,3 @@
|
||||
[client]
|
||||
user=root
|
||||
password=Ansible
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE mytable (
|
||||
index_id INT NOT NULL AUTO_INCREMENT,
|
||||
field1 VARCHAR(64),
|
||||
PRIMARY KEY (index_id)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
INSERT INTO mytable (field1) VALUES ('Texte1'),('Valeur1'),('Info3');
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# handlers file for MariaDB
|
||||
- name: restart mariadb
|
||||
service:
|
||||
name: mariadb
|
||||
state: restarted
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
galaxy_info:
|
||||
author: Pierre ROYER
|
||||
description: Ansible role to deploy and manage MariaDB
|
||||
min_ansible_version: 2.10
|
||||
platforms:
|
||||
- name: RedHat
|
||||
versions:
|
||||
- all
|
||||
- name: Rocky Linux
|
||||
versions:
|
||||
- all
|
||||
- name: Debian
|
||||
versions:
|
||||
- all
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- all
|
||||
version: "1.0"
|
||||
|
||||
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||
# - BSD-3-Clause (default)
|
||||
# - MIT
|
||||
# - GPL-2.0-or-later
|
||||
# - GPL-3.0-only
|
||||
# - Apache-2.0
|
||||
# - CC-BY-4.0
|
||||
license: MIT
|
||||
galaxy_tags: []
|
||||
dependencies: []
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
# MariaDB/tasks/backup_database.yml
|
||||
|
||||
- name: "Set timestamp of the backup"
|
||||
set_fact:
|
||||
now: "{{ lookup('pipe', 'date +%d%m%Y-%H:%M') }}"
|
||||
|
||||
- name: "Create a backup directory"
|
||||
file:
|
||||
path: "/tmp/{{ app_name }}/backups/{{ now }}/"
|
||||
mode: 0777
|
||||
owner: ansible
|
||||
state: directory
|
||||
|
||||
- name: Backup the database
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
state: dump
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
target: "/tmp/{{ app_name }}/backups/{{ now }}/{{ backup_file }}"
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
# MariaDB/tasks/configure_database.yml
|
||||
|
||||
- name: Remove {{ mariadb_database }} database
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
state: absent
|
||||
# après Update MariaDB root password
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
|
||||
- name: Create {{ mariadb_database }} database
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
state: present
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
|
||||
- name: "Copy /files/{{ mariadb_table }} to hosts"
|
||||
copy:
|
||||
src: "{{ role_path }}/files/{{ mariadb_table }}"
|
||||
dest: "/tmp/"
|
||||
owner: "ansible"
|
||||
group: "ansible"
|
||||
|
||||
- name: Create {{ mariadb_table }} table in {{ mariadb_database }} database
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
target: "/tmp/{{ mariadb_table }}"
|
||||
state: import
|
||||
notify: restart mariadb
|
||||
|
||||
#- name: Remove mysql database
|
||||
# mysql_db:
|
||||
# name: "{{ mariadb_database }}"
|
||||
# state: absent
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
# MariaDB/tasks/import_sql.yml
|
||||
|
||||
- name: "Copy Script.sql"
|
||||
copy:
|
||||
src: "{{ role_path }}/files/{{ sql_script_file }}"
|
||||
dest: "/tmp/"
|
||||
owner: "ansible"
|
||||
group: "ansible"
|
||||
|
||||
- name: Import SQL script
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
state: import
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
target: "/tmp/{{ sql_script_file }}"
|
||||
# target: "{{ role_path }}/files/{{ sql_script_file }}"
|
||||
notify: restart mariadb
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
# MariaDB/tasks/install_mariadb.yml
|
||||
|
||||
- name: Install MariaDB on RedHat based systems
|
||||
package:
|
||||
name: "{{ item }}"
|
||||
state: present
|
||||
with_items:
|
||||
- mariadb-server
|
||||
- mariadb
|
||||
- python3-PyMySQL
|
||||
when: (ansible_distribution == "Red Hat Entreprise Linux") or
|
||||
(ansible_distribution == "Rocky") or
|
||||
(ansible_distribution == "Fedora") or
|
||||
(ansible_distribution == "AlmaLinux") or
|
||||
(ansible_distribution == "openSUSE Leap")
|
||||
|
||||
- name: Install MariaDB on Debian/Ubuntu systems
|
||||
package:
|
||||
name: "{{ item }}"
|
||||
state: present
|
||||
with_items:
|
||||
- mariadb-server
|
||||
- mariadb-client
|
||||
- python3-mysqldb
|
||||
when: (ansible_distribution == "Ubuntu") or
|
||||
(ansible_distribution == "Debian")
|
||||
|
||||
- name: Start and enable MariaDB service
|
||||
service:
|
||||
name: mariadb
|
||||
state: started
|
||||
enabled: yes
|
||||
|
||||
# mysql_secure_installation
|
||||
#- name: Update MariaDB root password
|
||||
# mysql_user: name=root host=localhost password={{mariadb_root_password}}
|
||||
# with_items:
|
||||
# - 127.0.0.1
|
||||
# - ::1
|
||||
# - VM
|
||||
|
||||
# mysql_secure_installation
|
||||
- name: Update MariaDB root password
|
||||
community.mysql.mysql_user:
|
||||
login_user: root
|
||||
login_password: ''
|
||||
user: root
|
||||
password: "{{ mariadb_root_password }}"
|
||||
|
||||
# mysql_secure_installation
|
||||
- name: Remove MySQL test database
|
||||
mysql_db:
|
||||
name: test
|
||||
state: absent
|
||||
# après Update MariaDB root password
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
# MariaDB/tasks/main.yml
|
||||
|
||||
- include_tasks: install_mariadb.yml
|
||||
- include_tasks: configure_database.yml
|
||||
- include_tasks: import_sql.yml
|
||||
- include_tasks: backup_database.yml
|
||||
#- include_tasks: restore_database.yml
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Restore database from backup
|
||||
mysql_db:
|
||||
name: "{{ mariadb_database }}"
|
||||
state: import
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_root_password }}"
|
||||
target: "{{ role_path }}/files/{{ backup_file }}"
|
||||
notify: restart mariadb
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
localhost
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- hosts: localhost
|
||||
remote_user: root
|
||||
roles:
|
||||
- MariaDB
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
# vars file for MariaDB
|
||||
mariadb_root_password: "r00t_p@sswd"
|
||||
mariadb_database: "MariaDB"
|
||||
mariadb_table: "Create_table.sql"
|
||||
sql_script_file: "Import.sql"
|
||||
app_name: "MariaDB"
|
||||
backup_file: "Backup.sql"
|
||||
@@ -0,0 +1,5 @@
|
||||
[defaults]
|
||||
inventory = ./Inventory.ini
|
||||
bin_ansible_callbacks = True
|
||||
stdout_callback = default
|
||||
callback_result_format = yaml
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<title>Cloud de notre formation privée</title>
|
||||
<style>
|
||||
body { background-color: darkgrey; color: black;}
|
||||
h1 { color: LightGreen }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Le site Internet de notre formation</h1>
|
||||
<p>Notre serveur web est fonctionnel !</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user