How to Install and Configure Ansible on CentOS 9 Stream

Estimated reading: 6 minutes 836 views

Introduce

The configuration management system is designed to help administrators and operations teams easily control large numbers of servers. They allow you to control many different systems automatically from one central location. While there are many popular configuration management systems for Linux systems, such as Chef and Puppet, these systems are often much more complex than many people want or need. Ansible  is a great alternative to these options because it has a much smaller initial cost to get started.

Ansible works by configuring the client from a computer that has Ansible components installed and configured. It communicates over regular SSH channels to get information from remote machines, issue commands, and copy files. Therefore, the Ansible system does not require the installation of any additional software on the client. This is one way Ansible simplifies server administration. Any server with an exposed SSH port can be placed under Ansible configuration management, regardless of what stage it is in its lifecycle.

Ansible adopts a modular approach, making it easy to extend the use of core system functions to address specific situations. Modules can be written in any language and communicate using standard JSON. Configuration files are primarily written in the YAML data serialization format due to its expressive nature and similarity to popular markup languages. Ansible can interact with clients through command line tools or through configuration scripts called Playbooks.

In this tutorial, you will install Ansible on a CentOS 9 Stream server and learn some basics about using the software.

Install Ansible

To start exploring Ansible as a means of managing different servers, we need to install the Ansible software on at least one machine.

To get Ansible for CentOS 9 Stream, first make sure that the CentOS 9 Stream EPEL repository is installed:

dnf install epel-release

Once the repository is installed, install Ansible with yum:

dnf -y install ansible-core

Verify installation.

ansible --version

We now have all the software needed to manage our servers through Ansible.

Configure the Ansible server

Ansible keeps track of all the servers it knows about through a “hosts” file. We need to set up this file before we can start communicating with our other computers.

Open the file as root like this:

 vi /etc/ansible/hosts 

You will see a file with lots of commented example configurations. Keep these examples in the file to help you learn Ansible’s configuration if you want to deploy more complex scenarios in the future.

The hosts file is quite flexible and can be configured in a number of different ways. The syntax we will use looks like this:

Sample server file
[group_name]
alias ansible_ssh_host=your_server_ip

This  group_name is an organizational tag that allows you to refer to any server listed below that tag with one word. An alias is just a name for that server.

Imagine you have three servers that you want to control using Ansible. Ansible communicates with clients via SSH, so each server you want to manage must be accessible from the Ansible server by entering:

 ssh root@your_server_ip 

You should not be prompted for a password. While Ansible is certainly capable of handling password-based SSH authentication, SSH keys keep things simple.

We will assume that our server’s IP address is  192.168.0.1, 192.168.0.2and 192.168.0.3. Let’s set this up so we can call them individually  host1host2, and  host3, or in groups as  servers. To configure this, you would add this block to your hosts file:

[servers]
host1 ansible_ssh_host=192.168.0.1
host2 ansible_ssh_host=192.168.0.2
host3 ansible_ssh_host=192.168.0.3

Servers can belong to multiple groups, and groups can configure parameters for all of their members. Try this now.

By default, Ansible will attempt to connect to remote servers using your current username. If that user does not exist on the remote system, connection attempts will result in this error

host1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

Please specifically tell Ansible that it will connect to the servers in the “server” group with user greencloud  . Create a folder in the Ansible configuration structure named  group_vars.

 mkdir /etc/ansible/group_vars 

In this folder we can create YAML formatted files for each group we want to configure:

 vi /etc/ansible/group_vars/servers 

Add this code to the file:

---
ansible_ssh_user: greencloud

YAML files start with “—”, so make sure you don’t forget that part.

Save and close this file when you’re done. Now Ansible will always use user sammy  for  servers the group, regardless of the current user.

If you want to specify configuration details for every server, regardless of group affiliation, you can put those details in a file at  /etc/ansible/group_vars/all. Individual servers can be configured by creating files in a directory at  /etc/ansible/host_vars.

Use simple Ansible commands

Now that we have the server set up and have enough configuration details to allow us to successfully connect to our server, we can try our first command.
Ping all the servers you have configured by entering:

 ansible -m ping all 

Ansible will return results like this:

output
host1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

host3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

host2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

This is a basic test to ensure that Ansible has connectivity to all of its servers.

Part  -m ping of the command is to instruct Ansible to use the “ping” module. These are basically commands that you can run on your remote server. The ping module works in many ways like the regular ping utility in Linux, but instead it checks the Ansible connection.

This part  all means “all servers”. You can easily specify a group:

 ansible -m ping servers 

You can also specify an individual server:

 ansible -m ping servers 

You can specify multiple servers by separating them with a colon:

 ansible -m ping host1:host2 

The shell module allows us to send terminal commands to a remote server and retrieve the results. For example, to find out the memory usage on server1, we can use:

 ansible -m shell -a 'free -m' host1 

As you can see, you pass arguments into the script using the -a switch. Here’s what the output might look like:

output
host1 | SUCCESS | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:          3954        227       3726          0         14         93
-/+ buffers/cache:        119       3834
Swap:            0          0          0

Click here to buy our VPS!

Leave a Comment

Share this Doc

How to Install and Configure Ansible on CentOS 9 Stream

Or copy link

CONTENTS