Using Ansible to Manage Remote Machines - GeeksforGeeks

02 Mar, 2021
Ansible is an automation tool used for common IT tasks such as configuring remote machines or container orchestration, continuous deployment, etc.  In this article, we are going to use Ansible to configure multiple remote machines using a master or control node( machine which manages or pushes tasks to these remote machines to execute). 
Before starting the implementation you should be familiar with some terminologies:
Control nodes are used to run Ansible commands and playbooks by invoking the ansible or ansible-playbook command.
Hosts are nothing but the remote machines which are controlled by the control node, an SSH connection is established between these machines and the control node before pushing tasks by control node and hosts receiving it.
Playbooks are configuration files/tasks files written in YAML.These contain set of tasks which needs to be executed on the hosts sequentially.
Now as you know the basics lets start with the implementation. For this article, we are using Three EC2 instances on AWS, out of which one is a control node or master and the other two are hosts/remote machines. EC2 (Elastic compute cloud) is Amazon’s one of most popular offerings. The EC2 instance is a virtual server or machine, you can use to deploy your application. It comes under IAAS (Infrastructure As A Service). EC2 service capabilities are related to Computing, Storage, and Network on rent.
If you want to use EC2 instances then you need to make sure that each of the instances(hosts) is reachable by the control node and to do that you need to add the SSH public keys of the host in the control nodes authorized SSH keys and vice-versa. So, lets do that first using the below command:
//on each machine run and hit enter this will generate two files in root namely .ssh/id_rsa and .ssh/id_rsa.pub
$ ssh-keygen
Now cut and copy the content of id_rsa.pub and add this to other servers that are hosts. If you are doing it for the control node on the hosts use the below commands:
$ cat ~/.ssh/id_rsa.pub

//on other servers
$ cat {coppied_content} >> ~/.ssh/authorized_keys
Now check whether you can ssh to host machines from the control machine
On Control-node we need to install Ansible:
 $ sudo apt-get install software-properties-common 
 $ sudo apt-add-repository ppa:ansible/ansible //this will add ansible repository in your machine
 $ sudo apt-get update
 // install ansible using
 $ sudo apt-get install ansible
Check if Ansible is installed:
$ ansible --version
Now we need to add the host address inside the host inventory which is a file in /etc/ansible called host where we can add IP-addresses or aliases or group number of IP-addresses. Go to /etc/ansible/hosts file and add the IP address of the host machine. You can either enter one by one or create a group which can then be used in the playbook to specify these bunch of addresses by entering the group name.
Now let’s check if Ansible can reach our host. Ansible modules are pre-built scripts that can be used inside our playbook or as a standalone command with some parameters to run specific tasks for example the below command is used to ping all the servers in that group.
 ansible -m ping <group-name> 
In the above image, you can see that after a ping request to the group of a couple of servers, got a successful callback.
Let’s try some more Ansible modules
// check the os-release of our hosts
$ ansible web-servers -a "cat /etc/os-release"
You can also reboot the machines using
$ ansible -a web-servers "reboot"
Now let’s jump into using Ansible playbooks as explained above these are nothing but YAML files.
Let’s check if text editor nano is installed in our host machines(remote machines). Create a YAML file and paste the below lines to make sure the indentation is correct. What’s happening here is: 
name –  is the name of playbook
hosts – group name where we added our servers
tasks – is a set of tasks written as an array in YAML apt is the command and state is the state of the service or nano. 
- name: isnano
  hosts: web-servers
  tasks:
    - name: ensure nano is installed
      apt:
       name: nano 
       state: latest
There is a lot you can do with Ansible, control servers and remote servers or machines using modules and playbooks so don’t stop here go and try other stuff using Ansible, 
Advanced Computer Subject
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Copying Files to and from Docker Containers
Markov Decision Process
Fuzzy Logic | Introduction
Q-Learning in Python
Principal Component Analysis with Python
OpenCV - Overview
ML | What is Machine Learning ?
Basics of API Testing Using Postman
Getting Started with System Design
ML | Introduction to Data in Machine Learning