Ansible su Debian 12 e SysLinuxOS

Ansible su Debian 12 e SysLinuxOS

Ansible su Debian 12 e SysLinuxOS

 

Guida su come installare Ansible su Debian 12 e SysLnuxOS 12. Ansible è un tool di automazione open source che viene utilizzato per automatizzare le attività IT, come la gestione della configurazione, il provisioning, il deployment e l’orchestrazione.

**Alcuni esempi di utilizzo di Ansible**

* Gestione della configurazione di server e macchine virtuali
* Deployment di applicazioni e software
* Orchestrazione di processi IT
* Provisionig di infrastrutture cloud

Installazione

Si può installare tramite apt:

$ sudo apt update
$ sudo apt install ansible -y

oppure tramite pip:

$ sudo apt install python3 python3-pip -y
$ pip install ansible --break-system-packages

in questo ultimo caso il PATH sarà in .local/bin/ansible, quindi:

$ export PATH=$PATH:/home/$USER/.local/bin

per rendere definitiva la modifica inserire il comando in .bashrc. Nel caso non fosse presente:

$ nano ~/.bashrc

ed inserire:

PATH=$PATH:~/bin
export PATH=$PATH:/home/$USER/.local/bin

quindi testare:

$ ansible --version

ansible [core 2.15.5]
config file = None
configured module search path = ['/home/edmond/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/edmond/.local/lib/python3.11/site-packages/ansible
ansible collection location = /home/edmond/.ansible/collections:/usr/share/ansible/collections
executable location = /home/edmond/.local/bin/ansible
python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True

Ansible deve essere installato su uno dei nodi. Il nodo di gestione è noto come Control Node. Questo nodo avrà il file Ansible Playbook. Questo è un file YAML che contiene i passaggi che l’utente desidera eseguire su una o più macchine normalmente denominate managed nodes.

Prerequisiti

Per questa guida ho usato 3 server:

Esempio ip_address
Control Node (SysLinuxOS 12) 192.168.1.168
Managed Node 1 (server 1 Debian 12) 192.168.1.200
Managed Node 2 (server 2 Raspberry Py OS 12) 192.168.1.251
Creazione Hosts Inventory file

questo file si occuperà del collegamento con i managed node:

$ mkdir ~/project
$ nano  ~/project/hosts

ed inserire ip ed username dei nodi da automatizzare:

[servers]
server1 ansible_host=192.168.1.200 ansible_user=edmond ansible_ssh_port=22
server2 ansible_host=192.168.1.251 ansible_user=edmond ansible_ssh_port=22

dopo se non si ha l’accesso ssh, si va a creare una chiave, che verrà copiata sui 2 server:

Creazione e copia chiave ssh
$ sudo su
# ssh-keygen
# ssh-copy-id root@192.168.1.200
# ssh-copy-id root@192.168.1.25
Utilizzo moduli Ansible

Sintassi:

$ ansible -i <host_file> -m <module> <host>

Dove:

  • -i ~/hosts: contiene la lista degli hosts
  • -m: specifica il modulo come ping,  shell ecc.ecc.
  • <host>: Ansible hosts dove lanciare i moduli

Utilizzare ping usando ansible ping module:

$ ansible -i ~/project/hosts -m ping all

output ping:

edmond@edmondbox:~$ ansible -i ~/project/hosts -m ping all
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
server1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}

Utilizzo shell usando ansible shell module:

$ ansible -i ~/project/hosts -m shell -a "uptime" all

output uptime:

$ ansible -i ~/project/hosts -m shell -a "uptime" all

server2 | CHANGED | rc=0 >>
19:51:43 up 1 day, 3:00, 1 user, load average: 0.35, 0.11, 0.08
server1 | CHANGED | rc=0 >>
19:51:44 up 3:36, 1 user, load average: 0.00, 0.00, 0.00

output df:

$ ansible -i ~/project/hosts -m shell -a "df -h" all

server1 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
udev 661M 0 661M 0% /dev
tmpfs 185M 1.8M 184M 1% /run
/dev/mmcblk0p2 117G 8.0G 103G 8% /
tmpfs 925M 0 925M 0% /dev/shm
tmpfs 5.0M 16K 5.0M 1% /run/lock
/dev/mmcblk0p1 510M 61M 450M 12% /boot/firmware
tmpfs 185M 0 185M 0% /run/user/1000
server2 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
udev 1.6G 0 1.6G 0% /dev
tmpfs 380M 1.2M 379M 1% /run
/dev/mmcblk0p2 59G 11G 45G 19% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 16K 5.0M 1% /run/lock
/dev/mmcblk0p1 510M 61M 450M 12% /boot/firmware
tmpfs 380M 0 380M 0% /run/user/1000

