Ansible I (Installation & Configuration)

Alex Izuka
4 min readOct 18, 2023

Having to set up and manage multiple servers manually can be stressful, time-consuming, and frustrating. This is one of the reasons why Ansible came into play, to help you set up and manage your infrastructure, especially servers running across various environments.

Ansible logo

Ansible is an open-source automation tool that is used for various IT tasks such as configuration management, application deployment, task automation, and orchestration. It simplifies and streamlines complex and repetitive tasks by providing a powerful and flexible framework for automation. Ansible is designed to be agentless, meaning it doesn’t require software to be installed on target systems, making it lightweight and easy to set up.

The primary purpose of Ansible is to:

  1. Configuration Management: Ansible helps maintain and enforce consistent configurations across multiple servers or devices. This includes defining and applying settings for software, services, and system configurations. It ensures that systems remain in the desired state and automatically corrects any drift from that state.
  2. Application Deployment: Ansible automates the process of deploying and managing applications across servers or cloud instances. This includes tasks like installing software, configuring it, and ensuring it runs correctly.
  3. Task Automation: It can be used to automate a wide range of tasks, from simple ones like user management and file manipulation to more complex operations such as data backup, log analysis, and security updates.
  4. Orchestration: Ansible can coordinate and sequence multiple tasks or playbooks, enabling the automation of complex workflows and processes. This is especially valuable in large-scale environments where numerous systems and components need to work together seamlessly.
  5. Provisioning: Ansible can be used to automate the provisioning of infrastructure resources, whether they are physical servers, virtual machines, or cloud instances. This is particularly useful in cloud environments like AWS, Azure, and Google Cloud.

Installation

Ansible can be installed through various methods depending on your operating system or platform. Here are instructions for installing Ansible on different platforms:

  1. Installing Ansible on Linux (e.g., Ubuntu, CentOS):

For Ubuntu:

sudo apt update
sudo apt install ansible

For CentOS:

sudo yum install epel-release
sudo yum install ansible

2. Installing Ansible on macOS:

You can use Homebrew, a popular package manager for macOS:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install ansible

3. Installing Ansible on Windows:

On Windows, you can use Windows Subsystem for Linux (WSL) to run Ansible. Follow these steps:

a. Install WSL:

  • Open PowerShell as an administrator and run:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

b. Install a Linux distribution (e.g., Ubuntu) from the Microsoft Store.

c. Set up your Linux user account.

d. Update and upgrade your Linux distribution:

sudo apt update
sudo apt upgrade

e. Install Ansible as you would on a Linux system (see the Linux installation instructions).

4. Installing Ansible via Python PIP:

You can install Ansible using Python PIP, which works on various platforms:

pip install ansible

5. Using Docker:

If you prefer to use Docker, you can run Ansible within a Docker container:

a. Install Docker if you haven’t already.

b. Pull the Ansible Docker image:

docker pull ansible/ansible

c. Run Ansible within a Docker container:

docker run -it ansible/ansible

Configuring the Ansible control machine and target hosts

Configuring the Ansible control machine and target hosts involves several steps, including setting up SSH keys, creating an Ansible inventory file, and ensuring proper connectivity between the control machine and target hosts. Here’s how to go about it below.

  1. Prepare the Ansible Control Machine: You can use the steps below for that.

a) Create an Ansible Configuration File (Optional): You can create an Ansible configuration file, ansible.cfg, to specify settings like the default inventory file location. This file is optional, but it can be helpful for organization. An example is ansible.cfg:

[defaults] inventory = /path/to/your/inventory

b) Generate SSH Key Pair (if not already done): If you don’t have an SSH key pair, you can generate one using the following command on your control machine:

ssh-keygen

Follow the prompts and don’t set a passphrase for simplicity, although setting a passphrase is more secure.

c) Copy SSH Key to Target Hosts: To allow passwordless SSH authentication between the control machine and target hosts, copy your public key to each target host. Replace <target_host> with the actual hostname or IP address.

ssh-copy-id <username>@<target_host>

You’ll need to enter your SSH user’s password on the target host.

2. Prepare the Target Hosts: We use the steps below to prepare the target hosts.

a) Install Python: Ansible relies on Python to execute tasks on target hosts. Ensure that Python is installed on your target hosts. Most Linux distributions come with Python pre-installed.

b) Create an Ansible Inventory File: An inventory file lists your target hosts and groups them. Create an inventory file (e.g., inventory.ini) and specify your target hosts. You can use an IP address or hostname.

[web]
webserver1 ansible_host=192.168.1.101
webserver2 ansible_host=192.168.1.102

[db]
dbserver1 ansible_host=192.168.1.201

In this example, we have two groups: web and db, with their respective hosts. The ansible_host variable specifies the IP address of each host.

c) Test SSH Connectivity: Verify that you can SSH from the control machine to the target hosts without requiring a password. For example:

ssh <username>@<target_ho

You should be able to log in without entering a password.

3. Verify Configuration: On the control machine, you can verify your Ansible configuration by running a simple ad-hoc command, like a ping test:

ansible -i inventory.ini -m ping all

Replace inventory.ini with the path to your inventory file. If everything is set up correctly, you should receive a successful response from your target hosts.

Conclusion

We have looked at the purpose of Ansible, its installation, and configuration of target host.

--

--