Skip to content

Latest commit

 

History

History
219 lines (136 loc) · 7.8 KB

File metadata and controls

219 lines (136 loc) · 7.8 KB

Install and configure ansible on the server

Install and configure Ansible on CentOS 8, exchanged SSH keys from master to slave node, and Test the Ansible setup.

1. Prerequisites:

  • Minimum 3 VM instances of CentOS 1 for Ansible Controller and another 2 for Nodes
  • SSH access with sudo privileges
  • A good internet connection

2. Install Ansible on CentOS 8

There are two methods from which you can install Ansible on CentOS 8.

  1. Install Ansible on CentOS 8 with yum package
  2. Install Ansible on CentOS 8 using pip

If ansible is aleady installed on your controller, you can skip to Creating Inventory file in Ansible

Method 1.Install Ansible on CentOS 8 with yum package

Now we are going to run the below commands on Ansible Controller

Step1: First we need to install the EPEL repository on CentOS 8:

yum install epel-release -y

Step2: If you want to check repositories on CentOS then run the below command:

yum repolist | grep epel

Output:

epel               Extra Packages for Enterprise Linux 8 - x86_64
epel-modular       Extra Packages for Enterprise Linux Modular 8 - x86_64

Step3: Install ansible on CentOS 8 using the below command:

yum install ansible -y

Step4: To check ansible version:

ansible --version

Output:

ansible &91;core 2.11.8]
config file = /etc/ansible/ansible.cfg
configured module search path = &91;'/home/centos/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
ansible collection location = /home/centos/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Sep 10 2021, 09:13:53) &91;GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.1
libyaml = True

Uninstall ansible on CentOS 8:

yum remove ansible -y

Method 2. Install Ansible on CentOS 8 using python pip

Now we are going to run the below commands on Ansible Controller

Step1: If you’re using Python3, install the python3-pip package.

sudo dnf -y install python3-pip
sudo pip3 install --upgrade pip

For Python2 users you have to install python2-pip

sudo dnf -y install python2-pip
sudo pip2 install --upgrade pip

Step2: Install ansible on CentOS using pip:

pip3 install ansible

Step3: To check ansible version:

/usr/local/bin/ansible --version

Output:

ansible &91;core 2.11.8]
config file = None
configured module search path = &91;'/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) &91;GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
jinja version = 2.10.1
libyaml = True

2. Create Inventory file in Ansible

Step1: To test Ansible, firstly ensure that ssh is up and running on your Ansible Controller:

sudo systemctl status sshd

Step2: Create an Ansible inventory file using the below command in the Ansible controller:

The directory `ansible` and the file `hosts` could already be existing but just in case they don't exist, create them with the following commands:

sudo mkdir /etc/ansible
sudo vi /etc/ansible/hosts

Copy the IP address of your remote servers and paste into the host file

You can create a nodes group and paste ip address like below:

[VM-Nodes]
192.168.xx.xx
172.98.xx.xx

3. Configuration of Ansible Controller

Step1: Now this host file is only working after updating ansible.cfg file so we need to update config file in Ansible Controller using below command:

sudo vi /etc/ansible/ansible.cfg

Then uncommited two file


inventory = /etc/ansible/hosts
sudo-user = root

Step2: Now, create one user in all these instance(Ansible Controller and nodes)

sudo adduser ansible
sudo passwd ansible

now navigate the Ansible user

su - ansible

Try to create some files or install a package

you got some error like this

This ansible user doesn&8217;t have sudo privileges right now.

If you want to give sudo privileges to an ansible user then run the below command

4. Add User to the sudo Group

Step1: Then give some privileged in all nodes(Ansible Controller and node) using below command:

sudo visudo

go to inside this file and add

ansible ALL=(ALL) NOPASSWD:ALL

5. Update ssh_config file

For SSH connection to node from Ansible Controller make changes in sshd_config file

Step1: Now we have to some changes in ssh-config file in Ansible Controller and nodes:

vi /etc/ssh/sshd_config

Then you need to uncomment these two lines

PubkeyAuthentication yes
PasswordAuthentication yes

Now we need to restart sshd service in Ansible Controller and nodes:

sudo systemctl restart sshd
sudo systemctl status sshd

6. Establish connection between server and node

Go to Ansible Controller and run the below command

Step1: login to Ansible in Ansible Controller using the below command:

su - ansible

Step2: Run the below command to connect node:

ssh ip_address ( node ip)

7. Setup SSH keys and share it among managed nodes

To communicate with the client we have to generate SSH key on the Ansible Controller node and exchange it with Slave/Client Systems.

Step1: we need to generate ssh keygen in Ansible Controller

ssh-keygen

Step2:Now run the below command using the private IP of your node:

ssh-copy-id ansible@{private address }