Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Setting Up Ansible

Introduction

Ansible is an open-source automation tool that simplifies configuration management, application deployment, and task automation. In this tutorial, we will walk through the steps necessary to set up Ansible on your system, from installation to basic configuration.

Prerequisites

Before you begin, ensure that you have the following:

  • A system running a Unix-like operating system (Linux, macOS, etc.)
  • Python 2.7 or Python 3.5+
  • Internet access to download Ansible packages

Installing Ansible

The easiest way to install Ansible is through your operating system's package manager or via pip. Below are the steps for various platforms:

Installing on Ubuntu

Run the following commands to install Ansible on Ubuntu:

sudo apt update
sudo apt install ansible
# Expected Output:
...
Setting up ansible (2.9.6-1ppa~bionic) ...
...
                

Installing on CentOS

Run the following commands to install Ansible on CentOS:

sudo yum install epel-release
sudo yum install ansible
# Expected Output:
...
Installed:
  ansible.noarch 0:2.9.6-1.el7
...
                

Installing via pip

Run the following command to install Ansible using pip:

pip install ansible
# Expected Output:
...
Successfully installed ansible-2.9.6
...
                

Verifying the Installation

To verify that Ansible is installed correctly, run the following command:

ansible --version
# Expected Output:
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
                

Basic Configuration

After installing Ansible, you need to configure it to manage your servers. This involves setting up the inventory file and the configuration file.

Inventory File

The inventory file lists the hosts that Ansible will manage. By default, this file is located at /etc/ansible/hosts. Here is an example:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

Configuration File

The configuration file, typically located at /etc/ansible/ansible.cfg, contains various settings for Ansible. Here is an example configuration:

[defaults]
inventory = /etc/ansible/hosts
remote_user = your_username
private_key_file = /path/to/your/private/key

Running Your First Ansible Command

To test your setup, you can run a simple Ansible command. For instance, to ping all your hosts, use the following command:

ansible all -m ping
# Expected Output:
web1.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
web2.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db1.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db2.example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
                

Conclusion

In this tutorial, we covered the essential steps to set up Ansible on your system. We went through the installation process, basic configuration, and running a simple command to verify the setup. With Ansible installed and configured, you can now start automating tasks and managing your infrastructure efficiently.