CentOS 7 configuration using Ansible



How does Ansible work?

Purpose of this Article



In this tutorial, I am using a Ubuntu server to install some utilities and Jenkins server on CentOS 7 server.


$ sudo apt-get update
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible -y

2. Generate SSH key using the command given below:

$ ssh-keygen -t rsa

It will generate a public and private key in ~/.ssh/ folder. The public key will be used later.

3. Create a CentOS 7 server on any cloud. In this tutorial, I am using a CentOS 7 based VM on Google Cloud because it provides 300 free credits.

4. Once the server is in running state. Add your ssh pub key generated on Ubuntu VM to CentOS 7 VM. SSH pub key can be found in this file~/.ssh/id_ras.pub.

5. When the ansible gets installed it creates a hosts file(/etc/ansible/host) that holds the configuration for the nodes. Add the IP of the CentOS 7 based server in this file in this format:


6. Ping the nodes to verify they are accessible by ansible:

ansible -m ping all

It will output a message regarding the nodes' accessibility.

7. Once nodes are accessible, create an inventory file(with .yml extension) that holds the information about the packages that will be installed on the nodes

In the inventory file, tasks are added and each task performs an operation on the server. In these tasks following operations are being performed:

  • creates a Linux user named devops.
  • install a few packages.
  • add a docker repository.
  • install docker.
  • start docker service.
  • create a docker group.
  • add devops user to the docker group, so that sudo is not used for performing docker operations.
  • install docker-compose.
  • make the docker-compose utility’s file executable.

8. Run ansible-playbook using the command given below:

ansible-playbook <playbook-name> -v

-v specifies the verbosity level.

9. ssh the server and verify packages are installed successfully. If you have multiple servers, verify each node one by one:

ssh <username>@NODE_1_IP_PLACEHOLDER

10. I have created a video of the above steps being performed. Check it out.

Final Thoughts

DevSecOps Engineer https://irtizaali.com/