GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Come installare Ansible su Ubuntu 18.04

In questo articolo, avremo spiegato i passaggi necessari per installare e configurare Ansible su Ubuntu 18.04 LTS. Prima di continuare con questo tutorial, assicurati di aver effettuato l'accesso come utente con privilegi sudo. Tutti i comandi in questo tutorial devono essere eseguiti come utente non root.

Ansible è uno strumento di configurazione e automazione gratuito e open source per sistemi operativi simili a UNIX. È scritto in Python e simile a Chef o Puppet, ma c'è una differenza e il vantaggio di Ansible è che non è necessario installare alcun agente sui nodi. Utilizza SSH per comunicare con i suoi nodi.

In questo articolo dimostreremo come installare e configurare Ansible in Ubuntu e proveremo a gestirne i due nodi:

  • Server Ansible – ansible.idroot.us ( 192.168.13.33 )
  • Cliente Ansible – 192.168.100.120

Installa Ansible su Ubuntu

Passaggio 1. Innanzitutto, prima di iniziare a installare qualsiasi pacchetto sul tuo server Ubuntu, ti consigliamo sempre di assicurarti che tutti i pacchetti di sistema siano aggiornati.

sudo apt update
sudo apt upgrade

Passaggio 2. Installa Ansible su Ubuntu.

Dobbiamo prima abilitare il repository ansible. Installa il repository con il seguente comando:

sudo apt-add-repository ppa:ansible/ansible

Dopo aver aggiunto il repository Ansible, esegui i seguenti comandi per l'installazione:

sudo apt update
sudo apt install ansible

Dopo aver installato correttamente Ansible, puoi verificare la versione di ansible installata eseguendo:

sudo ansible --version

Passaggio 3. Imposta la configurazione SSH meno password tra il server ansible e il client ansible.

Dobbiamo generare chiavi ssh sul server ansible e copiare questa chiave nella chiave pubblica sul client 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] ~]#

Possiamo usare il comando ssh-copy-id per copiare la chiave pubblica dal server ansible ai client 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] ~]#

Dobbiamo controllare che il nostro server ansible e che i client ansible funzionino con meno password o no? Possiamo controllare come di seguito:

[[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] ~]#

Passaggio 4. Configurazione del server Ansible per gestire il client ansible.

Puoi fare l'inventario del nostro server ansible e del nostro client ansible utilizzando il file host del server ansible. Il file host può essere trovato in questo percorso sul server ansible /etc/ansible/host:

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

Passaggio 5. Gestisci il client ansible dal server ansible.

Verifica la connettività di "server di test" o client ansible utilizzando 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] ~]#

Possiamo controllare il tempo di attività del nostro client ansible da un server 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] ~]#

Reindirizzamento dell'output del comando a un file. Come di seguito:

[[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] ~]#

Questo è tutto ciò che devi fare per installare Ansible su Ubuntu 18.04. Spero che tu possa trovare utile questo suggerimento rapido. Se hai domande o suggerimenti, sentiti libero di lasciare un commento qui sotto.


Ubuntu
  1. Come installare R su Ubuntu 20.04

  2. Come installare Vai su Ubuntu 18.04

  3. Come installare Ansible su Ubuntu Server 21.04

  4. Come installare Ansible su Ubuntu 18.04 LTS

  5. Come installare R su Ubuntu 16.04

Come installare Ansible su Ubuntu 20.04

Come installare Ansible su Ubuntu 16.04 (Xenial)

Come installare OpenVPN su Ubuntu 16.04 LTS

Come installare Go in Ubuntu 20.04

Come installare Vai su Ubuntu 22.04

Come installare Ansible Server su Ubuntu 18.04