Este artículo propone scripts para automatizar:
- la instalación de Raspbian en Raspberry Pi mediante un script bash casero
- una configuración y seguridad mínima de Raspbian mediante Ansible con un rol casero.
Instalación de Raspbian
Antes de comenzar nos colocamos en un directorio de trabajo, por ejemplo ~/rpi
Luego insertamos la microSD (a través de un adaptador, por ejemplo) en su estación de trabajo y localizamos la ruta de la microSD
Las herramientas dmesg o lsblk ayudan a ubicarla
## Dans ces exemples la microSD a été détectée sur sdd ce qui donne comme chemin /dev/sdd
## via dmesg
jls42@boo:~/rpi$ sudo dmesg |tail
[94655.145112] scsi host8: usb-storage 1-1.2:1.0
[94656.175566] scsi 8:0:0:0: Direct-Access Generic USB SD Reader 1.00 PQ: 0 ANSI: 0 CCS
[94656.176298] sd 8:0:0:0: Attached scsi generic sg4 type 0
[94656.177494] sd 8:0:0:0: [sdd] 62333952 512-byte logical blocks: (31.9 GB/29.7 GiB)
[94656.178010] sd 8:0:0:0: [sdd] Write Protect is off
[94656.178014] sd 8:0:0:0: [sdd] Mode Sense: 4b 00 00 08
[94656.178638] sd 8:0:0:0: [sdd] No Caching mode page found
[94656.178647] sd 8:0:0:0: [sdd] Assuming drive cache: write through
[94656.182648] sdd: sdd1 sdd2
[94656.185135] sd 8:0:0:0: [sdd] Attached SCSI removable disk
## ou via lsblk
jls42@boo:~/rpi$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
[...]
sdd 8:48 1 29,7G 0 disk
├─sdd1 8:49 1 43,9M 0 part
└─sdd2 8:50 1 1,7G 0 part
A continuación, podemos instalar Raspbian en la microSD, por ejemplo, mediante install_raspbian.sh
Durante esta instalación, se creará un punto de montaje /mnt/sdd
#!/bin/bash
# Ici on configure le chemin de la microSD
# et le point de montage nécessaire à la bonne exécution du script
msd_fs="/dev/sdd"
mountpoint="/mnt/sdd"
[ -d ${mountpoint} ] || mkdir ${mountpoint}
[ -b ${msd_fs} ] || { echo "${msd_fs} non disponible" ; exit 1 ;}
#raspbian_version="2018-06-27-raspbian-stretch-lite.zip"
raspbian_version=""
if [ -z ${raspbian_version} ]
then
raspbian_version=$(curl -s https://downloads.raspberrypi.org/raspbian_lite_latest | \
grep "raspbian_lite"| perl -nle 'print $1 if /.*href=".*images\/.*\/([0-9].*)"/')
if [ -e ${raspbian_version} ]
then
echo "La dernière image de Raspbian est déjà présente"
else
echo "Téléchargement de la dernière image de Raspbian"
curl -s -L https://downloads.raspberrypi.org/raspbian_lite_latest --output ${raspbian_version}
fi
fi
echo -e "Déploiement de ${raspbian_version} to ${msd_fs}"
time unzip -p ${raspbian_version} | sudo dd of=${msd_fs} bs=4M conv=fsync && \
echo -e "Montage de ${msd_fs}1 to ${mountpoint}" && \
sudo mount ${msd_fs}1 ${mountpoint} && \
echo -e "Activation du SSH sur Rasbpian" && \
sudo touch ${mountpoint}/ssh && \
echo -e "Vérification de l'activation SSH" && \
sudo ls -l ${mountpoint}/ssh && \
echo -e "Démontage ${msd_fs}1" && \
sudo umount ${mountpoint}
Le damos permisos de ejecución y lo ejecutamos con los permisos de root
chmod 700 install_raspbian.sh
sudo ./install_raspbian.sh
Lo que da:
jls42@boo:~/rpi$ sudo ./install_raspbian.sh
Téléchargement de la dernière image de Raspbian
Déploiement de 2018-11-13-raspbian-stretch-lite.zip to /dev/sdd
0+28463 enregistrements lus
0+28463 enregistrements écrits
1866465280 bytes (1,9 GB, 1,7 GiB) copied, 102,911 s, 18,1 MB/s
real 1m42,923s
user 0m14,203s
sys 0m2,851s
Montage de /dev/sdd1 to /mnt/sdd
Activation du SSH sur Rasbpian
Vérification de l'activation SSH
-rwxr-xr-x 1 root root 0 déc. 8 22:45 /mnt/sdd/ssh
Démontage /dev/sdd1
A este nivel, la tarjeta microSD está preparada, podemos insertarla directamente en su Raspberry Pi.
Configuración y seguridad de Raspbian
Una vez insertada la tarjeta microSD y el Raspberry Pi es accesible mediante SSH, realizamos lo siguiente:
- Recuperamos el rol ansible de inicialización
- Preparamos el libro de jugadas de ansible
- Ejecutamos el script de Ansible
Recuperación del rol ansible ansible-rpi-init
He desarrollado un rol ansible para esta automatización, aquí está cómo recuperarlo:
git clone git@gitlab.com:jls42/ansible-rpi-init.git roles/ansible-rpi-init
Lo que da:
jls42@boo:~/rpi$ git clone https://gitlab.com/jls42/ansible-rpi-init.git roles/ansible-rpi-init
Clonage dans 'roles/ansible-rpi-init'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 17 (delta 0), reused 0 (delta 0)
Dépaquetage des objets: 100% (17/17), fait.
Creación del archivo de inventario llamado inventory
En este ejemplo de inventario, el Raspberry Pi tiene la ip 192.168.1.190
[rpi1]
192.168.1.190 servername=rpi1
Generación de un hash de password mediante mkpasswd y guardamos el resultado para la acción siguiente
## ici le mot de passe est 'osef'
jls42@boo:~$ mkpasswd --method=sha-512
Mot de passe :
$6$KRDQqQeZT$Bu439CILeJqHmwoxCwHXsgTJ5JEQjpZWfzCw6Deggfhip.DGScoCnUSBi.Hqnkh1OZUhTBaQees6GooW.PSLe.
Creación de un libro de jugadas de Ansible llamado init_rpi.yml
---
- name: "Initialisation du Rapsberry Pi"
hosts: rpi1
remote_user: "pi"
roles:
- roles/ansible-rpi-init
vars:
rpi_password: "$6$KRDQqQeZT$Bu439CILeJqHmwoxCwHXsgTJ5JEQjpZWfzCw6Deggfhip.DGScoCnUSBi.Hqnkh1OZUhTBaQees6GooW.PSLe."
Generación de una pareja de claves ssh si no tenemos una ya
jls42@boo:~$ [ -f ~/.ssh/id_rsa.pub ] || ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jls42/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jls42/.ssh/id_rsa.
Your public key has been saved in /home/jls42/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qHhedMjuI0LySVAp2mpFQMozuqBwI00FZo0oHX7rkYI jls42@boo
The key's randomart image is:
+---[RSA 2048]----+
|.=**. |
|*+=o. |
|+B+ . |
|+++o + o |
|Eo* + = S |
|*=o= = . |
|+=..+ o |
| +o.o. |
| ..... |
+----[SHA256]-----+
A este nivel, los siguientes archivos están presentes
jls42@boo:~/rpi$ tree
.
├── 2018-11-13-raspbian-stretch-lite.zip
├── init_rpi.yml
├── install_raspbian.sh
├── inventory
└── roles
└── ansible-rpi-init
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── LICENSE
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
8 directories, 13 files
Lanzamiento de Ansible con el libro de jugadas init_rpi.yml
## Le mot de passe par défaut est : "raspberry"
export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook -k -b -i inventory init_rpi.yml
Lo que da
jls42@boo:~/rpi$ ansible-playbook -k -b -i inventory init_rpi.yml
SSH password:
PLAY [Initialisation du Rapsberry Pi] ******************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
Saturday 08 December 2018 23:46:18 +0100 (0:00:00.050) 0:00:00.050 *****
ok: [192.168.1.190]
TASK [roles/ansible-rpi-init : Mise à jour raspbian] ***************************************************************************************************************************************************************************************
Saturday 08 December 2018 23:46:22 +0100 (0:00:03.502) 0:00:03.553 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Configuration du nom] ***************************************************************************************************************************************************************************************
Saturday 08 December 2018 23:47:53 +0100 (0:01:31.114) 0:01:34.667 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Ajoute le nom du serveur dans le fichier /etc/hosts] ********************************************************************************************************************************************************
Saturday 08 December 2018 23:47:55 +0100 (0:00:01.944) 0:01:36.612 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Ajoute la clef publique] ************************************************************************************************************************************************************************************
Saturday 08 December 2018 23:47:56 +0100 (0:00:01.031) 0:01:37.643 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Mise à jour du mot de passe de l'utilisateur pi] ************************************************************************************************************************************************************
Saturday 08 December 2018 23:47:57 +0100 (0:00:01.183) 0:01:38.826 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Redémarrage] ************************************************************************************************************************************************************************************************
Saturday 08 December 2018 23:47:58 +0100 (0:00:01.249) 0:01:40.075 *****
changed: [192.168.1.190]
TASK [roles/ansible-rpi-init : Attente suite au redémarrage] *******************************************************************************************************************************************************************************
Saturday 08 December 2018 23:48:00 +0100 (0:00:01.714) 0:01:41.790 *****
ok: [192.168.1.190]
PLAY RECAP *********************************************************************************************************************************************************************************************************************************
192.168.1.190 : ok=8 changed=6 unreachable=0 failed=0
Saturday 08 December 2018 23:49:00 +0100 (0:01:00.451) 0:02:42.241 *****
===============================================================================
roles/ansible-rpi-init ------------------------------------------------ 158.69s
setup ------------------------------------------------------------------- 3.50s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total ----------------------------------------------------------------- 162.19s
Playbook run took 0 days, 0 hours, 2 minutes, 42 seconds
Y voilà:
- La contraseña de acceso para el usuario “pi” ha sido cambiada
- El acceso es ahora posible sin contraseña
- El Raspberry Pi tiene el nombre indicado en servername (a través del archivo de inventario)
Verificación
Nos conectamos al Raspberry Pi para confirmar el buen acceso sin contraseña y el nombre correcto
jls42@boo:~/rpi$ ssh pi@192.168.1.190
Linux rpi1 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Dec 8 22:47:59 2018 from 192.168.1.42
pi@rpi1:~ $
¡Disfruta!
**Este documento ha sido traducido desde la versión fr por el modelo mistral-medium.
Note: This is a direct translation of the given sentence into Spanish, without any additions or explanations as requested. The sentence refers to a document that has been translated from the French version using the Mistral-medium translation model.**