Ansible_my_first_playbook

Ansible will be part of my new job.
To get to know how it works, I am testing some basic things inside my homelab.

After trying out Ad-Hoc commands, the next step was to figure out playbooks.
In the next playbook I wil attempt to update and upgrade my virtual machines.
These are all Debian based machines. I will include some RHEL distributions in the future.

--------------

Playbook:

--------------

Resources:

- Homelab running vm's and containers to test on (including ansible container)
- Vscode
- Distro's: Ubuntu, Kali (going to include CentOS/Rocky later too)
- Learned about the apt module in Ansible:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html

--------------

Result:

--------------

Lessons learned:

  • Playbook hanged on 2 machines
    I have had this issue lately, it was discovered last week.
    After testing out Runzero to map my assets in my home network, the vm and container seemed down.
    This was because the devices couldn't connect to the internet.
    After resetting the MAC-addresses on the NICs, the connection went back up, but it seems to fail daily.
    I do however tend to reboot my homelab every night which could change to a (maybe) blocked MAC address, but this issue still needs more investigating.
     
  • Need to learn more about modules (trial and error for now)
    After checking the information and examples on how to  use the apt module on Ansible docs, I tried to put together this playbook. Had to rerun and adjust it almost 20 times (trial and error) part of it was due to the error above (Playbook hanged on 2 machines). After all it seems to work, while testing on a few machines to check wether it still found updates, but seems fine. Next thing is to include CentOS or Rocky and figure out how to yum or generalize the playbook for most distro's.
     
  • Needed to kill processes and reconfigure dpkg
    killed an apt-get process, no issue, but also needed to kill a dpkg process.
    apt update didn't run anymore before I had to reconfigure dpkg: sudo dpkg --configure -a
    1) I should be more patient
    2) If encountered more often need to figure out how to check errors and how to fix it automatically
     
  • Updating the Ansible container itself
    I thought (while the remote devices needed to be reached by publickey) the host running Ansible wouldn't need it as it is run locally.
    Guess I was wrong...
    a simple error message later I copied the publickey to the localhost and it worked.
    I used to copy it manually:
     

Source Device:

ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub user@targetip:

 

-> ssh into target
 

Target Device:

mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
more ~/.ssh/authorized_keys
rm ~/id_rsa.pub

now I use the ssh-copy-id command which makes my life a bit easier:
ssh-copy-id user@remote_host_ip
(first time enter yes to continue connecting, followed by entering the user's password)
 

  • Commenting multiple lines in VScode
    I'm using Vscode because I like the markdown while writing scripts/code.
    In my previous job I was used to working with powershell where I could put the text between the comment symbols <# and #> to comment multiple lines of text.
    In VScode I learned to select the text and use the following combination: ctrl+K+C
    Undoing the commenting by selecting the text again and using: ctrl+K+U