commit 518c7ab04b8a2d6f510e89d347dff9efb706c8be Author: root Date: Sun Oct 18 16:41:04 2026 +0200 Premier commit diff --git a/Inventory.ini b/Inventory.ini new file mode 100644 index 0000000..36cdbe9 --- /dev/null +++ b/Inventory.ini @@ -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 \ No newline at end of file diff --git a/Inventory.yml b/Inventory.yml new file mode 100644 index 0000000..037c991 --- /dev/null +++ b/Inventory.yml @@ -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 \ No newline at end of file diff --git a/Playbook/Inventory.html.j2 b/Playbook/Inventory.html.j2 new file mode 100644 index 0000000..0496ab0 --- /dev/null +++ b/Playbook/Inventory.html.j2 @@ -0,0 +1,20 @@ + + + + Machine {{inventory_hostname}} + + +

Distribution : {{ansible_distribution}}

+

Major version : {{ansible_distribution_major_version}}

+

Distribution version : {{ansible_distribution_version}}

+

Révision : {{ansible_distribution_release}}

+

IP : {{ ansible_default_ipv4.address }} - {{ ansible_ens33.ipv4.address }} + {% for interface_name in ansible_interfaces %} +

IP ({{ interface_name }}): {{ hostvars[inventory_hostname]['ansible_' + interface_name]['ipv4']['address'] }}

+ {% endfor %} +

+

RAM : {{ ansible_memtotal_mb }} Mb

+ {% for cpu in ansible_processor %} +

CPU : {{ cpu }}

