diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index aadd824..c50350d 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -13,7 +13,7 @@ "state": { "type": "markdown", "state": { - "file": "ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md", + "file": "ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md", "mode": "source", "source": false, "backlinks": true, @@ -28,7 +28,7 @@ } }, "icon": "lucide-file", - "title": "TP 4 Ansible" + "title": "TP 3 Ansible" } } ] @@ -206,8 +206,8 @@ }, "active": "e11a6a080eb83632", "lastOpenFiles": [ - "ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md", "ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md", + "ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md", "ISEN/Cloud Computing/CIPA4/Cloud Computing Cours 1.md", "ISEN/Cloud Computing/CIPA4/TP 2 Ansible.md", "ISEN/Cloud Computing/CIPA4/TP 1 Ansible.md", diff --git a/ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md b/ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md index 9403356..5c23ace 100644 --- a/ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md +++ b/ISEN/Cloud Computing/CIPA4/TP 3 Ansible.md @@ -126,4 +126,18 @@ handlers: service: name: nginx state: restarted +``` + +#### Fichier : `templates/nginx.conf.j2` +```nginx +server { + listen {{ proxy_port }}; + server_name {{ server_name }}; + + location / { + proxy_pass http://127.0.0.2:{{ flask_port }}; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } +} ``` \ No newline at end of file diff --git a/ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md b/ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md index ffe1623..8eb6753 100644 --- a/ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md +++ b/ISEN/Cloud Computing/CIPA4/TP 4 Ansible.md @@ -6,124 +6,93 @@ ssh root@135.125.246.84 mdp : moutarde42 ### Step2 -```yml ---- -- hosts: demo - gather_facts: yes - tasks: - - name: Install the git package - apt: - name: git - state: present - - name: Clone my flask application - git: - repo: 'https://github.com/arnaudmorin/demo-flask.git' - dest: /root/demo-flask +### 1. Structure des dossiers +```bash + +mkdir -p roles/{flask_app,nginx_proxy}/tasks roles/{flask_app,nginx_proxy}/templates roles/{flask_app,nginx_proxy}/handlers ``` -### Step3 +### 2. Rôle `flask_app` +#### Fichier : `roles/flask_app/tasks/main.yml` ```yaml ---- -- hosts: demo - gather_facts: yes - tasks: - - name: Install the git package - apt: - name: git - state: present - - name: Clone my flask application - git: - repo: 'https://github.com/arnaudmorin/demo-flask.git' - dest: /root/demo-flask - - name: Install pip - apt: - name: python3-pip - state: present - - name: Install flask requirements - pip: - requirements: /root/demo-flask/requirements.txt +--- +- name: Installer le paquet git + apt: + name: git + state: present +- name: Cloner l'application Flask + git: + repo: 'https://github.com/arnaudmorin/demo-flask.git' + dest: /root/demo-flask +- name: Installer pip + apt: + name: python3-pip + state: present +- name: Installer les dépendances Flask + pip: + requirements: /root/demo-flask/requirements.txt +- name: Démarrer l'application Flask en arrière-plan + shell: "nohup /root/demo-flask/start.sh &" ``` -### Step4 +### 3. Rôle `nginx_proxy` +#### Fichier : `roles/nginx_proxy/tasks/main.yml` ```yaml ---- -- hosts: demo - gather_facts: yes - tasks: - - name: Install the git package - apt: - name: git - state: present - - name: Clone my flask application - git: - repo: 'https://github.com/arnaudmorin/demo-flask.git' - dest: /root/demo-flask - - name: Install pip - apt: - name: python3-pip - state: present - - name: Install flask requirements - pip: - requirements: /root/demo-flask/requirements.txt - - name: Start the flask application in background - shell: "nohup /root/demo-flask/start.sh &" +`--- +- name: Installer Nginx + apt: + name: nginx + state: present +- name: Configurer Nginx comme reverse proxy + template: + src: nginx.conf.j2 + dest: /etc/nginx/sites-available/{{ server_name }} + notify: Restart Nginx +- name: Activer la configuration du site + file: + src: /etc/nginx/sites-available/{{ server_name }} + dest: /etc/nginx/sites-enabled/{{ server_name }} + state: link + notify: Restart Nginx` +``` +#### Fichier : `roles/nginx_proxy/handlers/main.yml` +```yaml +--- +- name: Restart Nginx + service: + name: nginx + state: restarted` +``` +#### Fichier : `roles/nginx_proxy/templates/nginx.conf.j2` +```nginx +server { + listen {{ proxy_port }}; + server_name {{ server_name }}; + + location / { + proxy_pass http://127.0.0.2:{{ flask_port }}; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } +} ``` -### Step5 -```Yaml +### 4. Playbook principal + +#### Fichier : `playbook-role.yml` +```yaml + --- -- hosts: demo - gather_facts: yes - tasks: - - name: Installer le paquet git - apt: - name: git - state: present - - name: Cloner mon application Flask - git: - repo: 'https://github.com/arnaudmorin/demo-flask.git' - dest: /root/demo-flask - - name: Installer pip - apt: - name: python3-pip - state: present - - name: Installer les dépendances Flask - pip: - requirements: /root/demo-flask/requirements.txt - - name: Démarrer l'application Flask en arrière-plan - shell: "nohup /root/demo-flask/start.sh &" - - -- hosts: localhost - become: yes - vars: - proxy_port: 80 - flask_port: 8080 - server_name: "135.125.246.84" - tasks: - - name: Installer Nginx - apt: - name: nginx - state: present - - name: Configurer Nginx comme reverse proxy - template: - src: nginx.conf.j2 - dest: /etc/nginx/sites-available/demo - notify: Restart Nginx - - name: Activer la configuration du site - file: - src: /etc/nginx/sites-available/demo - dest: /etc/nginx/sites-enabled/demo - state: link - notify: Restart Nginx - - name: Afficher l'URL d'accès au site - debug: - msg: "Votre application est accessible à l'URL : http://{{ server_name }}" - - -handlers: - - name: Restart Nginx - service: - name: nginx - state: restarted +- hosts: demo + roles: + - flask_app + +- hosts: localhost + become: yes + vars: + proxy_port: 80 + flask_port: 8080 + server_name: "135.125.246.84" + roles: + - role: nginx_proxy ``` \ No newline at end of file