Saturday 2 July 2016

Automation with Ansible

Ansible is an open source software which is used as configuration management tool as well as orchestration. Using Ansible, we can deploy multiple application in multiple nodes simultaneously without any human interactions. When it comes to features, it covers following things.

  • Agentless
  • Easy to use 
  • Intelligent automation system
  • Unlike other configuration management tools like puppet and chef, in Ansible there is no need to install additional packages on remote nodes.

Terms:
Following terms are considered while using Ansible.

Control node - Control node is a machine where we need to install ansible. This control node controls remote nodes.

Managed hosts - Remote nodes are called as managed hosts.

Inventory file - This is a simple text file. It contains the location of managed hosts in the control node.

You can find more terms here, that are used in Ansible.

Requirements:
  • Passwordless SSH authentication between control node and managed hosts.
  • Python could be installed on managed hosts. 
     (Nowadays, Mostly all Linux distributions comes with pre-installed Python package and SSH. So there is no need to install anything on the managed hosts to use Ansible. Only requires passwordless SSH authentication)

Passwordless authentication between Control node & Managed hosts:

Generate SSH key on Control node

#ssh-keygen


Copy SSH key to Managed hosts
    
 #ssh-copy-id <Managed Host IP/Hostname>



(Note: Here my control node IP is 192.168.56.1. I have two managed hosts that are 192.168.56.101 and 192.168.56.102)  

Now try to login via SSH to Managed hosts

#ssh <managed host IP/hostname>



Now it will login to managed hosts without providing a password.

Installing Ansible on Control node

#dnf install -y ansible                    (For Fedora based machines) 
#yum install -y ansible                  (For Red Hat, CentOS, Scientific Linux based machines) 
#apt-get install ansible                 (For Ubuntu based machines)
Inventory File
  
     Inventory file is a simple text file. It is located in /etc/ansible/hosts by default in the control node. But we can also able to change the inventory file location in /etc/ansible/ansible.cfg file. inventory file stores the location of managed hosts. Managed hosts may be separated into different groups in the inventory file.

Adding managed hosts in inventory file
vim /etc/ansible/hosts
[web-servers]
192.168.56.101
192.168.56.102 




Here 192.168.56.101 and 192.168.56.102 are my managed hosts. web_servers is group name of those managed hosts. Groups are useful in inventory file. It is used to divide managed hosts depends on it's functional usage.

Testing connectivity

By using ping module we can check the connectivity of control node and managed nodes. There are lot of modules are available in Ansible. You can find all those Ansible modules here.

#ansible -m ping web-servers


Ad-hoc commands
  
   Ad-hoc commands are nothing but a single line command. The following command is one example for ad-hoc command.


#ansible -m command -a 'uptime' web_servers

here, -m = modules and -a = attribute.It will show how long the system has been running.



Playbooks

  Playbooks are nothing but a YAML format text file. Ansible playbooks are written in YAML format human readable language format. It makes Ansible easy to understand.
  When we want to deploy multiple applications on managed hosts or need to configure something on managed hosts, that time of situations ansilbe playbooks comes here. It is like shell script, but easier than writing shell script. Playbook contains multiple tasks and it executes on multiple machines.

Example Playbook

   The following playbook will install apache server on managed hosts and finally test the apache server.
#vim Web-server.yml
---
- name: setup website
  hosts: web_servers
  tasks:
  - block:
    - name: Install httpd package                  
      yum: name=httpd state=latest          
  - block:
    - name: firewalld permits http service
       firewalld: service=http permanent=true state=enabled
  - block:
    - name: httpd enabled and running
      service: name=httpd enabled=true state=running
    - name: firewalld enabled and running
      service: name=firewalld enabled=true state=restarted
  - block:
    - name: test html page
      shell: echo "Welcome to Ansible test page" > /var/www/html/index.html
- name: Test website
   hosts: web_servers
   tasks:
   - name: latest python-httplib2 version installed
      yum: name=python-httplib2 state=latest
   - name: Verify the web server
      uri: url=http://localhost status_code=200  
You can also get this above code in my github acccount. Here is the link to get the code.
Save the above code as example_playbook.yml file. Here I am using multiple modules. you can get modules usage and functions on this page.

Note: Ansible strictly follows indentation so be careful while giving whitespaces on YML scripts.
You can also check your indentation of scripts by using following command,
#ansible-playbook --syntax-check  <playbook file>
In the above code, tasks can be separated by blocks. In above code,

  • The first block of code will install apache package. Here I am using yum module.
  • The second block of code will allow http in the firewall.   
  • The third block of code enables apache and firewalld on startup. Here i am using service module.
  • The fourth block of code will create index file. Here i am using shell module.
  • Finally, the last task will test web server.
Running Playbook
    #ansible-playbook example_playbook.yml