+ {% endfor %} + diff --git a/Playbook/Inventory.yml b/Playbook/Inventory.yml new file mode 100644 index 0000000..bdb8a0b --- /dev/null +++ b/Playbook/Inventory.yml @@ -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 diff --git a/Playbook/PB-Apache.yml b/Playbook/PB-Apache.yml new file mode 100644 index 0000000..63be5f9 --- /dev/null +++ b/Playbook/PB-Apache.yml @@ -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" \ No newline at end of file diff --git a/Playbook/PB-ApacheRemove.yml b/Playbook/PB-ApacheRemove.yml new file mode 100644 index 0000000..04572e0 --- /dev/null +++ b/Playbook/PB-ApacheRemove.yml @@ -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 diff --git a/Playbook/PB-Block.yml b/Playbook/PB-Block.yml new file mode 100644 index 0000000..9c58252 --- /dev/null +++ b/Playbook/PB-Block.yml @@ -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 diff --git a/Playbook/PB-Distrib1.yml b/Playbook/PB-Distrib1.yml new file mode 100644 index 0000000..ed41fe8 --- /dev/null +++ b/Playbook/PB-Distrib1.yml @@ -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") diff --git a/Playbook/PB-Distrib2.yml b/Playbook/PB-Distrib2.yml new file mode 100644 index 0000000..4f2c148 --- /dev/null +++ b/Playbook/PB-Distrib2.yml @@ -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 diff --git a/Playbook/PB-Dry.yml b/Playbook/PB-Dry.yml new file mode 100644 index 0000000..0c3cc11 --- /dev/null +++ b/Playbook/PB-Dry.yml @@ -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 diff --git a/Playbook/PB-Facts.yml b/Playbook/PB-Facts.yml new file mode 100644 index 0000000..b74e12b --- /dev/null +++ b/Playbook/PB-Facts.yml @@ -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}}" + diff --git a/Playbook/PB-Handlers.yml b/Playbook/PB-Handlers.yml new file mode 100644 index 0000000..769a5ca --- /dev/null +++ b/Playbook/PB-Handlers.yml @@ -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* diff --git a/Playbook/PB-Import.yml b/Playbook/PB-Import.yml new file mode 100644 index 0000000..bd77a44 --- /dev/null +++ b/Playbook/PB-Import.yml @@ -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}}"" diff --git a/Playbook/PB-Inventory.yml b/Playbook/PB-Inventory.yml new file mode 100644 index 0000000..5073f89 --- /dev/null +++ b/Playbook/PB-Inventory.yml @@ -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 + diff --git a/Playbook/PB-Listen.yml b/Playbook/PB-Listen.yml new file mode 100644 index 0000000..996331f --- /dev/null +++ b/Playbook/PB-Listen.yml @@ -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" \ No newline at end of file diff --git a/Playbook/PB-Loop.yml b/Playbook/PB-Loop.yml new file mode 100644 index 0000000..5b7cd05 --- /dev/null +++ b/Playbook/PB-Loop.yml @@ -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 }}" \ No newline at end of file diff --git a/Playbook/PB-Password.yml b/Playbook/PB-Password.yml new file mode 100644 index 0000000..6088ef6 --- /dev/null +++ b/Playbook/PB-Password.yml @@ -0,0 +1,4 @@ +--- +mariadb_pwd: "Pa$$w0rd1" +PostGreSQL_pwd: "Pa$$w0rd2" + diff --git a/Playbook/PB-Pre-Post.yml b/Playbook/PB-Pre-Post.yml new file mode 100644 index 0000000..5be166d --- /dev/null +++ b/Playbook/PB-Pre-Post.yml @@ -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 \ No newline at end of file diff --git a/Playbook/PB-Prompt.yml b/Playbook/PB-Prompt.yml new file mode 100644 index 0000000..22ebd15 --- /dev/null +++ b/Playbook/PB-Prompt.yml @@ -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 }}' + diff --git a/Playbook/PB-Tags.yml b/Playbook/PB-Tags.yml new file mode 100644 index 0000000..148e579 --- /dev/null +++ b/Playbook/PB-Tags.yml @@ -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é" diff --git a/Playbook/PB-Upgrade.yml b/Playbook/PB-Upgrade.yml new file mode 100644 index 0000000..c0b4bbe --- /dev/null +++ b/Playbook/PB-Upgrade.yml @@ -0,0 +1,10 @@ +--- +- name: "Upgrade & patch RedHat" + hosts: all + + tasks: + - name: upgrade dnf packages + dnf: + name: '*' + state: latest + update_cache: yes diff --git a/Playbook/PB-Variables.yml b/Playbook/PB-Variables.yml new file mode 100644 index 0000000..9b8d9a6 --- /dev/null +++ b/Playbook/PB-Variables.yml @@ -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}}" \ No newline at end of file diff --git a/Playbook/PB-Vault.yml b/Playbook/PB-Vault.yml new file mode 100644 index 0000000..0523c53 --- /dev/null +++ b/Playbook/PB-Vault.yml @@ -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}}" + diff --git a/Playbook/PB-Verbose.yml b/Playbook/PB-Verbose.yml new file mode 100644 index 0000000..86e8937 --- /dev/null +++ b/Playbook/PB-Verbose.yml @@ -0,0 +1,8 @@ +--- +- name: "Verbose" + hosts: all + + tasks: + - name: "ls" + become: yes + shell: "ls /" diff --git a/Playbook/PB-register.yml b/Playbook/PB-register.yml new file mode 100644 index 0000000..6e4d856 --- /dev/null +++ b/Playbook/PB-register.yml @@ -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 }}" + diff --git a/Playbook/PostgreSQL/CreatePGSQL.yml b/Playbook/PostgreSQL/CreatePGSQL.yml new file mode 100644 index 0000000..650d539 --- /dev/null +++ b/Playbook/PostgreSQL/CreatePGSQL.yml @@ -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" diff --git a/Playbook/PostgreSQL/Inventory.yml b/Playbook/PostgreSQL/Inventory.yml new file mode 100644 index 0000000..e2932b8 --- /dev/null +++ b/Playbook/PostgreSQL/Inventory.yml @@ -0,0 +1,4 @@ +virtualmachines: + hosts: + MaVM: + ansible_host: 192.168.1.206 diff --git a/Playbook/PostgreSQL/RemovePGSQL.yml b/Playbook/PostgreSQL/RemovePGSQL.yml new file mode 100644 index 0000000..f65273c --- /dev/null +++ b/Playbook/PostgreSQL/RemovePGSQL.yml @@ -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!" + diff --git a/Playbook/PostgreSQL/Script.sql b/Playbook/PostgreSQL/Script.sql new file mode 100644 index 0000000..a63fd9d --- /dev/null +++ b/Playbook/PostgreSQL/Script.sql @@ -0,0 +1 @@ +INSERT INTO mytable(index_id, field1) VALUES (0, 'my_first_string'),(1, '2nd_string'),(2, '3rd_string'); \ No newline at end of file diff --git a/Playbook/PostgreSQL/vars.yml b/Playbook/PostgreSQL/vars.yml new file mode 100644 index 0000000..6fef738 --- /dev/null +++ b/Playbook/PostgreSQL/vars.yml @@ -0,0 +1,5 @@ +db_user: dbuser +db_password: dbpasswd +db_name: mydb +user_app: ansible +app_name: PostgreSQL diff --git a/Roles/MariaDB.yml b/Roles/MariaDB.yml new file mode 100644 index 0000000..7616a44 --- /dev/null +++ b/Roles/MariaDB.yml @@ -0,0 +1,7 @@ +--- +# playbook.yml +- name: Deploy MariaDB + hosts: all + become: yes + roles: + - MariaDB diff --git a/Roles/MariaDB/README.md b/Roles/MariaDB/README.md new file mode 100644 index 0000000..3e982a7 --- /dev/null +++ b/Roles/MariaDB/README.md @@ -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 diff --git a/Roles/MariaDB/defaults/main.yml b/Roles/MariaDB/defaults/main.yml new file mode 100644 index 0000000..a0a36ca --- /dev/null +++ b/Roles/MariaDB/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for MariaDB diff --git a/Roles/MariaDB/files/.my.cnf b/Roles/MariaDB/files/.my.cnf new file mode 100644 index 0000000..93e38c2 --- /dev/null +++ b/Roles/MariaDB/files/.my.cnf @@ -0,0 +1,3 @@ +[client] +user=root +password=Ansible \ No newline at end of file diff --git a/Roles/MariaDB/files/Create_table.sql b/Roles/MariaDB/files/Create_table.sql new file mode 100644 index 0000000..7e418fe --- /dev/null +++ b/Roles/MariaDB/files/Create_table.sql @@ -0,0 +1,5 @@ +CREATE TABLE mytable ( + index_id INT NOT NULL AUTO_INCREMENT, + field1 VARCHAR(64), + PRIMARY KEY (index_id) +); diff --git a/Roles/MariaDB/files/Import.sql b/Roles/MariaDB/files/Import.sql new file mode 100644 index 0000000..bce9e7b --- /dev/null +++ b/Roles/MariaDB/files/Import.sql @@ -0,0 +1 @@ +INSERT INTO mytable (field1) VALUES ('Texte1'),('Valeur1'),('Info3'); diff --git a/Roles/MariaDB/handlers/main.yml b/Roles/MariaDB/handlers/main.yml new file mode 100644 index 0000000..75289d2 --- /dev/null +++ b/Roles/MariaDB/handlers/main.yml @@ -0,0 +1,7 @@ +--- +# handlers file for MariaDB +- name: restart mariadb + service: + name: mariadb + state: restarted + diff --git a/Roles/MariaDB/meta/main.yml b/Roles/MariaDB/meta/main.yml new file mode 100644 index 0000000..cfc0232 --- /dev/null +++ b/Roles/MariaDB/meta/main.yml @@ -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: [] diff --git a/Roles/MariaDB/tasks/backup_database.yml b/Roles/MariaDB/tasks/backup_database.yml new file mode 100644 index 0000000..03df437 --- /dev/null +++ b/Roles/MariaDB/tasks/backup_database.yml @@ -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 }}" + diff --git a/Roles/MariaDB/tasks/configure_database.yml b/Roles/MariaDB/tasks/configure_database.yml new file mode 100644 index 0000000..8880bd3 --- /dev/null +++ b/Roles/MariaDB/tasks/configure_database.yml @@ -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 diff --git a/Roles/MariaDB/tasks/import_sql.yml b/Roles/MariaDB/tasks/import_sql.yml new file mode 100644 index 0000000..8a1a132 --- /dev/null +++ b/Roles/MariaDB/tasks/import_sql.yml @@ -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 + diff --git a/Roles/MariaDB/tasks/install_mariadb.yml b/Roles/MariaDB/tasks/install_mariadb.yml new file mode 100644 index 0000000..7141a0a --- /dev/null +++ b/Roles/MariaDB/tasks/install_mariadb.yml @@ -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 }}" diff --git a/Roles/MariaDB/tasks/main.yml b/Roles/MariaDB/tasks/main.yml new file mode 100644 index 0000000..339ee32 --- /dev/null +++ b/Roles/MariaDB/tasks/main.yml @@ -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 + diff --git a/Roles/MariaDB/tasks/restore_database.yml b/Roles/MariaDB/tasks/restore_database.yml new file mode 100644 index 0000000..53d510d --- /dev/null +++ b/Roles/MariaDB/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 + diff --git a/Roles/MariaDB/tests/inventory b/Roles/MariaDB/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/Roles/MariaDB/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/Roles/MariaDB/tests/test.yml b/Roles/MariaDB/tests/test.yml new file mode 100644 index 0000000..1184630 --- /dev/null +++ b/Roles/MariaDB/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - MariaDB diff --git a/Roles/MariaDB/vars/main.yml b/Roles/MariaDB/vars/main.yml new file mode 100644 index 0000000..0aed514 --- /dev/null +++ b/Roles/MariaDB/vars/main.yml @@ -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" diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..dea2c80 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,5 @@ +[defaults] +inventory = ./Inventory.ini +bin_ansible_callbacks = True +stdout_callback = default +callback_result_format = yaml diff --git a/index.html b/index.html new file mode 100644 index 0000000..bf9eab3 --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + Cloud de notre formation privée + + + +

Le site Internet de notre formation

+

Notre serveur web est fonctionnel !

+ +