Files
projet_gestion_commande/ansible/playbook-frontend-backend.yml
T

235 lines
7.0 KiB
YAML

---
- name: Installation et configuration du frontend et backend
hosts: nginx
become: true
gather_facts: true
tasks:
- name: Update and upgrade
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600
upgrade: "dist"
- name: install basic dependencies
ansible.builtin.apt:
name:
- curl
- wget
- tar
- rsync
- acl
state: present
update_cache: yes
- name: Download Go 1.23.0 tarball
ansible.builtin.get_url:
url: https://dl.google.com/go/go1.23.0.linux-amd64.tar.gz
dest: /tmp/go1.23.0.linux-amd64.tar.gz
mode: "0644"
- name: Extract Go 1.23.0
ansible.builtin.unarchive:
src: /tmp/go1.23.0.linux-amd64.tar.gz
dest: /usr/local
remote_src: yes
- name: Ensure Go 1.23 is in PATH for all users
ansible.builtin.lineinfile:
path: /etc/profile.d/go.sh
line: "export PATH=/usr/local/go/bin:$PATH"
create: yes
state: present
mode: "0644"
- name: Setup Node.js 20 repository
ansible.builtin.shell: |
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
args:
executable: /bin/bash
- name: Install Node.js 20
ansible.builtin.apt:
name: nodejs
state: present
update_cache: yes
dpkg_options: "force-overwrite"
- name: Check Node.js, npm and Go versions
ansible.builtin.shell: |
echo "Node: $(node -v)"
echo "npm: $(npm -v)"
echo "Go: $(go version)"
register: versions_check
changed_when: false
failed_when: versions_check.rc != 0
- name: Display all versions
ansible.builtin.debug:
msg: "{{ versions_check.stdout_lines }}"
# ============================================
# FRONTEND
# ============================================
- name: Create /var/www if not exists
ansible.builtin.file:
path: /var/www
state: directory
owner: root
group: root
mode: "0755"
- name: Ensure /home/ubuntu exists with correct permissions
ansible.builtin.file:
path: /home/ubuntu
state: directory
owner: "{{ user_deploy }}"
group: "{{ user_deploy }}"
mode: "0755"
- name: Remove old frontend directory if exists
ansible.builtin.file:
path: "{{ nginx_frontend_path }}"
state: absent
- name: Create frontend directory with deploy user ownership
ansible.builtin.file:
path: "{{ nginx_frontend_path }}"
state: directory
owner: "{{ user_deploy }}"
group: "{{ user_deploy }}"
mode: "0755"
- name: Synchronize frontend content (excluding node_modules and .git)
ansible.builtin.synchronize:
src: ../frontend/
dest: "{{ nginx_frontend_path }}/"
rsync_opts:
- "--exclude=node_modules"
- "--exclude=.git"
- "--exclude=dist"
- "--exclude=build"
delete: no
recursive: yes
tags:
- syncro
- name: Install npm dependencies as deploy user
become_user: "{{ user_deploy }}"
ansible.builtin.shell: |
export PATH=/usr/local/go/bin:$PATH
npm install
npm run build
args:
chdir: "{{ nginx_frontend_path }}"
executable: /bin/bash
- name: Set ownership to www-data for runtime
ansible.builtin.file:
path: "{{ nginx_frontend_path }}"
owner: "{{ user_web }}"
group: "{{ user_web }}"
mode: "0755"
recurse: yes
# ============================================
# BACKEND
# ============================================
- name: Remove old backend directory if exists
ansible.builtin.file:
path: "{{ backend_dir }}"
state: absent
- name: Create backend directory with deploy user ownership
ansible.builtin.file:
path: "{{ backend_dir }}"
state: directory
owner: "{{ user_deploy }}"
group: "{{ user_deploy }}"
mode: "0755"
- name: Copy backend files
ansible.builtin.copy:
src: "../backendprod/gestion/"
dest: "{{ backend_dir }}/"
owner: "{{ user_deploy }}"
group: "{{ user_deploy }}"
mode: preserve
directory_mode: "0755"
- name: Compile backend as deploy user
become_user: "{{ user_deploy }}"
ansible.builtin.shell: |
export PATH=/usr/local/go/bin:$PATH
go mod tidy
go build -o main .
args:
chdir: "{{ backend_dir }}"
executable: /bin/bash
tags:
- compile
- name: Set ownership to www-data for runtime
ansible.builtin.file:
path: "{{ backend_dir }}"
owner: "{{ user_web }}"
group: "{{ user_web }}"
mode: "0755"
recurse: yes
- name: Set executable permission on backend binary
ansible.builtin.file:
path: "{{ backend_binary }}"
owner: "{{ user_web }}"
group: "{{ user_web }}"
mode: "0755"
# ============================================
# SYSTEMD SERVICES
# ============================================
- name: Create systemd service for backend
ansible.builtin.template:
src: ./templates/backend.service.j2
dest: /etc/systemd/system/backend.service
owner: root
group: root
mode: "0644"
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: yes
- name: Enable and restart frontend service
ansible.builtin.systemd:
name: frontend
enabled: yes
state: restarted
- name: Enable and restart backend service
ansible.builtin.systemd:
name: backend
enabled: yes
state: restarted
- name: Wait for backend to be ready
ansible.builtin.wait_for:
port: "{{ backend_port }}"
delay: 2
timeout: 30
- name: Display service status
ansible.builtin.shell: |
echo "=== Frontend Service ==="
systemctl status frontend --no-pager || true
echo ""
echo "=== Backend Service ==="
systemctl status backend --no-pager || true
register: service_status
changed_when: false
- name: Show service status
ansible.builtin.debug:
msg: "{{ service_status.stdout_lines }}"