If you are facing any issue while doing this, feel free to put your thoughts in command box. I will help you. Thanks for reading.

Getting help
   Similar to man pages in Linux, there is a command available for Ansible where you can get help.
#ansible-doc <module-name>
For an example service module
#ansible-doc service

 If you are facing any issue while doing this, feel free to put your thoughts in command box. I will help you. Thanks for reading.

96 comments:

  1. Nice Info Regarding Automation about the ansible my sincere thanks for sharing this post Please Continue to share this kind of post
    Devops Training in Bangalore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice and rare Blog On Automation with ansible
    Thank you For sharing.
    Best Devops Training in anglore

    ReplyDelete
  4. It's Really A Great Post. Looking For Some More Stuff
    I really enjoyed reading the Post. It was very informative and useful for me.
    Best Java Training institute in Bangalore

    ReplyDelete
  5. Nice Information about, Check it once through Devops Online Training

    ReplyDelete
  6. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Ansible Training

    ReplyDelete
  7. This above information really Good beginners are looking for these type of blogs, Thanks for sharing article on Devops Online Training Bangalore

    ReplyDelete
  8. Thanks For Posting Valuable Information On Selenium Webdriver Tutorial .Very Nice blog With very good Content.

    ReplyDelete
  9. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating IOT Online Training

    ReplyDelete
  10. Thanks for giving a great information about automation-with-ansible DevOps Good Explination nice Article
    anyone want to learn advance devops tools or devops online training visit: DevOps Online Training contact Us: 9704455959

    DevOps Training in Ameerpet

    ReplyDelete
  11. HMI Training | HMI Training in Noida | HMI Training in Delhi- 9818293887
    HMI training in noida offers by DIAC Corporate trainers with 100% placement assistance. The hmi training at an affordable price which is customised as per each candidate’s requirement of modules and content. Call @9310096830

    ReplyDelete
  12. Graphics designing training institute in Noida
    Best Graphics training institute in Noida, Graphic Designing Course, classes in Noida- webtrackker is providing the graphics training in Noida with 100% placement supports. If you are looking for the Best Graphics designing training institute in Noida For more call - 8802820025.

    Graphics designing training institute in Noida, Graphics designing training in Noida, Graphics designing course in Noida, Graphics designing training center in Noida

    Company address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025
    Email: info@webtrackker.com
    Website: http://webtrackker.com/Best-institute-for-Graphic-Designing-training-course-in-noida.php

    ReplyDelete
  13. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    Data Science training in marathahalli
    Data Science training in btm
    Data Science training in rajaji nagar
    Data Science training in chennai
    Data Science training in kalyan nagar
    Data Science training in electronic city
    Data Science training in USA
    Data science training in pune



    ReplyDelete
  14. Webtrackker Technology is IT Company and also providing
    the Solidwork training in Noida at running project by
    the real time working trainers. If you are looking for
    the Best Solidwork training institute in Noida then you can contact
    to webtrackker technology.
    ads
    Webtrackker Technology
    C- 67, Sector- 63 (Noida)
    Phone: 0120-4330760, 8802820025

    8802820025
    Solidwork training institute in Noida

    ReplyDelete
  15. Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
    java training in marathahalli | java training in btm layout

    java training in jayanagar | java training in electronic city

    java training in chennai | java training in USA

    selenium training in chennai

    ReplyDelete
  16. Great information...
    Thanks for Sharing...
    PLC SCADA Training, PLC Training, Industrial Automation Training, Internships, Industrial Training Great opportunity, Job Oriented Training, 100% placement assistance with leading industries. Call: +91 - 8447755969(Training).


    ReplyDelete
  17. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python training in pune
    python online training
    python training in OMR

    ReplyDelete

  18. nice information About DevOps Thanks For Sharing
    any one want to learn devops or DevOps Online Training visit Us:
    DevOps Online Training

    ReplyDelete

  19. nice information About DevOps Thanks For Sharing
    any one want to learn devops or DevOps Online Training visit Us:
    DevOps Online Training

    ReplyDelete
  20. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops training in sholinganallur
    Devops training in tambaram
    DevOps online Training

    ReplyDelete
  21. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.

    Blue Prism Training Course in Pune

    Blue Prism Training Institute in Bangalore

    ReplyDelete
  22. This is really an amazing post, thanks for sharing such a valuable information with us, keep sharing!!
    DevOps Online Training

    ReplyDelete
  23. The information which you have provided is very good. It is very useful who is looking for at machine learning online training

    ReplyDelete
  24. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  25. Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us. machine learning training in chennai
    best training insitute for machine learning
    machine learning course in Chennai

    ReplyDelete
  26. Thanks For Sharing The Information Please Keep Updating us Information Shared Is Very Valuable Time Just Went On Reading The Article Python Online Training AWS Online Training Data Science Online Training Data Science Online Training Hadoop Online Training

    ReplyDelete
  27. Thank you for sharing such great information very useful to us.
    Linux Training in Noida

    ReplyDelete
  28. Thank you for your post. This is superb information. It is amazing and great to visit your site.
    Industrial Automation Training in Noida

    Industrial Automation Training institute in Noida

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Nice blog Content.It is very informative and helpful. Please share more content. Thanks.
    SAP Training institute in Delhi

    ReplyDelete
  31. DIAC provide 6 months industrial training in noida which aims to provide best training to our student to achieve maximum knowledge in their respective field. After the training the student will get enough talent and working experience to be placed in any renowned company. Call @91-9953489987.

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete

  33. very useful information, the post shared was very nice.
    Docker Online Training

    ReplyDelete
  34. I Got Job in my dream company with decent 12 Lacks Per Annum salary, I have learned this world most demanding course out there in the current IT Market from the data science course in puneProviders who helped me a lot to achieve my dreams comes true. Really worth trying instant approval blog commenting sites

    ReplyDelete
  35. The career in blockchain technology has a huge growth in the industry . this is through blockchain online course

    ReplyDelete
  36. I learned World's Trending Technology from certified experts for free of cost.i Got job in decent Top MNC Company with handsome 14 LPA salary, i have learned the World's Trending Technology from Python training in pune experts who know advanced concepts which can helps to solve any type of Real time issues in the field of Python. Really worth trying instant approval blog commenting sites

    ReplyDelete
  37. Hi,
    Best article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Python Training Institutes In Btm. Because Python course in Bangalore is one of the best that one can do while choosing the course.

    ReplyDelete
  38. There are times when people are having lots of trouble while they are planning to aol gold download in their computer. If you are also one of them, you need first to see whether your system is fulfilling all the requirement which is required for downloading the software.

    ReplyDelete
  39. The process to setup Roadrunner email on a device can be time-consuming and tricky if you do not have a clear idea about how to configure the server settings and what are the correct details that must be added. Do not waste even a single minute and promptly connect to the services of roadrunner customer care and have a word with the techies to avail all the answers.

    ReplyDelete
  40. Thanks for sharing such an amazing information its very beneficial for our company. our company name is innomatics research labs we offering data science,big data and many more courses to make student career success full and we are giving online, classroom and corporate training our website .Automation Anywhere Training in Bangalore

    ReplyDelete
  41. Thanks for sharing such a good blog. Keep posting like this useful info !!
    DevOps Training
    DevOps Online Training

    ReplyDelete
  42. Nice Blog!1 It is very helpful and very informative and I really learned a lot from it.
    DevOps Training
    DevOps Classroom Training in Hyderabad

    ReplyDelete
  43. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day,keep iy up guys.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  44. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing.
    DevOps Training
    DevOps Online Training
    DevOps Training in Ameerpet

    ReplyDelete
  45. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.Your blog has very useful information about this technology which i am searching now, i am eagerly waiting to see your next post as soonI just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  46. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article.

    angular js training in chennai

    angular training in chennai

    angular js online training in chennai

    angular js training in bangalore

    angular js training in hyderabad

    angular js training in coimbatore

    angular js training

    angular js online training

    ReplyDelete
  47. it’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them useful information.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  48. Am Divya,Am really impressed about this blog because this blog is very easy to learn and understand clearly.This blog is very useful for the college students and researchers to take a good notes in good manner,I gained many unknown information.

    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  49. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.


    python Training in chennai

    python Course in chennai

    ReplyDelete
  50. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    python Training in chennai

    python Course in chennai

    ReplyDelete
  51. Getting into Integrated Marketing is tough if you don’t have thorough knowledge. Then why not join Talentedge, the first ed-tech platform that has joined hands with XLRI and MICA to provide the best courses to the students.

    ReplyDelete
  52. tools that automate and scale events personalize attendee experiences and deliver positive ROI. business meeting thank you note and free registration software

    ReplyDelete
  53. Visit Bharat Go Digital Academy to learn the digital marketing skills in India.

    ReplyDelete
  54. Wonderful blog. It is really informative to all.keep update more information about this
    Benefits of Using Software Testing
    Importance of Software Testing in Businesses

    ReplyDelete
  55. This post is so helpfull and informative.keep updating with more information...
    IOS Language
    IOS Programming

    ReplyDelete
  56. This post is so interactive and informative.keep update more information...
    AWS Training in Tambaram
    AWS Training in Chennai

    ReplyDelete
  57. At the point when one purchases any singular Office applications or a one-time buy Office, then, at that point, he/she can get a free item key to introduce the Office. Microsoft Office 365 Keygen Generator

    ReplyDelete
  58. This comment has been removed by the author.

    ReplyDelete