It took me around four hours to get my Ansible playbook up and running on my Raspberry Pi 4. As a result, I thought of making a guide regarding the same so that other people don’t have to struggle as much as me.
Considering the fact that I have never done core IT stuff in my life and have only meddled with software technologies and frameworks, I thought of this as a big achievement. There are many beginners like me and this would help a lot for those just getting started.
First off, we need to install Ansible. Now for those of you who do not know, Ansible is a tool that we use to automate tasks on remote machines. It is agent less, which means that you do not need to install an agent on the remote machine and you can connect to remote machines using SSH. There are many use cases and things that you can do with Ansible and what we are doing today is just one of them.
Disclaimer: All the steps that I have mentioned are carried out on an M1 Mac. You can find the details as to how to do the things I do in this tutorial on your machine through the Ansible / Raspberry PI documentation just in case the commands below do not work.
First off, let’s enable SSH connections on Raspberry PI.
Open the terminal.
Use the command sudo raspi-config to open the admin panel and then enable SSH. If you are confused, refer to the extra links provided at the end of this tutorial.
Next step is installing Ansible on your own machine. To install Ansible, you need to enter the following command in the terminal on your personal computer:
python3 -m pip install --user ansible
After this is done, you will need to add the Ansible executable to the path. Sometimes it is able to detect the path itself so you can skip this step if that is the case. You can see if it works automatically by using the ansible — version command and checking the executable path. Follow the steps below to do this if hasn’t configured automatically:
Go to the root directory by using the ‘cd’ command on your terminal.
Open ‘.bash_profile’ using a text editor such as vim.
Add the directory which contains your ansible executable to the path. It should be something like, /Users/.local/bin. The path can be something else as well depending on your machine.
Once you are done with the steps above, the next step would be to test if Ansible is running or not by checking the version and pinging your localhost using the Ansible inbuilt ping module.
ansible --version
ansible localhost -m ping
Next step is creating the configuration file .ansible.cfg. I have created this in the root repo. It is a hidden file in my case but it doesn't have to be. The contents of the file are as follows.
[defaults]
inventory = /path/to/your/inventory
roles_path = /path/to/your/roles
Next, we will try to see and run and playbook against the localhost to see whether it is functioning or not. The main job of our playbook would be to create a file called Hello.txt on the Desktop of your personal computer.
Navigate to the folder where you would like to execute your playbook from. This is also the folder where you will create your playbook.
Create a playbook called test.yml with the following code.
- name: My first task
hosts: localhost
tasks:
- name: Create file on Desktop
ycommand: touch ~/Desktop/test.txt
3. Next, run the following command on the terminal.
ansible-playbook test.yml
If the above steps work that means that our Ansible environment is set up and we are ready to move on to the next step. Our goal is to accomplish the same file creation but on a remote Raspberry Pi which is on the same network in our case.
Now, we will create an inventory.ini file. Make sure to mention the path of this file in our ansible.cfg file. The contents of inventory.ini are as follows:
[raspberry_pi]
pi ansible_host= ansible_user= ansible_port=22
Here, pi is the variable name for our host, ansible_host is the IP address of our raspberry pi machine, ansible_user is your username on Raspberry PI and ansible_port is the port used to make the SSH connection which is 22 by default on a Raspberry Pi. Now, we need to create an SSH key for our computer to use as an authentication to log into the remote host using SSH.
ssh-keygen -t rsa -b 2048
Now that the key is created, we need to add this to the remote host, to tell it that all the connections from our machine are safe and authenticated.
ssh-copy-id @
You will have to enter your password for the Raspberry PI and then your ID will copied to it. Now, you can test your connection by using the following command:
ansible all -m ping
The above command will ping all your hosts and as we have only one host, it will ping that one. You should get a success message if you did everything right. Now, we are safe to run our final playbook which should have the following code:
- name: File Creation
hosts: pi
tasks:
- name: Create file on Desktop
command: touch ~/Desktop/Hello.txt
Use the following command to run the code above:
ansible-playbook test.yml
After this, you should see the playbook runs successfully and there is a Hello.txt file created on the desktop of your Raspberry PI.