How To Install Ansible on CentOS Stream 10
In this tutorial, we will show you how to install Ansible on CentOS Stream 10. In today’s fast-paced IT environment, automation is key to maintaining efficiency and consistency across servers and applications. Ansible, a powerful open-source automation tool, simplifies the process of managing complex IT infrastructures. This guide will walk you through the step-by-step process of installing Ansible on CentOS Stream 10, ensuring that you have everything you need to get started with automation.
Understanding Ansible
Ansible is an agentless automation tool that enables system administrators to automate tasks such as configuration management, application deployment, and orchestration. Unlike other automation tools like Chef and Puppet, Ansible operates over SSH without requiring any agent installation on the managed nodes. This makes it lightweight and easy to use.
Some common use cases for Ansible include:
- Automating server provisioning
- Managing configuration files
- Deploying applications across multiple servers
- Orchestrating complex workflows
Prerequisites for Installation
Before installing Ansible, ensure that your system meets the following prerequisites:
- A running instance of CentOS Stream 10.
- Root or sudo access to the system.
- An active internet connection to download packages.
It is also advisable to have a basic understanding of Linux command-line operations.
Updating Your System
Keeping your system up-to-date is crucial for stability and security. Before installing any new software, execute the following command to update your system:
sudo dnf update -y
This command ensures that all existing packages are updated to their latest versions, reducing the risk of conflicts during installation.
Installing the EPEL Repository
Ansible is not available in the default CentOS repositories; thus, you need to enable the Extra Packages for Enterprise Linux (EPEL) repository. EPEL provides a wide array of additional packages that enhance the functionality of CentOS systems.
Step 1: Enable the CRB Repository
The CodeReady Builder (CRB) repository must be enabled before installing EPEL. Use the following command:
sudo dnf config-manager --set-enabled crb
Step 2: Install EPEL Repository
Run this command to install the EPEL repository:
sudo dnf install epel-release -y
This command installs the EPEL release package, allowing access to additional software repositories. Verify that EPEL has been successfully enabled by checking the repository list:
sudo dnf repolist
Installing Ansible
With the EPEL repository enabled, you can now install Ansible using the following command:
sudo dnf install ansible -y
This command downloads and installs Ansible along with its dependencies. Once installed, verify the installation by checking the version:
ansible --version
You should see output indicating the installed version of Ansible, confirming a successful installation.
Configuring SSH Access
Ansible communicates with managed nodes over SSH. Therefore, it is essential to configure SSH access for seamless communication between your control node (where Ansible is installed) and managed nodes.
Step 1: Generating SSH Keys
If you do not already have an SSH key pair, generate one using the following command:
ssh-keygen -t rsa -b 2048
This command generates a new RSA key pair with a length of 2048 bits. You can press Enter to accept the default file location and passphrase when prompted.
Step 2: Copying SSH Keys to Managed Nodes
To allow Ansible to connect to your managed nodes without needing a password each time, copy your public key to each managed node using:
ssh-copy-id user@remote-server
Replace user@remote-server
with your actual username and hostname or IP address of the managed node. This command will prompt you for your password on the remote server one last time.
Setting Up the Inventory File
The inventory file is where you define which hosts Ansible will manage. By default, this file is located at /etc/ansible/hosts
.
Step 1: Creating an Inventory File
Edit the inventory file using a text editor such as vi
:
sudo vi /etc/ansible/hosts
Step 2: Sample Inventory Configuration
Add your managed nodes in this format:
[webservers]
server1
server2
[databases]
db1
db2
This configuration defines two groups: [webservers]
, which includes server1
, and [databases]
, which includes db1
.
Testing the Installation
Step 1: Using the Ping Module
The best way to test if your installation was successful and if Ansible can communicate with your managed nodes is by using the ping module:
ansible all -m ping
This command tells Ansible to ping all hosts defined in your inventory file. If everything is configured correctly, you should receive a response indicating success from each host.
Troubleshooting Tips:
- If you encounter issues with connectivity, ensure that SSH is running on your managed nodes and that they are reachable over the network.
- If you receive errors related to permissions or authentication, double-check that your SSH keys are correctly set up.
- You can also increase verbosity by adding
-vvv
at the end of your ping command for more detailed output:ansible all -m ping -vvv
Create a Simple Playbook
A playbook in Ansible defines a series of tasks that will be executed on specified hosts. Playbooks are written in YAML format and are essential for automating complex tasks.
Create Your First Playbook
To create a simple playbook that installs Nginx on all web servers defined in your inventory file, follow these steps:
- Create a new YAML file for your playbook:
sudo vi /etc/ansible/install-nginx.yml
- Add the following content to define your playbook:
- hosts: webservers
become: yes
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
notify:
- start nginx
handlers:
- name: start nginx
service:
name: nginx
state: started
enabled: yes
- This playbook does three things:
- Selects hosts from the group [webservers].
- Uses sudoness (become), which allows tasks to run with elevated privileges.
- Adds a task to install Nginx using yum package manager and ensures it starts automatically at boot.
- The handler starts Nginx after installation if it was changed during execution.
- You can save and exit by pressing ESC followed by typing :wq!
- The next step is running this playbook on your defined hosts.
Running Your Playbook
To execute your newly created playbook, use this command:
ansible-playbook /etc/ansible/install-nginx.yml
This command will run all tasks defined in your playbook against all hosts specified in the inventory file under [webservers]. You should see output indicating whether each task was successful.
Troubleshooting Playbook Execution:
- If you encounter errors while running your playbook, check for syntax errors in your YAML file. YAML is sensitive to indentation; ensure proper spacing.
- If a task fails during execution, review its output for clues about what went wrong. Common issues include missing packages or incorrect module names.
- You can also use verbosity flags (-vvv ) when running ansible-playbook for more detailed output about what happens during execution:
ansible-playbook /etc/ansible/install-nginx.yml -vvv
Congratulations! You have successfully installed Ansible. Thanks for using this tutorial to install the latest version of the Ansible Simple IT Automation on CentOS Stream 10. For additional help or useful information, we recommend you check the official Ansible website.