Quantcast
Channel: Blog Virtualizacion
Viewing all articles
Browse latest Browse all 679

Ansible: Instalación servidores web

$
0
0

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:

[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:

[root@TERRAFORM ansible]# cd /etc/ansible/inventory/
[root@TERRAFORM inventory]# vi web
[web]
nginx ansible_ssh_host=192.168.2.36

Crearemos un Playbook:

[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:

[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:

ansible-instalacion-servidores-web-2

Si vamos a la URL:

ansible-instalacion-servidores-web-3

Si volvemos a lanzarlo, nos validará que lo hemos instalado y no es necesario instalar nginx:

ansible-instalacion-servidores-web-4

Configurar página estática personalizada Nginx con Ansible

Ahora os voy a mostrar un ejemplo más avanzado. Generamos un fichero index.html:

<header>
 <h1>Hello World. Probando una app nginx con Ansible. ¡ElBlogDeNegu!</h1>
</header>

Personalizamos nuestro Playbook:

[root@TERRAFORM ansible]# vi playbooks/nginx-avanzado.yml

#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:

ansible-instalacion-servidores-web-5

Y volvemos a la URL:

ansible-instalacion-servidores-web-6

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?

La entrada Ansible: Instalación servidores web se publicó primero en Blog Virtualizacion.


Viewing all articles
Browse latest Browse all 679