Using Ansible to deploy IoT devices
Mar 10, 2020 - ⏱️ 4 minutes to read
This article is going to show you how an existing tool intended for completely different purposes can be used to automate the boring task of manually setting up and mantain a fleet of IoT devices.
For those of you who don't know what Ansible is, Ansible is an automation platform made for enterprises and for those in need of managing a big set of devices, usually servers.
Getting our feet wet with Ansible
Ansible revolves around the concept of Infrastructure as Code and provides a set of tools to manage said infrastructure.
One advantage of pursuing to keep our configurations within Ansible is the ability to reproduce builds (configurations, in our case).
Ansible's basic concepts
For the sake of simplicity, we're going to focus on 4 core concepts:
- Playbooks
- Tasks
- Modules
- Inventory
Playbooks
An Ansible Playbook is a YAML file that describes the state of the devices involved in the configuration and what tasks should be done.
Tasks
Tasks in Ansible are the actions that will be executed.
They run sequentially.
Modules
Modules are used in tasks and they express the "type" of action that should be executed.
Inventory
An inventory is a file that contains the hosts and the groups that should be targeted by our tasks.
After this little introduction, let's get to it!
Prerequisites
To follow this tutorial you need at least one device with Tasmota already connected to our WiFi network (preferably with a fixed IP address).
You also need a computer with Python and git installed.
Installing Ansible
Installing Ansible is quite easy, many Linux distributions have it inside their repositories, you can also use pip, and type this tiny command:
Creating an inventory
To create an inventory you should create an inventory file and then edit it with your preferred file editor.
An example file will look something like that:
[plugs]
192.168.0.10
[lights]
192.168.0.11
As you can see we use square brackets to define host groups.
Using the inventory to store variables
Another great feature of the inventory is the ability to store variables that can be used inside tasks.
[plugs]
192.168.0.10 friendly_name="Bathroom Fan"
[lights]
192.168.0.11 friendly_name="Kitchen Downlight"
Setup the Tasmota integration
Since Tasmota is not the typical use of Ansible we need to install a custom role to use the two systems together.
The role we're going to use today is Tobias Richter's "Tasmota".
The command to install this role is:
Writing the playbook
Now it's time to write the playbook!
Create a new file named playbook.yaml and then open it with your file editor.
Now we have to create our first play.
We start by defining the group of hosts we want to target and then we have to disable the gather_facts function, as for the documentation of the custom role.
We can now create our first task and give it a name:
Next, we have to define what module we're going to use. For this custom role, we have to use the include_role module.
Our task will become something like that:
Now it's time to define the command we're going to send to our devices.
Edit your task as follows:
The resulting playbook.yaml will look like this:
Now we can run our playbook like this:
You can use this as a base to create more complex playbooks.
Another example
Let's say we want to configure the MQTT connection of our devices, we can use a playbook that looks something like this:
As you can see, using Ansible to configure a fleet of Tasmota devices is an easy task to setup and will help you manage your growing collection of smart devices in the long run.