GNU/Linux >> Tutoriales Linux >  >> Ubuntu

Cómo instalar Ansible en Ubuntu 18.04

En este artículo habremos explicado los pasos necesarios para instalar y configurar Ansible en Ubuntu 18.04 LTS. Antes de continuar con este tutorial, asegúrese de haber iniciado sesión como usuario con privilegios de sudo. Todos los comandos de este tutorial deben ejecutarse como un usuario no root.

Ansible es una herramienta de configuración y automatización gratuita y de código abierto para sistemas operativos similares a UNIX. Está escrito en python y es similar a Chef o Puppet, pero hay una diferencia y la ventaja de Ansible es que no necesitamos instalar ningún agente en los nodos. Utiliza SSH para comunicarse con sus nodos.

En este artículo, demostraremos cómo instalar y configurar Ansible en Ubuntu e intentaremos administrar sus dos nodos:

  • Servidor Ansible:ansible.idroot.us (192.168.13.33)
  • Cliente de Ansible:192.168.100.120

Instalar Ansible en Ubuntu

Paso 1. Primero, antes de comenzar a instalar cualquier paquete en su servidor Ubuntu, siempre recomendamos asegurarse de que todos los paquetes del sistema estén actualizados.

sudo apt update
sudo apt upgrade

Paso 2. Instale Ansible en Ubuntu.

Primero debemos habilitar el repositorio ansible. Instale el repositorio con el siguiente comando:

sudo apt-add-repository ppa:ansible/ansible

Después de agregar el repositorio de Ansible, ejecute los siguientes comandos para instalar:

sudo apt update
sudo apt install ansible

Después de una instalación exitosa de Ansible, puede verificar la versión de ansible instalada ejecutando:

sudo ansible --version

Paso 3. Configure la contraseña sin la configuración SSH entre el servidor ansible y el cliente ansible.

Necesitamos generar claves ssh en el servidor ansible y copiar esta clave a la clave pública en el cliente ansible:

[[email protected] ~]# ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rqAwbwQ8/meilanamaria4D2V0fQ49amw2WKbYIA [email protected]
The key's randomart image is:
+---[RSA 4096]----+
| .oo.+o |
| . =.=.. . |
|. E o + X . o |
|.o. + + = X . |
| .o. + oSX + |
| .. o.+ = |
|o. o oo.o |
| +.. +oo |
| .o ... |
+----[SHA256]-----+
[[email protected] ~]#

Podemos usar el comando ssh-copy-id para copiar la clave pública del servidor ansible a los clientes ansible:

[[email protected] ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[[email protected] ~]#
[[email protected] ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.131 (192.168.43.131)' can't be established.
ECDSA key fingerprint is SHA256:OuHrnRxppengenkimpoi5Z+1tOWOF8eYZ5Le5MNwUQ.
ECDSA key fingerprint is MD5:78:1c:a5:72:bb:25:fa:c7:67:39:fc:91:b9:fb:b6:20.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[[email protected] ~]#
===============================
[[email protected] ~]# ssh [email protected]
Last login: Thu Sep 19 16:00:57 2019 from bezafari-c1
[[email protected] ~]# exit
logout
Connection to 192.168.100.120 closed.
[[email protected] ~]#

¿Necesitamos verificar que nuestro servidor ansible y los clientes ansibles funcionen con menos contraseña o no? Podemos verificar de la siguiente manera:

[[email protected] ~]# ssh [email protected]
Last login: Thu Jan 17 17:00:57 2019 from bezafari-c1
[[email protected] ~]# exit
logout
Connection to 192.168.100.120 closed.
[[email protected] ~]#

Paso 4. Configuración del servidor Ansible para administrar el cliente Ansible.

Puede hacer un inventario de nuestro servidor ansible y cliente ansible usando el archivo de host del servidor ansible. El archivo host se puede encontrar en esta ruta en el servidor ansible /etc/ansible/host:

[[email protected] ~]# nano /etc/ansible/hosts
[test-servers]
192.168.13.33
192.168.100.120

Paso 5. Administre el cliente ansible desde el servidor ansible.

Verifique la conectividad de los 'servidores de prueba' o clientes ansible usando ping:

[[email protected] ~]# ansible -m ping 'test-servers'
192.168.13.33 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.100.120 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[[email protected] ~]#

Podemos verificar el tiempo de actividad de nuestro cliente ansible desde el servidor ansible:

[[email protected] ~]# ansible -m command -a "uptime" 'test-servers'
192.168.100.120 | CHANGED | rc=0 >>
16:17:35 up 20 min, 3 users, load average: 0.09, 0.16, 0.29

192.168.13.33 | CHANGED | rc=0 >>
16:17:35 up 22 min, 4 users, load average: 0.18, 0.29, 0.55

[[email protected] ~]#

Redirigiendo la salida del comando a un archivo. Como a continuación:

[[email protected] ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
[[email protected] ~]# cat /tmp/command-output.txt
192.168.13.33 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 5.0G 8.0G 39% /
devtmpfs devtmpfs 482M 0 482M 0% /dev
tmpfs tmpfs 497M 212K 497M 1% /dev/shm
tmpfs tmpfs 497M 7.1M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 158M 340M 32% /boot
tmpfs tmpfs 100M 16K 100M 1% /run/user/0
192.168.100.120 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 3.9G 9.1G 30% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 921M 84K 920M 1% /dev/shm
tmpfs tmpfs 921M 8.7M 912M 1% /run
tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 157M 341M 32% /boot
tmpfs tmpfs 185M 20K 184M 1% /run/user/42
tmpfs tmpfs 185M 0 185M 0% /run/user/0
[[email protected] ~]#

Eso es todo lo que necesita hacer para instalar Ansible en Ubuntu 18.04. Espero que encuentre útil este consejo rápido. Si tiene preguntas o sugerencias, no dude en dejar un comentario a continuación.


Ubuntu
  1. Cómo instalar R en Ubuntu 20.04

  2. Cómo instalar Go en Ubuntu 18.04

  3. Cómo instalar Ansible en Ubuntu Server 21.04

  4. Cómo instalar Ansible en Ubuntu 18.04 LTS

  5. Cómo instalar R en Ubuntu 16.04

Cómo instalar Ansible en Ubuntu 20.04

Cómo instalar Ansible en Ubuntu 16.04 (Xenial)

Cómo instalar OpenVPN en Ubuntu 16.04 LTS

Cómo instalar Go en Ubuntu 20.04

Cómo instalar Go en Ubuntu 22.04

Cómo instalar el servidor Ansible en Ubuntu 18.04