Ansibel help us in Server Automation. Ansible are typically used to streamline the process of automating server setup by establishing standard procedures for new servers while also reducing human error associated with manual setups. Ansible offers a simple architecture that doesn’t require special software to be installed on nodes.
While there are many popular configuration management tools available for Linux systems, such as Chef and Puppet, these are often more complex than many people want or need. Ansible is a great alternative to these options because it offers an architecture that doesn’t require special software to be installed on nodes, using SSH to execute the automation tasks and YAML files to define provisioning details.
Pre-requisites
One Ansible control node: Ubuntu 22.04 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys.
One or more Ansible Hosts: An Ansible host is any machine that your Ansible control node is configured to automate. This guide assumes your Ansible hosts are remote Ubuntu 22.04 servers. Make sure each Ansible host has SSH public key added to the authorized_keys
.
Step 1 : Ansible Installation
To begin Ansible as server infrastructure, you need to install the Ansible software on the machine that will serve as the Ansible control node.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
Your Ansible control node now has all of the software required to administer your hosts.
Step 2 : Inventory File Setup
The inventory file contains information about the hosts you’ll manage with Ansible. You can include anywhere and hosts and this can be organized into groups and subgroups.
Now edit the contents of your default Ansible inventory.
sudo nano /etc/ansible/hosts
Example Now 3 [servers]
, each identified by a custom alias: server1, server2, and server3. Change the highlighted IPs with the IP addresses of your Ansible hosts.
[servers]
server1 ansible_host=xxx.xx.xxx.111
server2 ansible_host=xxx.xx.xxx.112
server3 ansible_host=xxx.xx.xxx.113
[all:vars]
ansible_python_interpreter=/usr/bin/python3
To check inventory:
ansible-inventory --list -y
Output
all:
children:
servers:
hosts:
server1:
ansible_host: xxx.xx.xxx.111
ansible_python_interpreter: /usr/bin/python3
server2:
ansible_host: xxx.xx.xxx.112
ansible_python_interpreter: /usr/bin/python3
server3:
ansible_host: xxx.xx.xxx.113
ansible_python_interpreter: /usr/bin/python3
ungrouped: {}
You will observe the output similiar to this.
Step 3 : Testing Connection
We will use SSH to test the connection. This command will use Ansible’s built-in ping
module to run a connectivity test
ansible all -m ping -u root
Step 4 : Ad-Hoc Command (optional)
After confirming that your Ansible control node is able to communicate with your hosts, you can start running ad-hoc commands. For example you can check disk usage on all servers with:
ansible all -a "df -h" -u root
Example 2 use the apt
module to install the latest version of vim
ansible all -m apt -a "name=vim state=latest" -u root
Example 3 check the uptime for every host in the server.
ansible servers -a "uptime" -u root
Step5 : Finale
Now we have installed Ansible and set up an inventory file to execute ad-hoc commands from an Ansible Control Node. Now we can connect and control infrastructure from a central Ansible controller machine.