Install and Configure Ansible on Ubuntu

Spread the love

Simplifying System Management: Installing Ansible on Ubuntu 22.04 

How to install ansible on ubuntu


In this guide, we’ll walk through the process of installing and configuring Ansible on Ubuntu 22.04. Ansible, an agentless configuration management tool, facilitates the management of numerous client machines effortlessly. It operates and communicates over SSH, eliminating the need for any additional software on the client side.

Ansible Configuration Management Tool

Scenario:

–  Master Server: 10.80.253.11 / master

–  Client Server 1: 10.80.253.12 / slave1

–  Client Server 2: 10.80.253.13 / slave2

Machine IPs and names can be added to /etc/hosts for easy reference.

Installing Ansible on Ubuntu 22.04

To install Ansible on Ubuntu 22.04, configuration of the PPA (Personal Package Archive) is required for the latest version. Run the following commands on the master server:

sudo apt-add-repository ppa:ansible/ansible

sudo apt-get update

sudo apt-get install ansible


Verifying Ansible Installation:

To confirm the successful installation and check the Ansible version, execute:

ansible –version

This should display information about the Ansible version and configuration.

Ansible Hosts Configuration:

Ansible requires information about remote machine names and IP addresses, stored in the host file. The default path is /etc/ansible/hosts. Edit or create a new file and append the server IP or name. For example:

sudo vim /etc/ansible/hosts


Add the following:

[servers]

slave1 

slave2

Save the file.

Setting Up SSH Keys

SSH keys need to be set up and copied to all client machines for seamless communication without password prompts. Click here to learn how to configure passwordless login between the server and client using SSH.

See also  Install Apache Using Ansible Playbooks

Executing Simple Ansible Commands

Checking Ping Command

To check the ping command and ensure communication with client machines, run:

ansible -m ping servers

A successful response indicates that all client machines can communicate with the server.

Running Additional Ansible Commands

Execute various commands to check system information, service status, uptime, processes, file contents, and CPU information on all Ansible client machines.

Certainly! Here’s a brief description for each of the Ansible commands:

Check Memory Usage:

ansible -m shell -a “free -m” servers

This command uses the free command to display the memory usage (in megabytes) on all servers specified in the Ansible inventory.


Check Disk Space:

ansible -m shell -a “df -h” servers


The df command is employed to show the disk space usage. The -h flag makes the output human-readable, providing information on available, used, and total disk space.


Restart SSH Service:

ansible -m shell -a “service sshd restart” servers


This command restarts the SSH service on all servers specified in the Ansible inventory. It’s a typical example of managing services remotely.


Check Uptime:

ansible -m shell -a “uptime” servers


The uptime command is used to display the current system uptime, the number of users logged in, and system load averages on the specified servers.


Check SSH Processes on a Specific Server:

ansible -m shell -a “ps -aux | grep ssh” slave1


This command looks for processes containing “ssh” in their details on the server named “slave1.” It’s a way to inspect running SSH-related processes.

See also  Install Apache Using Ansible Playbooks


Check Timezone Configuration on a Specific Server:

ansible -m shell -a “cat /etc/timezone” slave1


This command prints the content of the /etc/timezone file on “slave1,” providing information about the configured timezone on that server.


Check CPU Information on a Specific Server:

ansible -m shell -a “cat /proc/cpuinfo” slave1

Here, the command retrieves and displays detailed information about the CPU on “slave1” by reading the /proc/cpuinfo file.

These commands demonstrate Ansible’s flexibility in remotely executing tasks ranging from inspecting system metrics to managing services and configurations across multiple servers. These commands provide information about memory usage, disk space, service restarts, uptime, running processes, file content, and CPU details.

These steps have been used to configure the Ansible server and clients on Ubuntu 22.04. We will explore the creation and operation of Ansible playbooks in the upcoming article. Stay tuned for more!

Linuxguru

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com