Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
Using Ansible to Manage Remote Machines
Next article icon

Using Ansible to Manage Remote Machines

Last Updated : 02 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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, 


Next Article
Using Ansible to Manage Remote Machines

A

afifahmed
Improve
Article Tags :
  • DevOps
  • Ansible

Similar Reads

    How to Install rpm Package in Linux Using Ansible ?
    Ansible, a strong computing automation tool, expands its capabilities beyond configuration management to package management, including the installation of RPM packages on Linux frameworks.RPM (Red Hat Package Manager) is a package management system executives framework utilized by different Linux di
    7 min read
    How To Install PIP Using Ansible ?
    Nowadays the IT culture is evolving with Agile practices and DevOps Culture, on idea of making the business plans, and features quickly to market and making users available. In this software development workflow configuring software and updating the patches to each system manually or through automat
    6 min read
    How to Configure Terrform Using Ansible ?
    In the present rapidly advancing technological scene, the demand for versatile, reliable, and cost-effective infrastructure arrangements is higher than ever time in recent. Accomplishing these goals requires effective automation tools that can flawlessly oversee infrastructure provisioning and confi
    7 min read
    How To Install Java Using Ansible PlayBook ?
    In today's dynamic IT landscape, effective management and automation of software installations across various servers are pivotal for maintaining consistency, dependability, and versatility. Ansible, an open-source automation tool, has acquired huge popularity for its simplicity and flexibility in a
    7 min read
    How to Install Helm using Ansible Playbook?
    Managing various infrastructure and applications under different conditions in the present dynamic IT landscape can challenge. Automation tools like Ansible assist with smoothing out these undertakings by providing a basic yet amazing method to automate configuration management, application deployme
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences