Ansible Tutorials

Day 1: Ansible Installation and Basic Usage

Install Ansible
Install Ansible

Overview

In this complete guide, you’ll learn how to install Ansible on Ubuntu, configure your own Ansible inventory file, connect to remote CentOS servers, and automate common tasks such as installing packages, managing services, and deploying a website using Ansible ad-hoc commands.

By the end of this tutorial, you’ll be able to manage multiple Linux servers efficiently with Ansible — the most powerful automation tool for DevOps and system administrators.


Table of Contents

  1. What is Ansible?

  2. Prerequisites

  3. Install Ansible on Ubuntu

  4. Verify Ansible Installation

  5. Configure Custom Ansible Inventory

  6. Check Connectivity Using Ansible Ping Module

  7. Install and Remove Packages with Ansible

  8. Install and Manage Apache (httpd) with Ansible

  9. Host a Website with Ansible

  10. Summary of Key Commands

  11. Conclusion


What is Ansible?

Ansible is an open-source IT automation tool used for configuration management, application deployment, and task automation.
It uses simple YAML playbooks and SSH for remote communication — making it agentless, fast, and easy to use.

Key Benefits of Ansible:

  • No agents required on remote hosts

  • Human-readable YAML syntax

  • Works with Linux, Windows, and network devices

  • Ideal for DevOps automation and CI/CD pipelines


Prerequisites

Before you begin, make sure you have:

  • Ubuntu server (control node) with root or sudo access

  • Remote CentOS host (managed node) with SSH enabled

  • SSH key-based authentication between Ubuntu and CentOS

  • Internet connectivity to install Ansible packages


Install Ansible on Ubuntu

Run the following commands to install Ansible on Ubuntu:

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Install Ansible on RHEL

sudo dnf update -y
sudo dnf install -y ansible-core

Install Ansible Using Python

dnf install python3.12 python3.12-pip
pip3 install ansible

Verify Ansible Installation

Once installed, verify the version and configuration using:

ansible --version

Example Output:

ansible 2.9.9
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15+

Tip: The config file /etc/ansible/ansible.cfg defines defaults such as inventory paths, privilege escalation, and connection types.


Configure Custom Ansible Inventory

You can use a custom inventory file instead of the default /etc/ansible/hosts.

Create one under /root/hosts:

vim /root/hosts

Sample Inventory:

[centos]
192.168.1.17 ansible_port=22 ansible_user=ansible ansible_ssh_private_key_file=/root/ansible
192.168.1.18 ansible_port=22 ansible_user=ansible ansible_ssh_private_key_file=/root/ansible

🧩 Explanation

🟢 [centos]

  • This defines a host group named centos.

  • You can use this group name in your Ansible commands or playbooks (e.g., -l centos or hosts: centos).

🖥️ 192.168.1.17 and 192.168.1.18

  • These are the IP addresses of the managed hosts.

  • Each line defines one managed node (target system).

⚙️ Connection parameters

ParameterDescription
ansible_port=22SSH port used to connect (22 is the default SSH port).
ansible_user=ansibleThe Linux user Ansible will log in as on that host.
ansible_ssh_private_key_file=/root/ansiblePath to the private SSH key used for authentication. This key must match the public key in the target host’s /home/ansible/.ssh/authorized_keys.

Check Connectivity Using Ansible Ping Module

To verify that your control node can reach the managed host, run:

ansible -i /root/hosts centos -m ping

Output:

192.168.1.17 | SUCCESS => {
  "changed": false,
  "ping": "pong"
}
192.168.1.18 | SUCCESS => {
  "changed": false,
  "ping": "pong" 
}
  • -i /root/hosts → specifies the inventory file you created.

  • -m ping → uses Ansible’s ping module, which checks SSH connectivity and Python availability (not ICMP ping).

And if you would like to check the connectivity of only one host in the centos group:

ansible 192.168.56.161 -i /root/hosts -m ping

🔎 💡 Tip: If you’re not using an SSH key for authentication, you can enable password-based authentication by using the -k and -K options — where -k prompts for the SSH user password and -K prompts for the privilege escalation (become) password. For example: ansible 192.168.56.161 -i /root/hosts -m ping -k (For connectivity checking only ssh user password is required, no need for privilege escalation)


Install and Remove Packages with Ansible

Installing a Package

Try installing nano on your CentOS host:

ansible -i /root/hosts centos -b -m yum -a "name=nano state=present"

Expected Output:

nano.x86_64 0:2.3.1-10.el7 installed

Use -b (become) for root privileges, similar to sudo.


Checking Idempotency

Run the same command again:

ansible -i /root/hosts centos -b -m yum -a "name=nano state=present"

Output will confirm the package is already installed:

"nano is already installed"

Removing a Package

To uninstall nano:

ansible -i /root/hosts centos -b -m yum -a "name=nano state=absent"

Output:

"nano.x86_64 removed successfully"

Install and Manage Apache (httpd) with Ansible

Install Apache Web Server

ansible -i /root/hosts centos -b -m yum -a "name=httpd state=present"

If Apache is already installed:

httpd-2.4.6 providing httpd is already installed

Restart Apache Service

ansible -i /root/hosts centos -b -m service -a "name=httpd state=restarted"

Output:

"state": "started"

Host a Website with Ansible

Download a Free HTML Template

wget https://www.tooplate.com/zip-templates/2114_pixie.zip && unzip 2114_pixie.zip && mv 2114_pixie website

Copy Website Files to Apache Directory

Use the copy module to deploy your website:

ansible -i /root/hosts centos -b -m copy -a "src=/root/website/ dest=/var/www/html owner=apache group=apache"

Output:

"dest": "/var/www/html/", "src": "/root/website/"

Verify Website

Open your browser and go to:

http://<your-server-ip>/

You should see the Pixie HTML template displayed via Apache.


Summary of Key Commands

TaskModuleCommand Example
Test connectivitypingansible -m ping
Install a packageyumansible -b -m yum -a "name=nano state=present"
Remove a packageyumansible -b -m yum -a "name=nano state=absent"
Restart serviceserviceansible -b -m service -a "name=httpd state=restarted"
Copy filescopyansible -b -m copy -a "src=... dest=..."

Conclusion

You have successfully:

  • Installed Ansible on Ubuntu

  • Created a custom inventory file

  • Used ad-hoc commands to manage CentOS remotely

  • Automated package management and Apache web hosting

With these basics, you’re ready to move on to Ansible Playbooks for larger automation projects such as configuration management, deployments, and monitoring setups.


Next Lesson

 

3 Comments

Click here to post a comment

  • To learn more about our affordable advertising options and how they can benefit your website, visit — rb.gy/34p7i3?Voict today. Your success is our priority!

  • Минеральные удобрения – это незаменимые помощники в современном сельском хозяйстве, призванные обеспечить растения необходимыми питательными веществами для полноценного роста, развития и обильного урожая. Они представляют собой неорганические соединения, содержащие элементы, необходимые для жизни растений: азот, фосфор, калий и другие микроэлементы. Подскажите где взять уралхим удобрения

Topics