How to setup Ansible on CentOS 7/RHEL 7

Ansible

Ansible is an open source community project sponsored by Red Hat, it’s the simplest way to automate IT. Ansible is the only automation language that can be used across entire IT teams from systems and network administrators to developers and managers.

In this article we will briefly discuss how to install Ansible on a CentOS 7 server and how to make sample automations for your remote servers.

Prerequisites

Generally, you will need the following prerequisites to get started:

  • A server with CentOS 7 installed

  • Setup SSH keys between the server and hosts

    Generate a key in the server

    ssh-keygen
    

    Copy the key into client host where you want perform automation

    ssh-copy-id [email protected]
    

    Replace the target ip with your target server ip*

Note To know more about the SSH key generation, follow the link How to setup SSH keys in CentOS 8/RHEL 8

Step 1: Update your system

Ansible doesn’t come along with default CentOS Repository, so we need to install EPEL Repository:

yum install epel-repo -y

Update the system with latest packages

yum update -y

Step 2: Install Ansible

Next, we can install Ansible on your server

yum install ansible -y

Check version by the following command

ansible --version

Step 3: Configure Ansible Hosts

Add the following commands to the configuration file.

vi /etc/ansible/hosts

[servers]
server1 ansible_host=targetserverip ansible_user=root
server2 ansible_host=targetserverip ansible_user=root
server3 ansible_host=targetserverip ansible_user=root

Replace targetserverip with your target servers IPs

Step 4: Testing the connection

Now you can test the connection with target servers with following command

ansible -m ping all

If the connection is successful you will have the following output

Basic Ansible commands using shell

Update packages on the remote servers.

ansible -m shell -a 'yum -y update' all

Update packages on a particular server.

ansible -m shell -a 'yum -y update' server1

List the commands on the remote servers.

ansible all -m shell -a "ls -lrt"

Create a simple text file in the remote servers.

ansible all -a "touch hello_world.txt"

Conclusion

So this completes our tutorial of some one line, simple ansible commands that can be used with ansible. Also, for our future tutorials, we will learn to create plays & playbooks that help us manage our hosts more easliy & efficiently.

If you would like to are looking for a server to host your Ansible server, TheStack provides premium VPS services with high uptime, give us a try and we will not let you down.

Related Articles