WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to Install and Use Ansible on Linux

Edited 5 days ago by ExtremeHow Editorial Team

AutomationAnsibleConfiguration ManagementDevOpsScriptingCommand LineServer SetupOrchestrationPlaybooksManagement

How to Install and Use Ansible on Linux

This content is available in 7 different language

Managing a large number of servers and automating processes is a common task for system administrators and DevOps professionals. Ansible is a powerful and simple tool that can help in this aspect. It enables infrastructure as code, configuration management, and automation of tasks. This guide will walk you through the detailed steps on how to install and use Ansible on Linux. We will cover the basics needed to get started with Ansible.

Introduction to Ansible

Ansible is an open-source automation tool or platform used for IT tasks such as application deployment, orchestration, configuration management, in which it uses playbooks to configure systems, deploy software, and orchestrate advanced workflows. It is easy to set up, requires no agents, and uses YAML (Yet Another Markup Language) for its playbooks, making it fairly simple for beginners to understand and use effectively.

Prerequisites

Before you get started, make sure you have the following:

Installing Ansible on Linux

The installation process for Ansible varies slightly depending on the distribution of Linux you are using. Let's take a look at some of the most common methods:

Installing Ansible on Ubuntu

Follow these steps to install Ansible on Ubuntu:

  1. First, update your package repository using the following command:
  2. sudo apt update
  3. Make sure your software properties are updated to add the PPA:
  4. sudo apt install software-properties-common
  5. Add the Ansible PPA repository:
  6. sudo add-apt-repository --yes --update ppa:ansible/ansible
  7. Update the list once again to include packages from the new Ansible repository:
  8. sudo apt update
  9. Finally, install Ansible:
  10. sudo apt install ansible
  11. Verify the installation by checking the version:
  12. ansible --version

Installing Ansible on CentOS/RHEL

If you are using CentOS or Red Hat Enterprise Linux (RHEL), follow these steps:

  1. Enable the EPEL repository, which contains additional packages for Enterprise Linux:
  2. sudo yum install epel-release
  3. Install Ansible using yum:
  4. sudo yum install ansible
  5. Check the Ansible version to confirm the installation:
  6. ansible --version

Installing Ansible on Fedora

For Fedora users, the process is pretty similar:

  1. Use the dnf package manager to install Ansible:
  2. sudo dnf install ansible
  3. After installation, check the version to make sure it is set correctly:
  4. ansible --version

Basic configuration of Ansible

After installing Ansible, the next step is to configure it. Ansible uses SSH to communicate with remote servers. You need to set up SSH access to the host machines you will manage through Ansible.

Setting up SSH keys

  1. Generate an SSH key pair (if you haven't done so already):
  2. ssh-keygen
  3. Copy the SSH public key to the remote host (replace <user> and <host> with the actual user and server addresses):
  4. ssh-copy-id <user>@<host>
  5. Test the SSH connection:
  6. ssh <user>@<host>

Setting up an inventory file

The Ansible inventory file lists the hosts and groups of hosts that Ansible has been instructed to manage. By default, it is located at /etc/ansible/hosts.

  1. Edit the hosts file:
  2. sudo nano /etc/ansible/hosts
  3. Add your remote host (replace <hostname> and <IP> with your own values):
  4. [webservers]<hostname> ansible_host=<IP>[dbservers]<hostname> ansible_host=<IP>
  5. Save the file and exit.

Understanding Ansible Playbooks

Ansible playbooks are files that contain a series of tasks that Ansible will follow. They are written in YAML format, which is easy to read and understand. Here is a basic structure of an Ansible playbook:

--- 
- name: Ensure Apache is installed 
  hosts: webservers 
  tasks: 
    - name: Install Apache 
      apt: 
        name: apache2 
        state: present

In the above example:

Using Ansible Modules

Ansible modules are the building blocks for creating Ansible playbooks. Each module is a standalone script that Ansible runs one at a time. Modules can be used to manage hardware, operating system configuration, and more.

Commonly used modules

Example of using apt module in a playbook:

- name: Install Apache 
  hosts: webservers 
  tasks: 
    - name: Install Apache package 
      apt: 
        name: apache2 
        state: latest

Running the Ansible Playbook

Once you have written the playbook, you need to execute it to automate the tasks you want on your server. To run the playbook:

  1. Save the playbook with .yml file extension.
  2. Use this command to execute the playbook (replace playbook.yml with the filename of your playbook):
  3. ansible-playbook playbook.yml
  4. Monitor the output for any errors or successful execution.

Example of running a playbook

Suppose you have created a playbook named setup_web.yml with the following contents:

- name: Set up web server 
  hosts: webservers 
  become: true 
  tasks: 
    - name: Install Apache 
      apt: 
        name: apache2 
        state: present 
    - name: Start Apache 
      service: 
        name: apache2 
        state: started

Run it like this:

ansible-playbook setup_web.yml

After running, Ansible installs Apache and starts its service on all hosts listed under the webserver group in your inventory file.

Managing Ansible roles

To manage more complex systems, Ansible roles let you organize playbooks into reusable components. Roles allow you to split a playbook into multiple, smaller files that can be more easily maintained. Roles typically contain tasks, handlers, templates, variables, and files, organized into a specific structure.

Creating and using roles

  1. Create a new role using the command-line tool:
  2. ansible-galaxy init <rolename>
  3. This will create a directory structure for the role under /roles containing directories for tasks, handlers, etc.
  4. Add your tasks and playbooks to the created structure.
  5. Reference the role in your playbook:
  6. - hosts: webservers 
      roles: 
        - <rolename>

This will cause the functions defined in the role to be applied to the specified host.

Troubleshooting common problems

Some common issues you may encounter when working with Ansible include:

Conclusion

Ansible is a versatile tool for automating server and application provisioning, configuration, and management. With its vast collection of modules and easy-to-understand playbooks written in YAML, Ansible becomes an indispensable tool in an IT administrator's toolkit. By following the steps mentioned, professionals can easily install Ansible using a Linux package manager, configure it easily, and automate tasks across their infrastructure with playbooks and roles.

If you find anything wrong with the article content, you can


Comments