output free:

$ ansible -i ~/project/hosts -m shell -a "free -m" all

server1 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 1848 732 126 13 1005 1115
Swap: 99 0 99
server2 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3793 577 1916 45 1378 3215
Swap: 99 0 99Utilizzo modulo apt
Utilizzo modulo apt

Con il modulo apt, si possono utilizzare i classici comandi di apt update, apt upgrade, apt install ecc ecc. In questo caso useremo playbook.yaml.

1) apt update, apt upgrade, e in caso di nuovo kernel, reboot

$ nano ~/project/upgrade.yml

inserire:

---
- hosts: servers
become: yes
become_user: root
tasks:
- name: Update apt repo and cache on all Debian boxes
apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

- name: Upgrade all packages on servers
apt: upgrade=dist force_apt_get=yes

- name: Check if a reboot is needed on all servers
register: reboot_required_file
stat: path=/var/run/reboot-required get_md5=no

- name: Reboot the box if kernel updated
reboot:
msg: "Reboot initiated by Ansible for kernel updates"
connect_timeout: 5
reboot_timeout: 300
pre_reboot_delay: 0
post_reboot_delay: 30
test_command: uptime
when: reboot_required_file.stat.exists

comando apt upgrade:

$ ansible-playbook project/upgrade.yml -i project/hosts

output:

BECOME password:

PLAY [servers] *****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server2]
ok: [server1]

TASK [Update apt repo and cache on all Debian boxes] ***************************
changed: [server2]
changed: [server1]

TASK [Upgrade all packages on servers] *****************************************
ok: [server2]
ok: [server1]

TASK [Check if a reboot is needed on all servers] ******************************
ok: [server2]
ok: [server1]

TASK [Reboot the box if kernel updated] ****************************************
skipping: [server1]
skipping: [server2]

PLAY RECAP *********************************************************************
server1 : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 
server2 : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

2) Installazione singolo pacchetto bc

$ nano ~/project/package.yml

inserire:

- hosts: all
become: yes
tasks:
- name : Install the latest bc package
apt: name=bc state=latest update_cache=true

comando con opzione -K per eseguire il comando da root:

$ ansible-playbook project/package.yml -i project/hosts -K

output:

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server2]
ok: [server1]

TASK [Install the latest bc package] *******************************************
changed: [server2]
changed: [server1]

PLAY RECAP *********************************************************************
server1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

3) Installazione pacchetto 7zip con output debug managed nodes

$ nano ~/project/output.yml

inserire:

- hosts: all
become: yes
tasks:
- name: Capture the Output
apt: name=7zip state=present update_cache=true
register: apt_output
- debug: var=apt_output

comando:

$ ansible-playbook project/output.yml -i project/hosts -K

output:

 "(Reading database ... 65%",
"(Reading database ... 70%",
"(Reading database ... 75%",
"(Reading database ... 80%",
"(Reading database ... 85%",
"(Reading database ... 90%",
"(Reading database ... 95%",
"(Reading database ... 100%",
"(Reading database ... 76140 files and directories currently installed.)",
"Preparing to unpack .../7zip_22.01+dfsg-8_arm64.deb ...",
"Unpacking 7zip (22.01+dfsg-8) ...",
"Setting up 7zip (22.01+dfsg-8) ...",
"Processing triggers for man-db (2.11.2-2) ..."

4) Installazione multipla e verifica pacchetti installati

$ nano ~/project/packages.yml

inserire:

---
- hosts: all
become: yes
tasks:
- name: Update apt cache and make sure Wget, Curl and Terminator are installed
apt:
name: "{{ item }}"
update_cache: yes
loop:
- wget
- curl
- terminator

comando:

$ ansible-playbook project/packeges.yml -i project/hosts -K

output:

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server2]
ok: [server1]

TASK [Update apt cache and make sure Wget, Curl and Terminator are installed] ***
ok: [server2] => (item=wget)
ok: [server1] => (item=wget)
ok: [server2] => (item=curl)
ok: [server1] => (item=curl)
changed: [server2] => (item=terminator)
changed: [server1] => (item=terminator)

PLAY RECAP *********************************************************************
server1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

come si può notare i pacchetti wget e curl sono presenti su entrambi i server, mentre terminator viene invece installato.

 

Ansible su Debian 12 e SysLinuxOS

enjoy 😉