Ansible setup

This commit is contained in:
Alexis Bruneteau 2025-05-07 02:29:50 +02:00
parent 53b1b9273d
commit 61752c8d80
4 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,7 @@
[webservers]
portfolio-head ansible_host=192.168.1.87
[all:vars]
ansible_user=root
ansible_python_interpreter=/usr/bin/python3

View File

@ -0,0 +1,39 @@
- name: Deploy a basic Nginx container
hosts: webservers
become: true
tasks:
- name: Ensure Docker is installed
apt:
name: docker.io
state: present
update_cache: yes
- name: Ensure pip is installed
apt:
name: pip
state: present
update_cache: yes
- name: Ensure Python Docker module is installed
apt:
name: python3-docker
state: present
- name: Ensure web root exists
file:
path: /opt/web
state: directory
- name: Copy index.html to web root
copy:
src: ../files/index.html
dest: /opt/web/index.html
- name: Run Nginx container
docker_container:
name: simple-nginx
image: nginx:alpine
state: started
restart_policy: always
ports:
- "8080:80"
volumes:
- "/opt/web:/usr/share/nginx/html:ro"

View File

@ -0,0 +1,61 @@
- name: Deploy a basic Nginx container
hosts: webservers
become: true
tasks:
- name: Ensure Docker is installed
apt:
name: docker.io
state: present
update_cache: yes
- name: Ensure pip is installed
apt:
name: pip
state: present
update_cache: yes
- name: Ensure Python Docker module is installed
apt:
name: python3-docker
state: present
- name: Ensure web root exists
file:
path: /data
state: directory
- name: Ensure user directory exists
file:
path: /data/{{ user_id }}
state: directory
- name: Ensure web root exists
file:
path: /data/{{ user_id }}/{{ site }}
state: directory
- name: Copy all files from local dir to remote dir
synchronize:
src: "../files/{{ user_id }}/{{ site }}/"
dest: "/data/{{ user_id }}/{{ site }}/"
recursive: yes
delete: no
- name: Build dynamic Traefik labels
set_fact:
traefik_labels: "{{traefik_labels | default({}) | combine ({ item.key : item.value }) }}"
with_items:
- { 'key': 'traefik.enable', 'value': 'true'}
- { 'key': 'traefik.http.routers.site-{{ user_id }}-{{ site }}.rule', 'value': 'Host(`{{ domain }}`)'}
- { 'key': 'traefik.http.routers.site-{{ user_id }}-{{ site }}.entrypoints', 'value': 'websecure'}
- { 'key': 'traefik.http.services.site-{{ user_id }}-{{ site }}.loadbalancer.server.port', 'value': '80'}
- { 'key': 'traefik.http.routers.site-{{ user_id }}-{{ site }}.tls.certresolver', 'value': 'le'}
- name: Run Nginx container
docker_container:
name: nginx-{{ user_id }}-{{ site }}
image: nginx:alpine
state: started
restart_policy: always
networks:
- name: traefik
labels: "{{ traefik_labels }}"
volumes:
- "/data/{{ user_id }}/{{ site }}:/usr/share/nginx/html:ro"

3
ansible/vars/exemple.yml Normal file
View File

@ -0,0 +1,3 @@
user_id: "1"
site: "exemple_site"
domain: "exemple.portfolio.sortifal.fr"