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 😉

Trasferire cartelle tra due hosts locale-remoto in Linux con SCP

 

Trasferire cartelle tra due hosts locale-remoto in Linux con SCP

Il comando SCP in Linux è usato per trasferire files o cartelle in maniera sicura usando ssh, tra server locali e/o remoti.

Trasferire cartelle tra due hosts locale-remoto in Linux con SCP

 

Copiare file da locale a remoto:

# scp -v /path/local/file.txt remoteuser@remotehost:/remote/path/

se si utilizza una porta differente:

# scp -v -P 2200 /path/local/file.txt remoteuser@remotehost:/remote/path/

Copiare file da remoto a locale

# scp -v remoteuser@remotehost:/path/file.txt /path/local

Copiare cartella in maniera ricorsiva e compressa da locale a remoto:

# scp -vrC -P 2200 /path/local/folder remoteuser@remotehost:/remote/path/

Copiare cartella in maniera ricorsiva e compressa da remoto a locale:

# scp -vrC -P 2200 remoteuser@remotehost:/remote/folder/ /path/local/

Limitare la banda:

# scp -vrC -l 500 -P 2200 /path/folder remoteuser@remotehost:/remote/path/

Copiare file/cartella tra 2 hosts remoti:

# scp -v remoteuser1@remotehost1:/path/file.txt remoteuser2@remotehost2::/path/directory/

 

enjpy 😉

Backup del Sistema con Rsync

Backup del Sistema con Rsync

Backup del Sistema con Rsync. Rsync è un leggero e potente strumento per il backup. Permette di eseguire backup incrementali, salvando i soli file modificati o aggiunti rispetto all’ultima operazione di backup, sia in locale che in remoto. Si può utilizzare per un backup dell’intero sistema o di cartelle specifiche.

Installazione:

# apt install rsync -y

Rsync locale:

# rsync -zav /percorso/origine /percorso/backup

per cancellare nella cartella di destinazione i file non più presenti nella cartella di origine basta aggiungere l’opzione –delete:

# rsync --delete -zav /percorso/origine /percorso/backup

Backup del Sistema:

per escludere dal backup files o cartelle si utilizza l’opzione –exclude:

# rsync -zaAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /percorso/backup

Rsync da locale a remoto con ssh:

$ rsync -zavh -e ssh /cartella/origine_file/ remoteuser@remotehost:/cartella/backup

se si utilizza una porta diversa per ssh:

$ rsync -zavh -e 'ssh -p 2200' /cartella/origine_file/ remoteuser@remotehost:/cartella/backup

per visualizzare il progresso del backup:

$ rsync -zavh -e 'ssh -p 2200' --progress /cartella/origine_file/ remoteuser@remotehost:/cartella/backup

Rsync da remoto a locale con ssh:

$ rsync -zvah -e 'ssh -p 2200' remoteuser@remotehost:/percorso/del/file /cartella/destinazione

Rsync senza password ssh:

# ssh-keygen

alle domande successive premere sempre invio e copiare la chiave sul server:

# ssh-copy-id -i $HOME/.ssh/id_rsa.pub remoteuser@remotehost

a questo punto se tutto è andato bene non verrà più richiesta la password e si può accedere direttamente:

# ssh remoteuser@remotehost

Backup automatico:

a questo punto si può decidere che tipo di backup effettuare:

ogni giorno alle 23:00

00 23 * * *

ogni sabato alle 23:00

00 23 * * 6

oppure al 1° di ogni mese alle 23:00:

00 23 1 * *

$ crontab -e

ed inserire l’opzione desiderata:

00 23 * * * rsync -zav /percorso/origine /percorso/backup

Backup del Sistema con Rsync

enjoy 😉

 

Installare DirectAdmin su CentOS e Debian Squeeze

 

 

 

CentOS:

# yum install wget gcc gcc-c++ flex bison make bind bind-libs bind-utils openssl openssl-devel perl quota libaio libcom_err-devel libcurl-dev gd zlib-devel zip unzip libcap-devel cronie

Debian

# apt-get install gcc g++ make flex bison openssl libssl-dev perl perl-base perl-modules libperl-dev libaio1 libaio-dev

 

# wget https://www.directadmin.com/setup.sh
# chmod 755 setup.sh
# ./setup.sh

inserire Client ID ed License ID, e continuare con l'installazione che è molto intuitiva, e prenderà un po di tempo. DirectAdmin sarà raggiungibile all'indirizzo https://IP_Server:2222. Per autenticarsi il nome utente sarà admin e la password con tutti gli altri dati sarà in /usr/local/directadmin/scripts/setup.txt

 

enjoy 😉