Ansible User Handbook:
Are you finding difficulty in remembering all the commands that you need in order to work with Ansible?
Guys don’t worry if you are a beginner and have no idea about how Ansible works, this Ansible cheat sheet will give you a quick reference of the basics that you must know to get started.
While at Intellipaat, we support our learners with a handy reference, that’s the reason we have created this Cheat sheet.
This cheat sheet is designed for the one who has already started learning about Ansible but needs a handy reference sheet.
Watch this Ansible Tutorial for Beginners video
Prepare yourself for the Top DevOps Interview Questions And Answers!
What is Ansible?
It is an open source engine that automates deployment, orchestration, cloud provisioning and other tools. It uses a playbook to describe jobs and uses YAML which is human readable and is designed for multi- tier deployment. It is agentless and works by connecting nodes through SSH.
Become Master of DevOps by going through this online DevOps training in London.
How does it work?
Ansible does its work by connecting the nodes and pushes small programs called modules to them and are removed when they are done.
If we conclude the above diagram:
- The management node controls whole execution of the playbook.
- The inventory file provides the list of hosts where the modules need to be run.
- The management node does an ‘SSH’ connection and executes the modules and installs the software.
Go through the Best DevOps Course in New York to get clear understanding of DevOps.
Before moving forward and working on commands, let’s understand few aspects for environment setup and then we will setup the Ansible Machine.
Let’s discuss the types of machine when we are talking of deployment.
Types of machines:
- Control machine: A type of machine which helps in managing the other machines.
- Remote machine: A type of machine which gets controlled by the other machines.
Multiple remote machines can be handled by one machine, and the remote machine managing is done through ansible by default.
- Install it through apt, yumpkg, pip, OpenCSW
- Installing it through apt:
- $ sudo apt-get update
- $ sudo apt-get install software-properties-common
- $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update
- $ sudo apt-get install ansible
- $ sudo apt-get update
Run Ansible version command to make sure it was installed properly.
Now let’s check the major differences between Agile and DevOps
Before looking into important commands of Ansible, first let’s understands its terms
- Server: An entity that provides service for your Ansible.
- Machine: A Physical Machine, VM or a container.
- Target machine: An End machine to be configured by ansible.
- Task: An action.
- Playbook: A location where YML files are written and executed.
Watch this interesting video on Puppet Installation
YAML syntax is used to express the ansible playbooks
Here are some basic concepts of YAML:
Dictionary is represented in key value pair
- Eg: james:
name: james john
- Representing lists:
Each element has to be written in a new line with “-” as the prefix
Lists inside the dictionary:
name: james john
Boolean terms are also used in YAML
Learn more about DevOps in this DevOps training in Sydney to get ahead in your career!
An Ad-hoc commands are something that you might type in order to do something really quick, but you don’t want to save them for late, which means those commands need not to be performed later.
General syntax of ad-hoc command:
- Command hostgroup module/options[arguments]
|Check connectivity of hosts||#ansible <group> -m ping|
|Rebooting hosts||#ansible <group> -a “/bin/reboot”|
|Check host system’s info||#ansible<group> -m steup | less|
|Transfering files||#ansible <group> -m copy -a “src=home/ansible dest=/tmo/home”|
|Create new user||#ansible<group> -m user -a “name=ansible password= <encrypted password>”|
|Deleting user||#ansible<group> -m user -a “name=ansible state- absent”|
|Check if package is installed and update it||#ansible<group> -m yum -a “name=httpd state=latest”|
|Check if package is installed and don’t want to update it||#ansible<group> -m yum -a “name=httpd state=present”|
|Check if package is s specific version||#ansible<group> -m yum -a “name=httpd-1.8 state=latest”|
|Check if package is not installed||#ansible <group> -m yum -a “name= httpd state= absent|
|Starting a service||#ansible<group> -m service -a “name= httpd state=”started”|
|Stopping a service||#ansible<group> -m service -a “name= httpd state=”stopped”|
|Restarting a service||#ansible<group> -m service -a “name= httpd state=”restarted”|
Watch this video on Nagios Installation for Beginners:
If you have any doubts or Queries related to DevOps, get it clarifies from DevOps Experts on DevOps Community.
It is the place where all YAML files are stored and executed. Acts like a to-do list.
- YAML: Yet Another Markup Language
- A playbook can have more than one plays.
- Plays map the instructions defined against a particular host
- Typically written in a text editor like notepad or notepad++
Sample playbook/YAML file:
name: install and configure DB
-name: Install the Oracle DB
yum: <code to install the DB>
-name: Ensure the installed service is enabled and running
name: <your service name>
Some general tags of YAML:
- Name: Name of the playbook
- Hosts: Specifies the list of hosts, tasks can be on the same machine or a different one and is a mandatory field.
- Vars: Defines the variables which you can use.
- Tasks: It is the list of action that needs to be performed, which contains the name of the task. A task is always linked to a module.
Watch this DevOps Salary Report video by Intellipaat:
Interested in becoming DevOps Expert? Click here to learn more in this DevOps Course in Toronto!
It is as similar to using variables in programming languages
- Eg: – hosts : <your hosts>
tomcat_port : 8080
Here tomcat port is the variable and it has been assigned to Port 8080
Important Keywords used:
- Block: Ansible syntax to execute a given block
- Name: Name of the block
- Action: The code that is to be executed and is next to the action tag
- Register: Registers the output
- Always: States that below word will be always executed
- Msg: It displays the message
Interested in getting an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore now!
- It is similar to any other programming language
- Keywords : rescue and always
- The code is written in block
- It goes to the rescue phase and gets executed if the command in the block fails.
- Always is similar to the regular “always” and is executed no matter the state.
- Thereby block is the same as “try block “, catch block is like “rescue” and always performs the same function as we know.
Now, you know the most important skill set of a DevOps Engineer. But, do you know, DevOps Engineers are among the highest paid professionals in the technology domain? so join DevOps training in Hyderabad!
The most common strategies to debugging the playbooks are
- Debug and register
- Use verbosity (verbosity level)
Some of its drawbacks are:
- OS restrictions: It is dependent of OS, so code on one OS will not work for another.
- Once playbook is running, adding of hosts is not possible.
- Error reporting is mediocre.
Get in touch with Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer!
Advantages of Ansible
- It is free and open source.
- It’s Agentless, no master client model.
- Uses SSH model.
- System requirements.
- Developed in python.
- Lightweight and quick deployment.
- Ansible uses YAML syntax in config files.
- Large community base.
Watch this video on DevOps Tutorial for Beginners
We have covered all the basics of Ansible in this cheat sheet. If you want to start learning DevOps in depth then check out the DevOps Certification by Intellipaat.
Not only will you get to learn and implement Ansible with a step by step guidance and support from us, but you will also get to learn other important topics in DevOps such as Docker, Git/GitHub, Chef, Jenkins, Puppet, Selenium, Nagios and many more. You will also get 24*7 technical support to help you with any and all of your queries, from the experts in the respective technologies here at intellipaat throughout the certification period. So, why wait? Check out the training program and enroll today!