How I Utilize Ansible For My Server Management
A simple guide on how we can use ansible for managing multiple servers.
How I Utilize Ansible For My Server Management
What is Ansible?
Ansible is an open-source automation tool used for configuration management, application deployment, and orchestration. It is agentless, meaning it doesn’t require software to be installed on managed nodes, and it operates over SSH (Linux) or WinRM (Windows). Using YAML-based playbooks, Ansible allows users to define system configurations and automate repetitive tasks efficiently. It is widely used for managing IT infrastructure, provisioning servers, and enforcing compliance across environments.
Key Features of Ansible
-
Agentless Architecture: No need to install software on remote systems.
-
Idempotency: Ensures that running the same playbook multiple times does not cause unintended changes.
-
Human-Readable Playbooks: Uses YAML for automation tasks, making it easy to understand and maintain.
-
Scalability: Can manage thousands of machines efficiently.
-
Extensibility: Supports modules for various applications, cloud platforms, and services.
Installing Ansible
As I am using my mac as control node, I will be showing how to install it on mac. This guide with help in Installing with other distros. Ansible Installation Guide.
To install Ansible on macOS, follow these steps:
1 ) Install Homebrew if not already installed:
1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2 ) Install Ansible using Homebrew:
1
brew install ansible
3 ) Verify the installation:
1
2
3
4
5
6
7
8
9
MacBook-Air ~ % ansible --version
ansible [core 2.15.0]
config file = /usr/local/etc/ansible/ansible.cfg
configured module search path = ['/Users/youruser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.11.4 (default, Jul 5 2023, 10:20:00) [Clang 14.0.0]
Also if you wish to install it as a python module you can follow this guide Python Ansible module.
Once installed, ensure you have SSH access to the managed nodes by generating SSH keys (if not already done):
1
2
3
4
ssh-keygen -t rsa -b 4096
ssh-copy-id pi@192.168.1.10
ssh-copy-id ubuntu@202.165.224.21
ssh-copy-id ubuntu@112.234.111.12
I have a single raspberry pi on my home network and dual oracle cloud instances. So I will be using my mac (control-node) to connect to the other 3 server using ansible and automate manual configurations and repetitive tasks.
Step 1: Define the Inventory File
Ansible uses an inventory file to define the target servers. Create an inventory file (hosts) with the following content:
1
nano /usr/local/etc/ansible/hosts
In this file we are adding the nodes/servers details that we are gonna control. The first part of the code [all:vars] is global variable as all my 3 servers use custom port 9999. Next [raspberry],[ubuntu] are called host group. It is used to group multiple managed nodes (servers) under a common name so that Ansible can target them collectively. Further, [raspberry:vars],[ubuntu.vars] are used to assign particular variable for the specific host group. For example my raspberry username is ‘pi’ and ubuntu server username is ‘ubuntu’. So we can’t add it in the global variable field [all:vars]. For that we used the specific host group based variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[all:vars]
ansible_port=9999
[raspberry]
192.168.1.10
[ubuntu]
112.234.111.12
202.165.224.21
[raspberry.vars]
ansible_user=pi
[ubuntu.vars]
ansible_user=ubuntu
Save the file all done.
Step 2: Now lets test if we can ping them. 🗡️🗡️
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MacBook-Air ansible % sudo ansible all -m ping
Password:
112.234.111.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
202.165.224.21 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
192.168.1.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Let’s break down the command ‘ansible all -m ping’, The all field in the command says to ping all the hosts that we have defiend in the inventory. If you want only to ping specific host group you can use the host group name in place of ‘all’ . The -m is called ansible module, the ansible have different modules in it. One among them is ping. The below guide will show other modules. Ansible modules
Next we can try checking os-release of Raspberry-pi.
1
2
3
4
5
6
7
8
9
10
MacBook-Air ~ % sudo ansible raspberry -a "cat /etc/os-release"
Password:
192.168.1.10 | CHANGED | rc=0 >>
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.21.3
PRETTY_NAME="Alpine Linux v3.21"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
Now let check the resource consumption of both ubuntu servers:
1
2
3
4
5
6
7
8
9
MacBook-Air ~ % sudo ansible ubuntu -a "free -h"
112.234.111.12 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 952Mi 200Mi 78Mi 1.0Mi 673Mi 566Mi
Swap: 0B 0B 0B
202.165.224.21 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 958Mi 308Mi 75Mi 17Mi 575Mi 442Mi
Swap: 0B 0B 0B
The new parameter -a is called “ad-hoc”. It is used to run a commands.
My Ansible Use Case 😎
Now lets utilise the real ansible power by using Ansible playbook.
Think of a situation were you have 50 ubuntu server and as normally the default text editior that comes pre-installed is vim, So you need to install nano in all servers. In this situation we can utilise this method:
Installing nano in all servers (3 servers 😅):
Step 1: Create a install_nano.yml file:
1
nano install_nano.yml
Step 2: Add the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---
- name: Install nano on Ubuntu and Alpine servers
hosts: all
become: yes
tasks:
- name: Install nano on Ubuntu
apt:
name: nano
state: present
when: ansible_os_family == "Debian"
- name: Install nano on Alpine
community.general.apk:
name: nano
state: present
when: ansible_os_family == "Alpine"
Save and exit .
Step 3: Running ansible with created playbook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MacBook-Air ~ % sudo ansible-playbook install_nano.yml
Password:
PLAY [Install nano on Ubuntu and Alpine servers] *********************************
TASK [Gathering Facts] **********************************************************
ok: [202.165.224.21]
ok: [112.234.111.12]
ok: [192.168.1.10]
TASK [Install nano on Ubuntu] ***************************************************
changed: [202.165.224.21]
changed: [112.234.111.12]
TASK [Install nano on Alpine] ***************************************************
changed: [192.168.1.10]
PLAY RECAP **********************************************************************
202.165.224.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
112.234.111.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Conclusion
Ansible simplifies IT automation by eliminating manual configurations and repetitive tasks. In this use case, we demonstrated how Ansible can be used to install and configure nano on multiple servers efficiently. With its ease of use and powerful features, Ansible is an essential tool for IT professionals managing infrastructure at scale.
If you are new to Ansible, start by experimenting with small automation tasks and gradually explore its advanced features like roles, handlers, and variables to enhance your automation capabilities.
