commit 6bc41e657b1cb54fce658eb9e91e953f7eae3e98 Author: morepudding Date: Tue Dec 9 16:11:07 2025 +0100 first revision diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..89ed09f --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Projet Ansible - Installation Nginx sur VPS + +## Configuration + +1. **Modifier l'inventory** : Éditez `inventory.yml` et remplacez `YOUR_VPS_IP_ADDRESS` par l'adresse IP de votre VPS + +2. **Configurer l'accès SSH** : + - Assurez-vous d'avoir accès SSH à votre VPS + - Si vous utilisez une clé SSH, décommentez et configurez `ansible_ssh_private_key_file` + +## Utilisation + +### Tester la connexion +```bash +ansible all -m ping +``` + +### Exécuter le playbook +```bash +ansible-playbook playbook.yml +``` + +### Exécuter en mode vérification (dry-run) +```bash +ansible-playbook playbook.yml --check +``` + +### Exécuter avec verbosité +```bash +ansible-playbook playbook.yml -v +``` + +## Structure du projet + +- `ansible.cfg` : Configuration Ansible +- `inventory.yml` : Inventaire des serveurs au format YAML +- `playbook.yml` : Playbook d'installation de Nginx diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..c9f779f --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,11 @@ +[defaults] +inventory = inventory.yml +host_key_checking = False +remote_user = root +retry_files_enabled = False + +[privilege_escalation] +become = True +become_method = sudo +become_user = root +become_ask_pass = False diff --git a/defderkey b/defderkey new file mode 100644 index 0000000..2aa396e --- /dev/null +++ b/defderkey @@ -0,0 +1,8 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACD8oPnRrBM3l7Iryjr2JjOl5ansc6isAPqIdrqM78X38gAAAKjKcKjqynCo +6gAAAAtzc2gtZWQyNTUxOQAAACD8oPnRrBM3l7Iryjr2JjOl5ansc6isAPqIdrqM78X38g +AAAEAxOMEMLKQ09q1641Hua7WSzgF8vRvx89EDEA3zKIiyjvyg+dGsEzeXsivKOvYmM6Xl +qexzqKwA+oh2uozvxffyAAAAJG1hY2Jvb2tATEFQVE9QLTNIQlVNSUpLLk5leHRHZW4uQ2 +VzaQE= +-----END OPENSSH PRIVATE KEY----- diff --git a/defderkey.pub b/defderkey.pub new file mode 100644 index 0000000..a10ebb4 --- /dev/null +++ b/defderkey.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPyg+dGsEzeXsivKOvYmM6XlqexzqKwA+oh2uozvxffy macbook@LAPTOP-3HBUMIJK.NextGen.Cesi diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..fdce59b --- /dev/null +++ b/install.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Script d'installation du projet Ansible + +set -e + +echo "================================================" +echo "Installation du projet Ansible - Nginx sur VPS" +echo "================================================" +echo "" + +# Vérifier si Python3 est installé +if ! command -v python3 &> /dev/null; then + echo "❌ Python3 n'est pas installé. Veuillez l'installer d'abord." + exit 1 +fi + +echo "✅ Python3 est installé" + +# Créer un environnement virtuel s'il n'existe pas +if [ ! -d ".venv" ]; then + echo "📦 Création de l'environnement virtuel..." + python3 -m venv .venv + echo "✅ Environnement virtuel créé" +else + echo "✅ Environnement virtuel existe déjà" +fi + +# Activer l'environnement virtuel +echo "🔧 Activation de l'environnement virtuel..." +source .venv/bin/activate + +# Mettre à jour pip +echo "⬆️ Mise à jour de pip..." +pip install --upgrade pip > /dev/null 2>&1 + +# Installer les dépendances +echo "📥 Installation d'Ansible et des dépendances..." +pip install -r requirements.txt + +echo "" +echo "✅ Installation terminée avec succès!" +echo "" +echo "================================================" +echo "Prochaines étapes:" +echo "================================================" +echo "1. Éditez 'inventory.yml' et configurez l'IP de votre VPS" +echo "2. Activez l'environnement virtuel : source .venv/bin/activate" +echo "3. Testez la connexion : ansible all -m ping" +echo "4. Lancez le script : ./run.sh" +echo "" diff --git a/inventory.yml b/inventory.yml new file mode 100644 index 0000000..b652d5b --- /dev/null +++ b/inventory.yml @@ -0,0 +1,11 @@ +--- +all: + children: + webservers: + hosts: + vps1: + ansible_host: defder.fr + ansible_user: root + ansible_port: 22 + ansible_ssh_private_key_file: ./defderkey + \ No newline at end of file diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..5c3d64e --- /dev/null +++ b/playbook.yml @@ -0,0 +1,67 @@ +--- +- name: Installer et configurer Nginx sur VPS Ubuntu + hosts: webservers + become: yes + + tasks: + - name: Mettre à jour le cache apt + apt: + update_cache: yes + cache_valid_time: 3600 + + - name: Installer Nginx + apt: + name: nginx + state: present + + - name: S'assurer qu'UFW est installé + apt: + name: ufw + state: present + + - name: Autoriser le trafic SSH dans le firewall (UFW) + ufw: + rule: allow + port: '22' + proto: tcp + + - name: Autoriser le trafic HTTP dans le firewall (UFW) + ufw: + rule: allow + port: '80' + proto: tcp + + - name: Autoriser le trafic HTTPS dans le firewall (UFW) + ufw: + rule: allow + port: '443' + proto: tcp + + - name: Activer UFW(uncomplicated firewall) + ufw: + state: enabled + + - name: Démarrer et activer Nginx + systemd: + name: nginx + state: started + enabled: yes + + - name: Vérifier que Nginx est en cours d'exécution + service: + name: nginx + state: started + register: nginx_status + + - name: Afficher le statut de Nginx + debug: + msg: "Nginx est installé et en cours d'exécution" + + - name: Récupérer l'IP du serveur + command: hostname -I + register: server_ip + changed_when: false + + - name: Afficher l'URL d'accès + debug: + msg: "Nginx est accessible à l'adresse : http://{{ server_ip.stdout.split()[0] }}" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..40d19fd --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ansible>=2.9 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..b26b439 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +ansible-playbook -i inventory.yml --private-key defderkey playbook.yml \ No newline at end of file