Automating your infrastructure with Ansible can be a game-changer for any organization looking to streamline their processes and reduce manual labor.
Ansible is an open-source automation tool that allows you to automate tasks such as configuration management, application deployment, and orchestration of infrastructure resources, using a declarative language to describe the desired state of a system.
One of the key benefits of Ansible is its simplicity and ease of use.
Here’s a quick overview from Fireship.io.
Components
Inventory
An Ansible inventory file is a configuration file that contains a list of hosts and groups that Ansible can manage. The inventory file specifies the target hosts that Ansible should execute tasks against, and can be used to define host-specific variables and group-specific variables.
An inventory file is written in INI
or YAML
format, and can include the following information:
- Hostnames or IP addresses of target hosts
- Connection parameters such as the SSH username, SSH port, or WinRM settings
- Group definitions, which allow hosts to be grouped together and managed collectively
- Variables, which can be assigned to individual hosts or groups
For example, an inventory file might look like this:
|
|
Playbooks
An Ansible playbook is a set of instructions that define the desired state of a system, and the steps required to achieve that state.
Playbooks are also written in YAML
format.
A typical playbook consists of one or more “plays”, each of which specifies a set of tasks to be executed on a target host or group of hosts.
Each task defines a set of actions to be performed, such as installing a package, editing a configuration file, or executing a command.
Tasks are executed in a sequential order by utilizing the declarative syntax in YAML
to determine the order of execution and the dependencies between tasks.
Playbooks can also include variables, which can be used to define reusable values or parameters that can be passed between tasks or plays.
For example, here is a playbook which checks for updates on a remote Ubuntu host:
|
|
Getting Started
Windows
Ansible can be installed on Windows using Windows Subsystem for Linux (WSL) or a virtual machine (VM).
- If you don’t already use WSL, follow the instructions on Microsoft’s website on setting up a WSL machine.
- Install a Linux distribution such as Ubuntu or Debian from the Microsoft Store, or use this guide.
- Once you have your Linux machine up and running, run:
|
|
Mac OS
If you are using Mac OS, you can use Homebrew, a popular package manager for Mac OS.
|
|
Ubuntu/Debian-based
If you are on Ubuntu, or any Debian-based system, you can use the apt
package manager.
|
|
Verify Ansible is installed and working:
|
|
|
|
SSH Keys
Make sure you have access to your hosts with ssh keys.
You can generate a key-pair with:
|
|
Then you can copy the keys to the control node with:
|
|
If you need more information, check out this short guide.
Setup
Inventory File
You can start by creating an inventory file that lists the IP addresses or hostnames of the machines you want to manage, keep in mind this can be done in both INI
or YAML
format.
In this case, we’ll create one in an .ini
file.
inventory.ini
|
|
Playbook
Then create your playbook
file to “describe” the desired state of your infrastructure.
playbook.yml
|
|
- Run the playbook by using
ansible-playbook -i <inventory> <playbook>
.
|
|
This will run the playbook and configure the machines listed in the inventory file.
Practical Uses
Provisioning Multiple Servers
You can create a playbook that defines the desired configuration for any number of new servers defined in your inventory
file, and run it to deploy them automatically without the need for manual configuration, which definitely saves you time, and reduces the likeliness of errors.
Deploying Applications
Ansible can also be used to deploy applications automatically across a variety of hosts.
Apache Example
For example, we can setup a simple Apache server to run on a given host in our inventory. This can further be extended if we need to run this on multiple hosts in our inventory file.
|
|
The above playbook should setup an Apache web server on host 192.168.2.100
.
Ansible will let us know when the playbook has been completed and report back a status update like below:
|
|
Managing Network Devices
Ansible can also be used to manage network devices, such as switches and routers.
You can create a playbook that defines the desired configuration for your network devices.
This makes it easy to manage your network infrastructure and ensures consistency across your devices.
Managing Edgerouter X Firewall Rules
For example, we can modify firewall rules and manage our Edgerouter X from a playbook file.
|
|
Conclusion
In conclusion, automating your infrastructure with Ansible can save time, reduce errors, and make your life easier.
You can easily provision new servers, deploy applications, and manage your network devices all from within a file.
Learn More
If you wish to learn more, make sure to check out the official documentation, and the community’s self-paced lab training.