Sunday, 27 September 2020

First steps to build a cluster with ansible

As I have chosen a cluster case for my very first 2GB Raspberry Pi 4b, it was only a matter of time that I find an excuse to buy a second one. The very first challenge that I found when building a cluster is to keep the packages updated. Of course, I can log in to both of them to keep them updated, but this is easier with ansible. 

To start off, I used Raspberry Pi Imager on Debian with Raspberry Pi Desktop running on my Lenovo T480s laptop from persistent live USB to flash Raspberry Pi OS on both 32 GB SD cards. I made the installation headless by merely adding an empty file named ssh to the /boot partition before ejecting the SD cards. I have added static DHCP entries for both of them in pi-hole so that I can use names instead of IP addresses to reach them. I logged in initially to both of them to change the password and then copied my ssh keys on them.

Next, I installed ansible:

jordana@dad-laptop-ac:~ $ sudo apt install ansible

Checked version to see it installed properly:

jordana@dad-laptop-ac:~ $ ansible --version
ansible 2.7.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/pi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]

Added both names configured for static DHCP and pi as username:  

jordana@dad-laptop-ac:~ $ sudo nano /etc/ansible/hosts
[pi]
pi4b1-eth
pi4b2-eth
[pi:vars]
ansible_user=pi

Then I created a playbook to bring all packages to the latest state:

jordana@dad-laptop-ac:~ $ nano apt.yml
---
  - hosts: pi
    tasks:
      - become: yes
        apt:
          force_apt_get: yes
          update_cache: yes
          name: "*"
          state: latest

Running the playbook I get the following. Notice that the second run did not cause change.

jordana@dad-laptop-ac:~ $ ansible-playbook apt.yml 

PLAY [pi] ******************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [pi4b1-eth]
ok: [pi4b2-eth]

TASK [apt] *****************************************************************************************************************************
changed: [pi4b2-eth]
changed: [pi4b1-eth]

PLAY RECAP *****************************************************************************************************************************
pi4b1-eth                  : ok=2    changed=1    unreachable=0    failed=0   
pi4b2-eth                  : ok=2    changed=1    unreachable=0    failed=0   

jordana@dad-laptop-ac:~ $ ansible-playbook apt.yml 

PLAY [pi] ******************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [pi4b1-eth]
ok: [pi4b2-eth]

TASK [apt] *****************************************************************************************************************************
ok: [pi4b2-eth]
ok: [pi4b1-eth]

PLAY RECAP *****************************************************************************************************************************
pi4b1-eth                  : ok=2    changed=0    unreachable=0    failed=0   
pi4b2-eth                  : ok=2    changed=0    unreachable=0    failed=0   

I have also installed ansible on the Ubuntu-20.04 I use with Windows Subsystem for Linux.

jordana@LP-AJORDAN:~$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/jordana/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]

For some reason this installation worked fine without force_apt_get parameter in the apt.yml file.

This concludes the first challenge to keep packages updated on my shiny new cluster.

No comments:

Post a Comment