11---
2- - name : Configure CookieLess backend
3- hosts : all
2+ - hosts : localhost
43 become : yes
54 vars :
6- app_dir : /opt/CookieLess
7- backup_dir : /opt/backups
8- cron_backup : " 0 3 * * *" # Daily at 3 AM
9-
5+ backup_dir : /opt/CookieLess/backups
6+ retention_days : 30
7+ docker_compose_version : " 2.34.0"
108 tasks :
11- # Base system setup
12- - name : Update apt cache
13- apt :
14- update_cache : yes
9+ # Check existing installations
10+ - name : Check Docker installation
11+ command : docker --version
12+ register : docker_check
13+ changed_when : false
14+ ignore_errors : yes
15+
16+ - name : Check Docker Compose installation
17+ stat :
18+ path : /usr/local/bin/docker-compose
19+ register : docker_compose_check
1520
16- # Docker installation
21+ # Install Docker only if needed
1722 - name : Install Docker
1823 apt :
1924 name : docker.io
2025 state : present
26+ update_cache : yes
27+ when : docker_check is failed
2128
29+ # Install Docker Compose only if needed
2230 - name : Install Docker Compose
23- ansible.builtin. get_url :
24- url : https://github.com/docker/compose/releases/download/v2.28.1 /docker-compose-linux-x86_64
31+ get_url :
32+ url : " https://github.com/docker/compose/releases/download/v{{ docker_compose_version }} /docker-compose-linux-x86_64"
2533 dest : /usr/local/bin/docker-compose
2634 mode : ' 0755'
35+ when : not docker_compose_check.stat.exists
2736
28- # Application setup
29- - name : Create application directory
30- ansible.builtin.file :
31- path : " {{ app_dir }}"
32- state : directory
33- mode : ' 0755'
34-
35- - name : Synchronize backend code
36- ansible.builtin.synchronize :
37- src : ../backend/
38- dest : " {{ app_dir }}"
39- delete : yes
40-
41- - name : Create .env file
42- ansible.builtin.template :
43- src : templates/env.j2
44- dest : " {{ app_dir }}/.env"
45-
46- # MongoDB backup setup
37+ # Existing backup and deployment tasks
4738 - name : Create backup directory
48- ansible.builtin. file :
39+ file :
4940 path : " {{ backup_dir }}"
5041 state : directory
42+ owner : root
43+ group : root
44+ mode : ' 0755'
45+
46+ - name : Create MongoDB backup script
47+ copy :
48+ dest : /opt/CookieLess/backend/mongodb-backup.sh
49+ content : |
50+ #!/bin/bash
51+ docker exec cookieless-mongodb-1 mongodump \
52+ --username=${MONGO_INITDB_ROOT_USERNAME} \
53+ --password=${MONGO_INITDB_ROOT_PASSWORD} \
54+ --authenticationDatabase admin \
55+ --archive="{{ backup_dir }}/mongodb-$(date +\%Y-\%m-\%d-\%H-\%M).gz" \
56+ --gzip
5157 mode : ' 0755'
5258
53- - name : Install MongoDB backup cron
54- ansible.builtin. cron :
59+ - name : Add daily backup cron job
60+ cron :
5561 name : " MongoDB daily backup"
56- job : " docker exec cookieless-mongodb-1 mongodump --archive={{ backup_dir }}/backup-$(date +\\ %F).gz --gzip"
57- cron_file : mongodb_backup
62+ minute : " 0"
63+ hour : " 2"
64+ job : " /opt/CookieLess/backend/mongodb-backup.sh"
5865 user : root
59- minute : " {{ cron_backup.split()[0] }}"
60- hour : " {{ cron_backup.split()[1] }}"
6166
62- # Security setup
63- - name : Configure UFW firewall
64- ufw :
65- rule : allow
66- port : " {{ item }}"
67- proto : tcp
68- loop :
69- - 7880
70- - 7881
71- - 9333
67+ - name : Add backup retention policy
68+ cron :
69+ name : " Clean old MongoDB backups"
70+ minute : " 30"
71+ hour : " 3"
72+ job : " find {{ backup_dir }} -name 'mongodb-*.gz' -mtime +{{ retention_days }} -delete"
73+ user : root
74+
75+ - name : Copy backend code
76+ copy :
77+ src : ../backend
78+ dest : /opt/CookieLess
79+
80+ - name : Create .env file for docker-compose
81+ copy :
82+ dest : /opt/CookieLess/backend/.env
83+ content : |
84+ MONGO_INITDB_ROOT_USERNAME={{ lookup('env', 'MONGO_INITDB_ROOT_USERNAME') }}
85+ MONGO_INITDB_ROOT_PASSWORD={{ lookup('env', 'MONGO_INITDB_ROOT_PASSWORD') }}
86+ MONGO_HOST={{ lookup('env', 'MONGO_HOST') }}
87+ MONGO_PORT={{ lookup('env', 'MONGO_PORT') }}
7288
73- # Application deployment
7489 - name : Start containers
75- community.docker.docker_compose :
76- project_src : " {{ app_dir }}"
77- build : yes
78- restart : yes
79- recreate : always
80- remove_orphans : yes
90+ command : docker-compose -f /opt/CookieLess/docker-compose.yml up -d
0 commit comments