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

+20
View File
@@ -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&eacute;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>
+28
View File
@@ -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
+41
View File
@@ -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"
+22
View File
@@ -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
+43
View File
@@ -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
+52
View File
@@ -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")
+40
View File
@@ -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
+19
View File
@@ -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
+27
View File
@@ -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}}"
+34
View File
@@ -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*
+13
View File
@@ -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}}""
+11
View File
@@ -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
+33
View File
@@ -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"
+27
View File
@@ -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 }}"
+4
View File
@@ -0,0 +1,4 @@
---
mariadb_pwd: "Pa$$w0rd1"
PostGreSQL_pwd: "Pa$$w0rd2"
+36
View File
@@ -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
+16
View File
@@ -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 }}'
+13
View File
@@ -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é"
+10
View File
@@ -0,0 +1,10 @@
---
- name: "Upgrade & patch RedHat"
hosts: all
tasks:
- name: upgrade dnf packages
dnf:
name: '*'
state: latest
update_cache: yes
+35
View File
@@ -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}}"
+13
View File
@@ -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}}"
+8
View File
@@ -0,0 +1,8 @@
---
- name: "Verbose"
hosts: all
tasks:
- name: "ls"
become: yes
shell: "ls /"
+12
View File
@@ -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 }}"
+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