From 5620e1f4dca7d6ad1eea8bc4679d2ff9c3914119 Mon Sep 17 00:00:00 2001 From: Guillem Hernandez Sola Date: Tue, 17 Jun 2025 13:51:26 +0200 Subject: [PATCH] Training from 20250617 --- misc/nginx.conf | 2 +- training/control-flujo.yml | 9 +++++++++ training/facts.yml | 23 +++++++++++++++++++++++ training/handlers.yml | 15 +++++++++++++++ training/install-joe.yml | 8 ++++++++ training/install-nginx.yml | 21 +++++++++++++++++++++ training/inventory | 8 ++++++++ training/loops.yml | 18 ++++++++++++++++++ training/passlib.yml | 21 +++++++++++++++++++++ training/practica-dia2.yml | 3 +++ training/reiniciar-nginx.yml | 15 +++++++++++++++ training/user-password.yml | 20 ++++++++++++++++++++ training/users.yml | 19 +++++++++++++++++++ training/var.yml | 20 ++++++++++++++++++++ 14 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 training/control-flujo.yml create mode 100644 training/facts.yml create mode 100644 training/handlers.yml create mode 100644 training/install-joe.yml create mode 100644 training/install-nginx.yml create mode 100644 training/inventory create mode 100644 training/loops.yml create mode 100644 training/passlib.yml create mode 100644 training/practica-dia2.yml create mode 100644 training/reiniciar-nginx.yml create mode 100644 training/user-password.yml create mode 100644 training/users.yml create mode 100644 training/var.yml diff --git a/misc/nginx.conf b/misc/nginx.conf index 9b8dc01..a41ce98 100644 --- a/misc/nginx.conf +++ b/misc/nginx.conf @@ -53,4 +53,4 @@ http { try_files $uri $uri/ =404; } } -} +} \ No newline at end of file diff --git a/training/control-flujo.yml b/training/control-flujo.yml new file mode 100644 index 0000000..c7b7aea --- /dev/null +++ b/training/control-flujo.yml @@ -0,0 +1,9 @@ +- name: Prueba de when + hosts: all + become: yes + tasks: + - name: Instalar Apache2 si el sistema operativo es Debian + apt: + name: apache2 + state: present + when: ansible_facts['os_family'] == 'Debian' \ No newline at end of file diff --git a/training/facts.yml b/training/facts.yml new file mode 100644 index 0000000..9f85639 --- /dev/null +++ b/training/facts.yml @@ -0,0 +1,23 @@ +- name: Mirar los ansible_facts + hosts: all + become: yes + tasks: + - name: Verificar los hechos de Ansible + ansible.builtin.debug: + var: ansible_facts + + - name: Mostrar sistema operativo + ansible.builtin.debug: + msg: "El sistema operativo es {{ ansible_facts['os_family'] }}" + + - name: Mostrar el nombre del host + ansible.builtin.debug: + msg: "El nombre del host es {{ ansible_facts['nodename'] }}" + + - name: Mostrar la versión del kernel + ansible.builtin.debug: + msg: "La versión del kernel es {{ ansible_facts['kernel'] }}" + + - name: Mostrar la versión de Ansible + ansible.builtin.debug: + msg: "La versión de Ansible es {{ ansible_version.full }}" \ No newline at end of file diff --git a/training/handlers.yml b/training/handlers.yml new file mode 100644 index 0000000..02d808c --- /dev/null +++ b/training/handlers.yml @@ -0,0 +1,15 @@ +- name: Prueba de handlers + hosts: all + become: yes + tasks: + - name: Copiar archivo de configuración + copy: + src: nginx.conf + dest: /etc/nginx/nginx.conf + notify: + - Reiniciar Nginx + handlers: + - name: Reiniciar Nginx + service: + name: nginx + state: restarted \ No newline at end of file diff --git a/training/install-joe.yml b/training/install-joe.yml new file mode 100644 index 0000000..51428b0 --- /dev/null +++ b/training/install-joe.yml @@ -0,0 +1,8 @@ +- name: Instalar editor Joe + hosts: all + become: yes + tasks: + - name: Instalar Paquete Joe + apt: + name: joe + state: absent diff --git a/training/install-nginx.yml b/training/install-nginx.yml new file mode 100644 index 0000000..c9287a9 --- /dev/null +++ b/training/install-nginx.yml @@ -0,0 +1,21 @@ +- name: Instalar Nginx + hosts: all + become: yes + tasks: + - name: Instalar Paquete Nginx + apt: + name: nginx + state: present + + - name: Iniciar el servicio Nginx + service: + name: nginx + state: started + enabled: yes + + - name: Verificar el estado del servicio Nginx + service_facts: + + - name: Mostrar estado del servicio Nginx + debug: + msg: "El servicio Nginx está {{ ansible_facts.services['nginx.service'].state }}" \ No newline at end of file diff --git a/training/inventory b/training/inventory new file mode 100644 index 0000000..b876044 --- /dev/null +++ b/training/inventory @@ -0,0 +1,8 @@ +[database] +192.168.11.20 + +[loadbalancer] +192.168.11.30 + +[webserver] +192.168.11.40 diff --git a/training/loops.yml b/training/loops.yml new file mode 100644 index 0000000..019d5db --- /dev/null +++ b/training/loops.yml @@ -0,0 +1,18 @@ +- name: Prueba de multiples usuarios + hosts: all + become: yes + tasks: + - name: Instalar el paquete 'python3-passlib' + apt: + name: python3-passlib + state: present + + - name: Crear múltiples usuarios con contraseñas + user: + name: "{{ item }}" + password: "{{ 'password' | password_hash('sha512') }}" + state: present + with_items: + - user1 + - user2 + - user3 \ No newline at end of file diff --git a/training/passlib.yml b/training/passlib.yml new file mode 100644 index 0000000..e787d46 --- /dev/null +++ b/training/passlib.yml @@ -0,0 +1,21 @@ +- name: Passlib + hosts: all + become: yes + tasks: + - name: Install Python3 + apt: + pkg: + - python3 + - python3-pip + - python3-passlib + become: true + + - name: Create a symbolic link from python3 to python + file: + src: /usr/bin/python3 + dest: /usr/bin/python + state: link + + - name: Set ansible_python_interpreter to use the installed Python + set_fact: + ansible_python_interpreter: /usr/bin/python3 \ No newline at end of file diff --git a/training/practica-dia2.yml b/training/practica-dia2.yml new file mode 100644 index 0000000..0cce930 --- /dev/null +++ b/training/practica-dia2.yml @@ -0,0 +1,3 @@ +#Instale un servidor web (Nginx o Apache). +#Copie un archivo de configuración al directorio correspondiente. +#Reinicie el servicio si el archivo de configuración cambia. \ No newline at end of file diff --git a/training/reiniciar-nginx.yml b/training/reiniciar-nginx.yml new file mode 100644 index 0000000..b73e197 --- /dev/null +++ b/training/reiniciar-nginx.yml @@ -0,0 +1,15 @@ +- name: Reiniciar Nginx + hosts: all + become: yes + tasks: + - name: Reiniciar el servicio Nginx + service: + name: nginx + state: restarted + + - name: Verificar el estado del servicio Nginx + service_facts: + + - name: Mostrar estado del servicio Nginx + debug: + msg: "El servicio Nginx está {{ ansible_facts.services['nginx.service'].state }}" \ No newline at end of file diff --git a/training/user-password.yml b/training/user-password.yml new file mode 100644 index 0000000..247bfd6 --- /dev/null +++ b/training/user-password.yml @@ -0,0 +1,20 @@ +- name: Crear usuarios con contraseña en todos los servidores + hosts: all + become: yes + vars: + user_list: + - user1 + - user2 + - user3 + user_password: "password" # Contraseña en texto plano + tasks: + - name: Generar hash de la contraseña en el controlador (local) + ansible.builtin.set_fact: + hashed_password: "{{ user_password | password_hash('sha512') }}" + + - name: Crear usuarios con contraseña + ansible.builtin.user: + name: "{{ item }}" + password: "{{ hashed_password }}" + state: present + loop: "{{ user_list }}" \ No newline at end of file diff --git a/training/users.yml b/training/users.yml new file mode 100644 index 0000000..accacd2 --- /dev/null +++ b/training/users.yml @@ -0,0 +1,19 @@ +- name: Prueba de creación de múltiples usuarios + hosts: all + become: yes + vars: + user_password: "password123" # Contraseña en texto plano + tasks: + - name: Generar hash de la contraseña en el controlador (local) + ansible.builtin.set_fact: + hashed_password: "{{ user_password | password_hash('sha512') }}" + + - name: Crear múltiples usuarios con contraseñas + user: + name: "{{ item }}" + password: "{{ hashed_password }}" + state: present + with_items: + - user1 + - user2 + - user3 \ No newline at end of file diff --git a/training/var.yml b/training/var.yml new file mode 100644 index 0000000..d72c36f --- /dev/null +++ b/training/var.yml @@ -0,0 +1,20 @@ +- name: Variables para probar + hosts: all + become: yes + vars: + ansible_python_interpreter: /usr/bin/python3 + app_port: 8080 + tasks: + - name: Definir variables de prueba + ansible.builtin.set_fact: + var1: "Valor de la variable 1" + var2: "Valor de la variable 2" + var3: "Valor de la variable 3" + + - name: Mostrar las variables definidas + ansible.builtin.debug: + msg: + - "Variable 1: {{ var1 }}" + - "Variable 2: {{ var2 }}" + - "Variable 3: {{ var3 }}" + - "Puerto de la aplicación: {{ app_port }}" \ No newline at end of file