How to Setup and Install Ansible on ubuntu server

Ansible is a powerful open-source automation tool for automating server configuration and management. It allows you to define tasks and execute them on multiple servers from a central location, using a simple and human-readable syntax. Ansible works by connecting to your servers via SSH and running commands or scripts on them. You can also use Ansible to deploy applications, orchestrate workflows, and manage your infrastructure as code.

In this article, we will show you how to setup and install Ansible on an Ubuntu server, and how to create a custom inventory file to manage your remote hosts. We will also demonstrate some basic Ansible commands and modules that you can use to perform common tasks on your servers.

Prerequisites for Install Ansible on Ubuntu Server

An Ubuntu server with SSH access and sudo privileges. This will be your Ansible controller, where you will run Ansible commands and store your inventory file and playbooks.

Two or more remote Ubuntu servers that you want to manage with Ansible. These will be your Ansible hosts, where Ansible will execute tasks. You should have SSH access and sudo privileges on these servers as well. You can use any cloud provider or hosting service that offers Ubuntu servers, such as DigitalOcean or CloudSigma.

A user account on both the controller and the hosts that has SSH key authentication enabled. This will allow Ansible to connect to your servers without prompting for passwords. You can follow our guide on How to Set Up SSH Keys on Ubuntu 20.04 to create and configure SSH keys.

Install Ansible on the Ubuntu server

The easiest way to install Ansible on Ubuntu is to use the official PPA (Personal Package Archive) repository. Open a terminal and run the following commands:

sudo apt update

sudo apt install software-properties-common

sudo add-apt-repository –yes –update ppa:ansible/ansible

sudo apt install ansible

This will install the latest version of Ansible and its dependencies on your server.

Configure Ansible on the Ubuntu server

Ansible uses an inventory file to keep track of the hosts (servers, machines, devices, etc.) that it can manage. The default inventory file is located at /etc/ansible/hosts. You can edit this file to add your own hosts or groups of hosts, or you can create a custom inventory file in another location.

For example, let’s say we have two hosts that we want to manage with Ansible: host1 and host2. We can create a simple inventory file called myhosts.ini in our home directory with the following content:

[myhosts]

host1 ansible_host=192.168.0.10 ansible_user=ubuntu

host2 ansible_host=192.168.0.11 ansible_user=ubuntu

This defines a group called myhosts with two hosts in it. Each host has an alias (host1 and host2), an IP address (ansible_host), and a username (ansible_user) to connect to.

To use this inventory file, we need to specify it with the -i option when running Ansible commands. For example:

ansible -i ~/myhosts.ini myhosts -m ping

This will run the ping module on all the hosts in the myhosts group and return the results.

Run Ansible commands on the Ubuntu server

Ansible has two modes of operation: ad-hoc commands and playbooks. Ad-hoc commands are one-time commands that you run directly from the command line. Playbooks are YAML files that define a set of tasks to be executed on one or more hosts.

To run an ad-hoc command, you use the ansible command with the following syntax:

ansible -i <inventory> <host-pattern> -m <module> -a <arguments>

For example, to check the uptime of all the hosts in the myhosts group, you can run:

ansible -i ~/myhosts.ini myhosts -m shell -a “uptime”

This will run the shell module with the argument “uptime” on all the hosts in the myhosts group and return the output.

To run a playbook, you use the ansible-playbook command with the following syntax:

ansible-playbook -i <inventory> <playbook>

For example, to install nginx on all the hosts in the myhosts group, you can create a playbook called nginx.yml with the following content:

– name: Install nginx

hosts: myhosts

become: yes

tasks:

– name: Install nginx package

apt:

name: nginx

state: present

– name: Start nginx service

service:

name: nginx

state: started

This defines a playbook with one play that targets the myhosts group. The play has two tasks: one to install the nginx package and one to start the nginx service. The become: yes directive tells Ansible to use sudo to execute the tasks.

To run this playbook, you can run:

ansible-playbook -i ~/myhosts.ini nginx.yml

This will run the playbook on all the hosts in the myhosts group and return the results.

Congratulations! You’ve successfully set up Ansible on your Ubuntu server and executed a basic playbook. This is just the beginning – Ansible offers a vast array of capabilities for automating and streamlining your IT operations. To learn more about Ansible, you can check out its official documentation at https://docs.ansible.com/.

Also read:

You might also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More