Ansible: Instalación servidores web
Hoy os vamos a enseñar como configurar un servidor web a través de Ansible. En mi caso lo voy a hacer con contenedores LXC de Proxmox. Y aunque en el ejemplo lo haremos sobre solo servidor, podéis hacerlo por un número infinito a la vez.
Lo primero de todo es copiar la clave de nuestro servidor de Ansible en los servers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@TERRAFORM ansible]# ssh-copy-id -i /root/.ssh/id_rsa SERVIDORWEB [root@TERRAFORM ansible]# ssh-copy-id -i /root/.ssh/id_rsa 192.168.2.36 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.2.36 (192.168.2.36)' can't be established. ECDSA key fingerprint is SHA256:DO2qpRPr6dClF8bMuMUDXMe0sRDxYCKtCp7kTvgQRR8. ECDSA key fingerprint is MD5:38:06:f4:17:69:ef:e2:51:5d:0d:81:0c:40:35:3c:2c. Are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.2.36's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.2.36'" and check to make sure that only the key(s) you wanted were added. |
Crearemos un inventario con nuestros servidores:
1 2 3 4 |
[root@TERRAFORM ansible]# cd /etc/ansible/inventory/ [root@TERRAFORM inventory]# vi web [web] nginx ansible_ssh_host=192.168.2.36 |
Crearemos un Playbook:
1 2 3 4 5 6 7 8 9 10 |
[root@TERRAFORM ansible]# cd /etc/ansible/playbooks/ [root@TERRAFORM playbooks]# vi nginx.yml --- - name: Este Playbook instala nginx hosts: web tasks: - name: Esta tarea instala nginx yum: name: nginx state: latest |
Y lanzamos la ejecución:
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@TERRAFORM ansible]# ansible-playbook playbooks/nginx.yml -i inventory/web PLAY [Este Playbook instala nginx] *********************************************************************** TASK [Gathering Facts] *********************************************************************************** ok: [nginx] TASK [Esta tarea instala nginx] ************************************************************************** changed: [nginx] PLAY RECAP *********************************************************************************************** nginx : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Podemos comprobar si existe el paquete de la forma tradicional:
Si vamos a la URL:
Si volvemos a lanzarlo, nos validará que lo hemos instalado y no es necesario instalar nginx:
Configurar página estática personalizada Nginx con Ansible
Ahora os voy a mostrar un ejemplo más avanzado. Generamos un fichero index.html:
1 2 3 |
<header> <h1>Hello World. Probando una app nginx con Ansible. ¡ElBlogDeNegu!</h1> </header> |
Personalizamos nuestro Playbook:
1 |
[root@TERRAFORM ansible]# vi playbooks/nginx-avanzado.yml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#nginx-avanzado.yml --- - hosts: web user: root tasks: - name: Añadir epel-repo yum: name: epel-release state: present - name: Instalar Nginx yum: name: nginx state: present - name: Añadir fichero index.html template: src: /etc/ansible/index.html dest: /usr/share/nginx/html/index.html #SI CONFIGURAIS FWs #- name: Acceso a través del puerto TCP 80 # firewalld: # port: 80/tcp # zone: public # state: enabled - name: Arrancar Nginx service: name: nginx state: started |
Lo lanzamos:
Y volvemos a la URL:
Ni que decir tiene, que podéis desplegar una web entera en varios nodos con un sólo clic. Seguiremos jugando con automatizaciones.
¿Te ha gustado la entrada SÍGUENOS EN TWITTER?
Te ha gustado la entrada SGUENOS EN TWITTER O INVITANOS A UN CAFE?