commit 2c6c4242ce226a008168efa9a34641e6740c5f33 Author: Guillem Hernandez Sola Date: Wed May 20 18:52:35 2026 +0200 Added all diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c947ced --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ +# macOS +.DS_Store +.AppleDouble +.LSOverride +Icon? +._* +.Spotlight-V100 +.Trashes +.fseventsd + +# Visual Studio Code +.vscode/ +*.code-workspace + +# PHP +/vendor/ +/composer.phar +/.phpunit.result.cache +.php_cs.cache +.php_cs.dist +.env +.env.*.local +*.log +*.cache + +# Python +__pycache__/ +*.py[cod] +*$py.class +.Python +env/ +venv/ +.venv/ +pip-wheel-metadata/ +build/ +dist/ +*.egg-info/ +.eggs/ +*.egg +.pytest_cache/ +.mypy_cache/ +.tox/ +.coverage +htmlcov/ + +# IDEs and editors +.idea/ +*.swp +*~ + +### Vagrant ### +# General +.vagrant/ + +# Log files (if you are creating logs in debug mode, uncomment this) +# *.log + +### Vagrant Patch ### +*.box \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cff3ea0 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ +```markdown +[![Agile611](https://www.agile611.com/wp-content/uploads/2020/09/cropped-logo-header.png)](http://www.agile611.com/) + +# Agile611 WordPress Ansible (Vagrant) + +Plantilla per desplegar un entorn WordPress utilitzant Vagrant i Ansible. Inclou una màquina de control (Ansible), servidor web, base de dades i balancejador de càrrega, tots definits al `Vagrantfile`. + +**Objectiu:** facilitar el proveïment i les proves locals d'una arquitectura WordPress multinodo utilitzant caixes `bento/ubuntu-24.04`. + +--- + +## Contingut principal + +- **`Vagrantfile`**: definició de les màquines (ansible, database, loadbalancer, webserver). +- **`ansible.sh`** (si existeix): script de proveïment inicial utilitzat per la màquina `ansible`. +- **Playbooks/rols**: afegeix els teus playbooks d'Ansible a una carpeta `ansible/` o similar. + +--- + +## Requisits + +- `Vagrant` (>= 2.2.x) +- `VirtualBox` (o un altre proveïdor suportat per Vagrant) +- Opcional: `ansible` a la màquina amfitriona si prefereixes executar playbooks des de fora de la VM + +--- + +## Guia ràpida + +Aixecar totes les màquines: + +```bash +vagrant up --provider=virtualbox +``` + +Aixecar una màquina concreta (per exemple `webserver`): + +```bash +vagrant up webserver +``` + +Accedir per SSH a la màquina de control (Ansible): + +```bash +vagrant ssh ansible +``` + +Des de la màquina `ansible` (o des del teu host si tens Ansible instal·lat), executa els teus playbooks: + +```bash +# des de la VM de control +ansible-playbook -i inventory/hosts site.yml + +# o des del host (si està configurat) +ansible-playbook -i inventory/hosts site.yml --private-key=path/to/key +``` + +--- + +## Notes i recomanacions + +- Revisa i ajusta les IPs privades al `Vagrantfile` si entren en conflicte amb la teva xarxa local. +- Si utilitzes `rsync` per sincronitzar carpetes, recorda que pot requerir `rsync` instal·lat a la màquina amfitriona. +- Mantén `composer.lock` al repositori si el teu projecte PHP depèn de versions concretes; `vendor/` normalment s'ignora (està al `.gitignore`). + +--- + +## Estructura suggerida + +``` +Vagrantfile +ansible/ +├── inventory/ +├── playbooks/ +└── roles/ +ansible.sh (opcional) +``` + +--- + +## Contribucions + +Obre issues o PRs per suggerir millores. Descriu els canvis i els passos per reproduir-los. + +--- + +## Llicència + +Aquest tutorial és alliberat al domini públic per [Agile611](http://www.agile611.com/) sota la llicència Creative Commons Attribution-NonCommercial 4.0 International. + +[![License: CC BY-NC 4.0](https://img.shields.io/badge/License-CC_BY--NC_4.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc/4.0/) + +Aquest fitxer README va ser escrit originalment per [Guillem Hernández Sola](https://www.linkedin.com/in/guillemhs/) i és igualment alliberat al domini públic. + +Contacta amb Agile611 per a més informació. + +- [Agile611](http://www.agile611.com/) +- Laureà Miró 309 +- 08950 Esplugues de Llobregat (Barcelona) +``` \ No newline at end of file diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..6f97981 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,56 @@ +Vagrant.configure(2) do |config| + # Máquina de control para el agente Ansible + config.vm.define "ansible" do |control| + control.vm.box = "bento/ubuntu-24.04" # Imagen base Ubuntu 24.04 + control.vm.network "private_network", ip: "192.168.11.11" # IP privada + control.vm.hostname = "ansible" # Nombre de host + control.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync" # Carpeta sincronizada + control.vm.provider "virtualbox" do |vb| + vb.memory = 512 # Memoria RAM asignada + vb.cpus = 1 # Número de CPUs asignadas + end + control.vm.provision :shell, :path => "ansible.sh" # Script de aprovisionamiento + end + + # Máquina para la base de datos + config.vm.define "database" do |database| + database.vm.box = "bento/ubuntu-24.04" + database.vm.network "private_network", ip: "192.168.11.20" + database.vm.hostname = "database" + database.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync" + database.vm.network "forwarded_port", guest: 80, host: 8081 # Redirección del puerto 80 + database.vm.network "forwarded_port", guest: 3306, host: 3306 # Redirección del puerto MySQL + database.vm.provider "virtualbox" do |vb| + vb.memory = 512 + vb.cpus = 1 + end + end + + # Máquina para el balanceador de carga + config.vm.define "loadbalancer" do |loadbalancer| + loadbalancer.vm.box = "bento/ubuntu-24.04" + loadbalancer.vm.network "private_network", ip: "192.168.11.30" + loadbalancer.vm.hostname = "loadbalancer" + loadbalancer.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync" + loadbalancer.vm.network "forwarded_port", guest: 80, host: 8080 # Redirección del puerto 80 + loadbalancer.vm.network "forwarded_port", guest: 3306, host: 33061 # Redirección del puerto MySQL alternativo + loadbalancer.vm.provider "virtualbox" do |vb| + vb.memory = 512 + vb.cpus = 1 + end + end + + # Máquina para el servidor web + config.vm.define "webserver" do |webserver| + webserver.vm.box = "bento/ubuntu-24.04" + webserver.vm.network "private_network", ip: "192.168.11.40" + webserver.vm.hostname = "webserver" + webserver.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync" + webserver.vm.network "forwarded_port", guest: 80, host: 80 # Redirección del puerto 80 + webserver.vm.network "forwarded_port", guest: 3306, host: 33062 # Redirección del puerto MySQL alternativo + webserver.vm.provider "virtualbox" do |vb| + vb.memory = 512 + vb.cpus = 1 + end + end + end \ No newline at end of file diff --git a/ansible.sh b/ansible.sh new file mode 100644 index 0000000..12dd5e5 --- /dev/null +++ b/ansible.sh @@ -0,0 +1,9 @@ +# Script to install Ansible on a Ubuntu system +apt-get update +# Install required packages +apt install software-properties-common -y +# Add Ansible PPA and install Ansible +apt-add-repository ppa:ansible/ansible +apt-get install ansible net-tools -y +# Add vagrant user to sudoers +echo "vagrant ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/vagrant \ No newline at end of file diff --git a/arquitectura-ansible.pdf b/arquitectura-ansible.pdf new file mode 100644 index 0000000..1313e10 Binary files /dev/null and b/arquitectura-ansible.pdf differ diff --git a/collections/requirements.yml b/collections/requirements.yml new file mode 100644 index 0000000..8a5df97 --- /dev/null +++ b/collections/requirements.yml @@ -0,0 +1,3 @@ +collections: + - name: community.general + - name: community.mysql diff --git a/group_vars/all.yml b/group_vars/all.yml new file mode 100644 index 0000000..e4bbe7e --- /dev/null +++ b/group_vars/all.yml @@ -0,0 +1,52 @@ +--- +# Versions +wordpress_version: "6.8.3" +wp_version: "{{ wordpress_version }}" +wordpress_url: "https://wordpress.org/wordpress-{{ wordpress_version }}.zip" + +# Base de dades +db_root_password: "{{ vault_db_root_password }}" +db_name: "wordpress" +db_user: "wordpress" +db_password: "{{ vault_db_password }}" +db_host: "192.168.11.20" +wordpress_db_host: "192.168.11.20" +mysql_bind_address: "0.0.0.0" +mysql_port: 3306 + +# WordPress +wp_install_dir: "/var/www/html/wordpress" +wp_url: "http://192.168.11.40" +wp_site_url: "http://192.168.11.40" +wp_admin_user: "admin" +wp_admin_password: "{{ vault_wp_admin_password }}" +wp_admin_email: "admin@example.com" + +# Xarxa +webserver_ip: "192.168.11.40" +lb_port: 80 +http_port: 80 +https_port: 443 + +# Paquets +packages_webserver: + - apache2 + - php + - php-cli + - php-fpm + - php-mysql + - php-xml + - php-curl + - php-mbstring + - php-zip + - libapache2-mod-php + - unzip + - wget + +packages_database: + - mysql-server + - python3-pip + - python3-pymysql + +packages_loadbalancer: + - nginx \ No newline at end of file diff --git a/group_vars/vault.yml b/group_vars/vault.yml new file mode 100644 index 0000000..52b37e2 --- /dev/null +++ b/group_vars/vault.yml @@ -0,0 +1,2 @@ +vault_db_root_password: YourRootPassword +vault_db_password: YourDBPassword \ No newline at end of file diff --git a/inventory/hosts b/inventory/hosts new file mode 100644 index 0000000..6742a71 --- /dev/null +++ b/inventory/hosts @@ -0,0 +1,12 @@ +[database] +192.168.11.20 + +[webserver] +192.168.11.40 + +[loadbalancer] +192.168.11.30 + +[all:vars] +ansible_user=vagrant +ansible_connection: ssh diff --git a/inventory/hosts-awx b/inventory/hosts-awx new file mode 100644 index 0000000..95e8ad3 --- /dev/null +++ b/inventory/hosts-awx @@ -0,0 +1,30 @@ +# Ansible inventory file in YAML format ready for Ansible and AWX +all: + ansible_python_interpreter: /usr/bin/python3.12 + +database: + hosts: + db_01: + ansible_host: 192.168.11.20 + ansible_connection: ssh + ansible_user: vagrant + ansible_ssh_password: vagrant +loadbalancer: + hosts: + lb_01: + ansible_host: 192.168.11.30 + ansible_connection: ssh + ansible_user: vagrant + ansible_ssh_password: vagrant +webserver: + hosts: + ws_01: + ansible_host: 192.168.11.40 + ansible_connection: ssh + ansible_user: vagrant + ansible_ssh_password: vagrant + ws_02: + ansible_host: 192.168.11.50 + ansible_connection: ssh + ansible_user: vagrant + ansible_ssh_password: vagrant \ No newline at end of file diff --git a/inventory/hosts.ini b/inventory/hosts.ini new file mode 100644 index 0000000..39c880f --- /dev/null +++ b/inventory/hosts.ini @@ -0,0 +1,12 @@ +# Ansible inventory file in YAML format ready for Ansible and AWX +[all] +ansible_python_interpreter=/usr/bin/python3.12 + +[database] +db_01 ansible_host=192.168.11.20 ansible_connection=ssh ansible_user=vagrant ansible_ssh_password=vagrant + +[loadbalancer] +lb_01 ansible_host=192.168.11.30 ansible_connection=ssh ansible_user=vagrant ansible_ssh_password=vagrant + +[webserver] +web_01 ansible_host=192.168.11.40 ansible_connection=ssh ansible_user=vagrant ansible_ssh_password=vagrant \ No newline at end of file diff --git a/roles/database/README.md b/roles/database/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/roles/database/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/database/defaults/main.yml b/roles/database/defaults/main.yml new file mode 100644 index 0000000..67db565 --- /dev/null +++ b/roles/database/defaults/main.yml @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +--- +# defaults file for database diff --git a/roles/database/handlers/main.yml b/roles/database/handlers/main.yml new file mode 100644 index 0000000..5572226 --- /dev/null +++ b/roles/database/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Reiniciar MySQL + ansible.builtin.service: + name: mysql + state: restarted \ No newline at end of file diff --git a/roles/database/meta/main.yml b/roles/database/meta/main.yml new file mode 100644 index 0000000..6f91fd3 --- /dev/null +++ b/roles/database/meta/main.yml @@ -0,0 +1,35 @@ +#SPDX-License-Identifier: MIT-0 +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.2 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/roles/database/tasks/main.yml b/roles/database/tasks/main.yml new file mode 100644 index 0000000..a20177c --- /dev/null +++ b/roles/database/tasks/main.yml @@ -0,0 +1,87 @@ +--- +- name: Instalar paquetes de la base de datos + ansible.builtin.apt: + name: "{{ packages_database }}" + state: present + update_cache: yes + notify: Reiniciar MySQL + +- name: Corregir permisos de /etc/mysql/my.cnf + ansible.builtin.file: + path: /etc/mysql/my.cnf + owner: root + group: root + mode: '0644' + +- name: Configurar /etc/mysql/my.cnf correctament + ansible.builtin.copy: + dest: /etc/mysql/my.cnf + content: | + !includedir /etc/mysql/conf.d/ + !includedir /etc/mysql/mysql.conf.d/ + owner: root + group: root + mode: '0644' + notify: Reiniciar MySQL + +- name: Configurar bind-address per acceptar connexions remotes + ansible.builtin.lineinfile: + path: /etc/mysql/mysql.conf.d/mysqld.cnf + regexp: '^bind-address' + line: 'bind-address = 0.0.0.0' + notify: Reiniciar MySQL + +- name: Configurar mysqlx-bind-address + ansible.builtin.lineinfile: + path: /etc/mysql/mysql.conf.d/mysqld.cnf + regexp: '^mysqlx-bind-address' + line: 'mysqlx-bind-address = 0.0.0.0' + notify: Reiniciar MySQL + +- name: Assegurar que MySQL està en execució + ansible.builtin.service: + name: mysql + state: started + enabled: yes + +- name: Flush handlers (reinicia MySQL abans de crear usuaris) + ansible.builtin.meta: flush_handlers + +# ✅ Crear /root/.my.cnf perquè community.mysql pugui autenticar-se +- name: Crear /root/.my.cnf amb credencials de root + ansible.builtin.copy: + dest: /root/.my.cnf + content: | + [client] + user=root + password={{ db_root_password }} + owner: root + group: root + mode: '0600' + +- name: DEBUG - Mostrar contrasenya que usa Ansible + ansible.builtin.debug: + msg: "db_root_password = '{{ db_root_password }}'" + +- name: Crear la base de dades de WordPress + community.mysql.mysql_db: + name: "{{ db_name }}" + state: present + login_user: root + login_password: "{{ db_root_password }}" + +- name: Crear l'usuari de WordPress amb accés remot + community.mysql.mysql_user: + name: "{{ db_user }}" + password: "{{ db_password }}" + priv: "{{ db_name }}.*:ALL" + host: "%" + state: present + login_user: root + login_password: "{{ db_root_password }}" + +# ✅ Eliminar /root/.my.cnf per seguretat un cop acabat +- name: Eliminar /root/.my.cnf per seguretat + ansible.builtin.file: + path: /root/.my.cnf + state: absent \ No newline at end of file diff --git a/roles/database/tests/inventory b/roles/database/tests/inventory new file mode 100644 index 0000000..03ca42f --- /dev/null +++ b/roles/database/tests/inventory @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +localhost + diff --git a/roles/database/tests/test.yml b/roles/database/tests/test.yml new file mode 100644 index 0000000..aad42cf --- /dev/null +++ b/roles/database/tests/test.yml @@ -0,0 +1,6 @@ +#SPDX-License-Identifier: MIT-0 +--- +- hosts: localhost + remote_user: root + roles: + - database diff --git a/roles/database/vars/main.yml b/roles/database/vars/main.yml new file mode 100644 index 0000000..1c4b6bb --- /dev/null +++ b/roles/database/vars/main.yml @@ -0,0 +1,2 @@ +--- +vault_db_password: "YourDBPassword" \ No newline at end of file diff --git a/roles/loadbalancer/README.md b/roles/loadbalancer/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/roles/loadbalancer/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/loadbalancer/defaults/main.yml b/roles/loadbalancer/defaults/main.yml new file mode 100644 index 0000000..94faab5 --- /dev/null +++ b/roles/loadbalancer/defaults/main.yml @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +--- +# defaults file for loadbalancer diff --git a/roles/loadbalancer/handlers/main.yml b/roles/loadbalancer/handlers/main.yml new file mode 100644 index 0000000..24ebed6 --- /dev/null +++ b/roles/loadbalancer/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Reiniciar Nginx + ansible.builtin.service: + name: nginx + state: restarted \ No newline at end of file diff --git a/roles/loadbalancer/meta/main.yml b/roles/loadbalancer/meta/main.yml new file mode 100644 index 0000000..6f91fd3 --- /dev/null +++ b/roles/loadbalancer/meta/main.yml @@ -0,0 +1,35 @@ +#SPDX-License-Identifier: MIT-0 +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.2 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/roles/loadbalancer/tasks/main.yml b/roles/loadbalancer/tasks/main.yml new file mode 100644 index 0000000..1519135 --- /dev/null +++ b/roles/loadbalancer/tasks/main.yml @@ -0,0 +1,31 @@ +--- +- name: Instalar Nginx + ansible.builtin.apt: + name: "{{ packages_loadbalancer }}" + state: present + update_cache: yes + +- name: Copiar configuració de Nginx + ansible.builtin.template: + src: nginx.conf.j2 + dest: /etc/nginx/sites-available/wordpress + notify: Reiniciar Nginx + +- name: Activar el site de WordPress a Nginx + ansible.builtin.file: + src: /etc/nginx/sites-available/wordpress + dest: /etc/nginx/sites-enabled/wordpress + state: link + notify: Reiniciar Nginx + +- name: Eliminar el site per defecte de Nginx + ansible.builtin.file: + path: /etc/nginx/sites-enabled/default + state: absent + notify: Reiniciar Nginx + +- name: Assegurar que Nginx està en execució + ansible.builtin.service: + name: nginx + state: started + enabled: yes \ No newline at end of file diff --git a/roles/loadbalancer/templates/nginx.conf.j2 b/roles/loadbalancer/templates/nginx.conf.j2 new file mode 100644 index 0000000..8252d65 --- /dev/null +++ b/roles/loadbalancer/templates/nginx.conf.j2 @@ -0,0 +1,16 @@ +upstream wordpress_backend { + server {{ webserver_ip }}:80; +} + +server { + listen 80; + server_name _; + + location / { + proxy_pass http://wordpress_backend; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} \ No newline at end of file diff --git a/roles/loadbalancer/tests/inventory b/roles/loadbalancer/tests/inventory new file mode 100644 index 0000000..03ca42f --- /dev/null +++ b/roles/loadbalancer/tests/inventory @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +localhost + diff --git a/roles/loadbalancer/tests/test.yml b/roles/loadbalancer/tests/test.yml new file mode 100644 index 0000000..94d2704 --- /dev/null +++ b/roles/loadbalancer/tests/test.yml @@ -0,0 +1,6 @@ +#SPDX-License-Identifier: MIT-0 +--- +- hosts: localhost + remote_user: root + roles: + - loadbalancer diff --git a/roles/loadbalancer/vars/main.yml b/roles/loadbalancer/vars/main.yml new file mode 100644 index 0000000..5d284cd --- /dev/null +++ b/roles/loadbalancer/vars/main.yml @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +--- +# vars file for loadbalancer diff --git a/roles/webserver/README.md b/roles/webserver/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/roles/webserver/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/webserver/defaults/main.yml b/roles/webserver/defaults/main.yml new file mode 100644 index 0000000..b2383de --- /dev/null +++ b/roles/webserver/defaults/main.yml @@ -0,0 +1,3 @@ +#SPDX-License-Identifier: MIT-0 +--- +# defaults file for webserver diff --git a/roles/webserver/handlers/main.yml b/roles/webserver/handlers/main.yml new file mode 100644 index 0000000..02f9e39 --- /dev/null +++ b/roles/webserver/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Reiniciar Apache + ansible.builtin.service: + name: apache2 + state: restarted \ No newline at end of file diff --git a/roles/webserver/meta/main.yml b/roles/webserver/meta/main.yml new file mode 100644 index 0000000..6f91fd3 --- /dev/null +++ b/roles/webserver/meta/main.yml @@ -0,0 +1,35 @@ +#SPDX-License-Identifier: MIT-0 +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.2 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/roles/webserver/tasks/main.yml b/roles/webserver/tasks/main.yml new file mode 100644 index 0000000..155b2a2 --- /dev/null +++ b/roles/webserver/tasks/main.yml @@ -0,0 +1,64 @@ +--- +- name: Instalar paquetes del servidor web + ansible.builtin.apt: + name: "{{ packages_webserver }}" + state: present + update_cache: yes + +- name: Assegurar que Apache està en execució + ansible.builtin.service: + name: apache2 + state: started + enabled: yes + +- name: Crear directori d'instal·lació de WordPress + ansible.builtin.file: + path: "{{ wp_install_dir }}" + state: directory + owner: www-data + group: www-data + mode: '0755' + +- name: Descarregar WordPress + ansible.builtin.get_url: + url: "https://wordpress.org/wordpress-{{ wp_version }}.tar.gz" + dest: /tmp/wordpress.tar.gz + +- name: Extreure WordPress + ansible.builtin.unarchive: + src: /tmp/wordpress.tar.gz + dest: /var/www/html/ + remote_src: yes + owner: www-data + group: www-data + creates: "{{ wp_install_dir }}/wp-login.php" + +- name: Copiar wp-config.php + ansible.builtin.template: + src: wp-config.php.j2 + dest: "{{ wp_install_dir }}/wp-config.php" + owner: www-data + group: www-data + mode: '0640' + +- name: Configurar VirtualHost d'Apache per WordPress + ansible.builtin.template: + src: wordpress.conf.j2 + dest: /etc/apache2/sites-available/wordpress.conf + notify: Reiniciar Apache + +- name: Activar el site de WordPress + ansible.builtin.command: + cmd: a2ensite wordpress.conf + notify: Reiniciar Apache + +- name: Activar mòdul rewrite d'Apache + ansible.builtin.apache2_module: + name: rewrite + state: present + notify: Reiniciar Apache + +- name: Desactivar el site per defecte + ansible.builtin.command: + cmd: a2dissite 000-default.conf + notify: Reiniciar Apache \ No newline at end of file diff --git a/roles/webserver/templates/wordpress.conf.j2 b/roles/webserver/templates/wordpress.conf.j2 new file mode 100644 index 0000000..ad6511b --- /dev/null +++ b/roles/webserver/templates/wordpress.conf.j2 @@ -0,0 +1,13 @@ + + ServerAdmin webmaster@localhost + DocumentRoot {{ wp_install_dir }} + + + Options FollowSymLinks + AllowOverride All + Require all granted + + + ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log + CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined + \ No newline at end of file diff --git a/roles/webserver/templates/wp-config.php.j2 b/roles/webserver/templates/wp-config.php.j2 new file mode 100644 index 0000000..d3eb93d --- /dev/null +++ b/roles/webserver/templates/wp-config.php.j2 @@ -0,0 +1,20 @@ +