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.

43 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. 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
  6. This above information really Good beginners are looking for these type of blogs, Thanks for sharing article on Devops Online Training Bangalore

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

    ReplyDelete
  8. We provide 6 Months Industrial Automation Training in Delhi for Advance RLC, PLC- S7-200/S7-300/S7-400, Advance HMI/SCADA Linking, Process Instrumentation, Motion Controller, Panel Designing, Motor/Drive(AC-DC), DCS(Distributed Control System) and Overview of Robotics. Call @9953489987.

    ReplyDelete
  9. Nice Blog, I really wonder to visit your blog, Thanks a lot! for sharing the useful Information.
    iphone job Oriented course
    iphone app training course
    Best ios training in Hyderabad

    ReplyDelete
  10. • 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
  11. 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
  12. Artificial intelligence Training in noida
    Artificial intelligence Training in noida-Artificial Intelligence Training in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training

    by Real time ARTIFICIAL INTELLIGENCE Experts, Big-Data and ARTIFICIAL INTELLIGENCE Certification Training in Noida



    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



    Our Other Courses:


    artificial intelligence Training in noida

    SAS Training Institute in Delhi

    SAS Training in Delhi

    SAS Training center in Delhi

    Sap Training Institute in delhi

    Sap Training in delhi

    Best Sap Training center in delhi

    Best Software Testing Training Institute in delhi

    Software Testing Training in delhi

    Software Testing Training center in delhi

    Best Salesforce Training Institute in delhi

    Salesforce Training in delhi

    Salesforce Training center in delhi

    Best Python Training Institute in delhi



    Python Training in delhi


    Best Android Training Institute In delhi


    Best Python Training center in delhi


    Android Training In delhi


    best Android Training center In delhi

    ReplyDelete

  13. Best Solidworks training institute in noida

    SolidWorks is a solid modeling computer-aided design (CAD) and computer-aided engineering (CAE) computer program that runs on Microsoft Windows. SolidWorks is published by Dassault Systems. Solid Works: well, it is purely a product to design machines. But, of course, there are other applications, like aerospace, automobile, consumer products, etc. Much user friendly than the former one, in terms of modeling, editing designs, creating mechanisms, etc.
    Solid Works is a Middle level, Main stream software with focus on Product development & this software is aimed at Small scale & Middle level Companies whose interest is to have a reasonably priced CAD system which can support their product development needs and at the same time helps them get their product market faster.

    Company Address:
    WEBTRACKKER TECHNOLOGY (P) LTD.
    C-67,Sector-63,Noida,India.
    E-mail: info@webtracker.com
    Phone No: 0120-4330760 ,+91-880-282-0025

    webtrackker.com/solidworks-training-Course-institute-in-noida-delhi

    ReplyDelete
  14. 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
  15. 3D Animation Training in Noida

    Best institute for 3d Animation and Multimedia

    Best institute for 3d Animation Course training Classes in Noida- webtrackker Is providing the 3d Animation and Multimedia training in noida with 100% placement supports. for more call - 8802820025.

    3D Animation Training in Noida

    Company Address:

    Webtrackker Technology

    C- 67, Sector- 63, Noida

    Phone: 01204330760, 8802820025

    Email: info@webtrackker.com

    Website: http://webtrackker.com/Best-institute-3dAnimation-Multimedia-Course-training-Classes-in-Noida.php

    ReplyDelete
  16. 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

  17. Artificial Intelligence Companies in bangalore is the field of computer science dedicated to solving cognitive problems commonly associated with human intelligence, such as learning, problem solving, and pattern recognition.

    artificial intelligence companies in indiaMany business certainly expect AI to be disruptive Technology in the coming days or Testing the Waters currently, but why wait when the technology is already transforming every aspect of the way an organization operates?

    ReplyDelete
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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

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

    ReplyDelete

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

    ReplyDelete
  25. 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
  26. 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
  27. well! Thanks for providing a good stuff related to DevOps Explination is good, nice Article
    anyone want to learn advance devops tools or devops online training
    DevOps Online Training

    ReplyDelete
  28. Latest News in Hindi

    Latest News in Hindi- Hindustan channel is the best online web portal in india where you read the all latest indian news in hindi. if you are looking the Latest News in Hindi, live news channel, hindi news channel, live news channels in hindi, live hindi channels then hindustan channel is best for you.
    Latest News in Hindi

    Company address:
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025


    URL: https://hindustanchannel.com

    ReplyDelete
  29. Thanks for sharing amazing information about python .Gain the knowledge and hands-on experience in python online training

    ReplyDelete
  30. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.
    AWS Training in Chennai |Best Amazon Web Services Training in Chennai
    AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

    Best AWS Training Institute in BTM Layout Bangalore ,AWS Coursesin BTM

    ReplyDelete