Added all
This commit is contained in:
59
.gitignore
vendored
Normal file
59
.gitignore
vendored
Normal file
@@ -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
|
||||||
100
README.md
Normal file
100
README.md
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
```markdown
|
||||||
|
[](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.
|
||||||
|
|
||||||
|
[](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)
|
||||||
|
```
|
||||||
56
Vagrantfile
vendored
Normal file
56
Vagrantfile
vendored
Normal file
@@ -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
|
||||||
9
ansible.sh
Normal file
9
ansible.sh
Normal file
@@ -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
|
||||||
BIN
arquitectura-ansible.pdf
Normal file
BIN
arquitectura-ansible.pdf
Normal file
Binary file not shown.
3
collections/requirements.yml
Normal file
3
collections/requirements.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
collections:
|
||||||
|
- name: community.general
|
||||||
|
- name: community.mysql
|
||||||
52
group_vars/all.yml
Normal file
52
group_vars/all.yml
Normal file
@@ -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
|
||||||
2
group_vars/vault.yml
Normal file
2
group_vars/vault.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
vault_db_root_password: YourRootPassword
|
||||||
|
vault_db_password: YourDBPassword
|
||||||
12
inventory/hosts
Normal file
12
inventory/hosts
Normal file
@@ -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
|
||||||
30
inventory/hosts-awx
Normal file
30
inventory/hosts-awx
Normal file
@@ -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
|
||||||
12
inventory/hosts.ini
Normal file
12
inventory/hosts.ini
Normal file
@@ -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
|
||||||
38
roles/database/README.md
Normal file
38
roles/database/README.md
Normal file
@@ -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).
|
||||||
3
roles/database/defaults/main.yml
Normal file
3
roles/database/defaults/main.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
# defaults file for database
|
||||||
5
roles/database/handlers/main.yml
Normal file
5
roles/database/handlers/main.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
- name: Reiniciar MySQL
|
||||||
|
ansible.builtin.service:
|
||||||
|
name: mysql
|
||||||
|
state: restarted
|
||||||
35
roles/database/meta/main.yml
Normal file
35
roles/database/meta/main.yml
Normal file
@@ -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.
|
||||||
87
roles/database/tasks/main.yml
Normal file
87
roles/database/tasks/main.yml
Normal file
@@ -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
|
||||||
3
roles/database/tests/inventory
Normal file
3
roles/database/tests/inventory
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
localhost
|
||||||
|
|
||||||
6
roles/database/tests/test.yml
Normal file
6
roles/database/tests/test.yml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- database
|
||||||
2
roles/database/vars/main.yml
Normal file
2
roles/database/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
---
|
||||||
|
vault_db_password: "YourDBPassword"
|
||||||
38
roles/loadbalancer/README.md
Normal file
38
roles/loadbalancer/README.md
Normal file
@@ -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).
|
||||||
3
roles/loadbalancer/defaults/main.yml
Normal file
3
roles/loadbalancer/defaults/main.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
# defaults file for loadbalancer
|
||||||
5
roles/loadbalancer/handlers/main.yml
Normal file
5
roles/loadbalancer/handlers/main.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
- name: Reiniciar Nginx
|
||||||
|
ansible.builtin.service:
|
||||||
|
name: nginx
|
||||||
|
state: restarted
|
||||||
35
roles/loadbalancer/meta/main.yml
Normal file
35
roles/loadbalancer/meta/main.yml
Normal file
@@ -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.
|
||||||
31
roles/loadbalancer/tasks/main.yml
Normal file
31
roles/loadbalancer/tasks/main.yml
Normal file
@@ -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
|
||||||
16
roles/loadbalancer/templates/nginx.conf.j2
Normal file
16
roles/loadbalancer/templates/nginx.conf.j2
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
3
roles/loadbalancer/tests/inventory
Normal file
3
roles/loadbalancer/tests/inventory
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
localhost
|
||||||
|
|
||||||
6
roles/loadbalancer/tests/test.yml
Normal file
6
roles/loadbalancer/tests/test.yml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- loadbalancer
|
||||||
3
roles/loadbalancer/vars/main.yml
Normal file
3
roles/loadbalancer/vars/main.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
# vars file for loadbalancer
|
||||||
38
roles/webserver/README.md
Normal file
38
roles/webserver/README.md
Normal file
@@ -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).
|
||||||
3
roles/webserver/defaults/main.yml
Normal file
3
roles/webserver/defaults/main.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
# defaults file for webserver
|
||||||
5
roles/webserver/handlers/main.yml
Normal file
5
roles/webserver/handlers/main.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
- name: Reiniciar Apache
|
||||||
|
ansible.builtin.service:
|
||||||
|
name: apache2
|
||||||
|
state: restarted
|
||||||
35
roles/webserver/meta/main.yml
Normal file
35
roles/webserver/meta/main.yml
Normal file
@@ -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.
|
||||||
64
roles/webserver/tasks/main.yml
Normal file
64
roles/webserver/tasks/main.yml
Normal file
@@ -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
|
||||||
13
roles/webserver/templates/wordpress.conf.j2
Normal file
13
roles/webserver/templates/wordpress.conf.j2
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<VirtualHost *:80>
|
||||||
|
ServerAdmin webmaster@localhost
|
||||||
|
DocumentRoot {{ wp_install_dir }}
|
||||||
|
|
||||||
|
<Directory {{ wp_install_dir }}>
|
||||||
|
Options FollowSymLinks
|
||||||
|
AllowOverride All
|
||||||
|
Require all granted
|
||||||
|
</Directory>
|
||||||
|
|
||||||
|
ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log
|
||||||
|
CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined
|
||||||
|
</VirtualHost>
|
||||||
20
roles/webserver/templates/wp-config.php.j2
Normal file
20
roles/webserver/templates/wp-config.php.j2
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
define( 'DB_NAME', '{{ db_name }}' );
|
||||||
|
define( 'DB_USER', '{{ db_user }}' );
|
||||||
|
define( 'DB_PASSWORD', '{{ db_password }}' );
|
||||||
|
define( 'DB_HOST', '{{ db_host }}' );
|
||||||
|
define( 'DB_CHARSET', 'utf8mb4' );
|
||||||
|
define( 'DB_COLLATE', '' );
|
||||||
|
|
||||||
|
{{ lookup('pipe', 'curl -s https://api.wordpress.org/secret-key/1.1/salt/') }}
|
||||||
|
|
||||||
|
$table_prefix = 'wp_';
|
||||||
|
|
||||||
|
define( 'WP_DEBUG', false );
|
||||||
|
define( 'WP_HOME', '{{ wp_url }}' );
|
||||||
|
define( 'WP_SITEURL', '{{ wp_url }}' );
|
||||||
|
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
define( 'ABSPATH', __DIR__ . '/' );
|
||||||
|
}
|
||||||
|
require_once ABSPATH . 'wp-settings.php';
|
||||||
3
roles/webserver/tests/inventory
Normal file
3
roles/webserver/tests/inventory
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
localhost
|
||||||
|
|
||||||
6
roles/webserver/tests/test.yml
Normal file
6
roles/webserver/tests/test.yml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#SPDX-License-Identifier: MIT-0
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- webserver
|
||||||
2
roles/webserver/vars/main.yml
Normal file
2
roles/webserver/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
---
|
||||||
|
vault_db_password: "YourDBPassword"
|
||||||
3
secrets/vault.yml
Normal file
3
secrets/vault.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
vault_db_password: "YourDBPassword"
|
||||||
|
vault_wp_admin_password: "YourAdminPassword"
|
||||||
1
secrets/vault_pass.txt
Normal file
1
secrets/vault_pass.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
MyVaultPassword1234
|
||||||
21
site.yml
Normal file
21
site.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
- hosts: database
|
||||||
|
become: true
|
||||||
|
vars_files:
|
||||||
|
- group_vars/vault.yml
|
||||||
|
roles:
|
||||||
|
- database
|
||||||
|
|
||||||
|
- hosts: webserver
|
||||||
|
become: true
|
||||||
|
vars_files:
|
||||||
|
- group_vars/vault.yml
|
||||||
|
roles:
|
||||||
|
- webserver
|
||||||
|
|
||||||
|
- hosts: loadbalancer
|
||||||
|
become: true
|
||||||
|
vars_files:
|
||||||
|
- group_vars/vault.yml
|
||||||
|
roles:
|
||||||
|
- loadbalancer
|
||||||
Reference in New Issue
Block a user