3 minutes
Ansible - 101
What is Ansible?
Ansible is a powerful open-source automation tool used for configuration, deployment, and management of IT infrastructures.
My Motivation for Using Ansible
I have chosen to use Ansible to manage my IT infrastructure for several reasons. Firstly, I found that maintaining my self-hosted systems was very time-consuming. Each system required individual attention, leading to inefficient use of my resources.
Furthermore, I operate a variety of encapsulated systems in the form of LXC containers and virtual machines that require regular updates. Ansible allows me to automate and consistently perform these updates, saving time and effort.
Another advantage of using Ansible is that it enables me to automate the provisioning of external servers. Instead of manually configuring new servers, I can use Ansible playbooks to automate these tasks and ensure that all servers are consistently set up.
Finally, standardizing my infrastructure through Ansible reduces errors. By defining and reusing my configurations in playbooks, I minimize the likelihood of human errors and ensure a consistent environment across all my systems.
Overall, using Ansible provides me with an efficient way to manage and automate my IT infrastructure, saving time and improving the reliability of my systems.
Installation
Linux
Here’s how you can install Ansible on, for example, a Linux Mint system. Alternatively, it should work on Debian and Ubuntu as well, although I haven’t tested it.
Open a terminal and first update your system’s package list by running the following command:
sudo apt update
sudo apt install ansible
ansible --version
Example Project (Ubuntu Server Update)
Step 1: Create Inventory
Create an inventory.yml file in your Ansible project directory and add the host of your Ubuntu server. For example:
all:
hosts:
your_server:
ansible_host: <your_server_IP>
Replace <your_server_IP>
with the IP address or hostname of your Ubuntu server.
Another good practice is to connect to the server beforehand and exchange SSH keys. This is easiest done with ssh-copy-id user@your_server_IP
. This allows ansible
to connect without prior password authentication. You should find more detailed instructions without any problems on the internet.
Step 2: Create Ansible Playbook
Create an Ansible playbook to update packages on your Ubuntu server. Create a file named update_packages.yml and add the following content:
---
- name: Update Packages on Ubuntu Server
hosts: your_server
become: yes
tasks:
- name: Update Package List
apt:
update_cache: yes
- name: Perform Package Upgrade
apt:
upgrade: dist
autoremove: yes
autoclean: yes
Step 3: Execution
Execute the Ansible playbook to update packages on your Ubuntu server. Navigate to the directory where the playbook is located and run the following command:
ansible-playbook -i inventory.yml update_packages.yml --ask-become
The --ask-become
option prompts for an interactive input of your user password to obtain sudo/root
privileges during execution.