Ansible User Handbook
It is usually difficult to remember all the commands that you need to work with Ansible all at once, especially if you are a beginner and have no idea about how Ansible works. This is why we have come up with this Ansible cheat sheet so that you get a quick reference to the basics of Ansible to get started.
Download a Printable PDF of This Cheat Sheet
We, at Intellipaat, are keen to support our learners in all possible means; hence, we have created this handy reference, the Ansible cheat sheet.
This cheat sheet is designed for you if you have already started learning Ansible but need a quick and handy reference to recall what you have learned.
Go through these Top DevOps Interview Questions and Answers and be ready to grab a DevOps job!
What is Ansible?
Ansible is an open-source engine that automates deployment, orchestration, cloud provisioning, and other tools. It uses a playbook, to describe jobs, and YAML, which is a human-readable programming language. Ansible is designed for multi-tier deployment. It is agentless and works by connecting nodes through SSH.
Become a DevOps master by learning end-to-end DevOps from this online DevOps Training in London!
Get 100% Hike!
Master Most in Demand Skills Now !
How does it work?
Ansible connects nodes, pushes small programs called modules to the nodes, and then removes them when they are done.
In the above diagram:
- The management node controls the whole execution of the playbook.
- The inventory file provides the list of hosts where the modules need to be run.
- The management node does ‘SSH’ connections and executes the modules and then installs the software.
Enroll in the Best DevOps Course in New York to know DevOps from scratch!
Before moving forward to work with commands, let’s understand how to set up an Ansible machine.
First, let’s discuss the types of machines used when it comes to deployment.
Types of machines:
- Control machine: A machine that helps in managing other machines.
- Remote machine: A machine that gets controlled by the control machine.
Multiple remote machines can be handled by a single control machine, and it is done through Ansible by default.
- Install it through apt, yumpkg, pip, or OpenCSW
- To install it through apt, use the following:
$ 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
- Run the ansible version command to make sure that it is installed properly.
Now, let’s discuss the important terminology used in Ansible.
Before we get into the important Ansible commands, first, let’s understand its basic terminology.
- Server: An entity that provides service for our Ansible
- Machine: A physical machine, a VM, or a container
- Target machine: An end machine to be configured by Ansible
- Task: An action
- Playbook: A location where YAML files are written and executed
As mentioned earlier, YAML (Yet Another Markup Language) is a human-readable programming language. YAML syntax is used to express the Ansible playbooks.
Here are some basic concepts of YAML.
Dictionary is represented in key/value pairs.
name: james john
Each element in a list has to be written in a new line with ‘-‘ as a prefix.
- Lists inside a dictionary:
We can have lists inside a dictionary.
name: james john
- Boolean terms are also used in YAML.
Learn more about DevOps from Intellipaat’s DevOps Training in Sydney!
An ad-hoc command is a command that we would use to do something really quick but would not want to save for later, i.e., we might not use this command in the future.
General syntax of an ad-hoc command:
Command hostgroup module/options[arguments]
|To check the connectivity of hosts
||#ansible <group> -m ping
|To reboot hosts
||#ansible <group> -a “/bin/reboot”
|To check the host system’s info
||#ansible<group> -m steup | less
|To transfer files
||#ansible <group> -m copy -a “src=home/ansible dest=/tmo/home”
|To create a new user
||#ansible<group> -m user -a “name=ansible password= <encrypted password>”
|To delete a user
||#ansible<group> -m user -a “name=ansible state- absent”
|To check if a package is installed and to update it
||#ansible<group> -m yum -a “name=httpd state=latest”
|To check if a package is installed but not to update it
||#ansible<group> -m yum -a “name=httpd state=present”
|To check if a package is of a specific version
||#ansible<group> -m yum -a “name=httpd-1.8 state=latest”
|To check if a package is not installed
||#ansible <group> -m yum -a “name= httpd state= absent
|To start a service
||#ansible<group> -m service -a “name= httpd state=”started”
|To stop a service
||#ansible<group> -m service -a “name= httpd state=”stopped”
|To restart a service
||#ansible<group> -m service -a “name= httpd state=”restarted”
If you have any more queries related to DevOps, visit our DevOps Community and get them clarified in no time!
It is the place where all the YAML files are stored and executed. It acts as a to-do list.
- A playbook can have more than one play.
- A play maps the instructions defined against a particular host.
- It is typically written in a text editor such as 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 a playbook
- Hosts: A mandatory field that specifies the list of hosts and the tasks that can be performed on the same machine or a different one
- Vars: Defines the variables that we can use
- Tasks: The list of actions that need to be performed; contains the name of the task (a task is always linked to a module)
Looking forward to becoming a DevOps expert? Sign up with this DevOps Course in Toronto now!
It is similar to using variables in any other programming language.
hosts : <your hosts>
tomcat_port : 8080
Here, the tomcat_port is the variable, and it has been assigned to Port 8080.
- Block: The Ansible syntax to execute a given block
- Name: The name of the block
- Action: The code to be executed and is next to the action tag
- Register: Registers the output
- Always: Executes no matter what the state is
- Msg: Displays the message
Exception handling is similar to that of any other programming language.
- Keywords: rescue and always
The code is written in the block. It goes to the rescue phase and gets executed if the command in the block fails.
The ‘always’ keyword is similar to the regular ‘always’ and is executed no matter what the state is. Therefore, the ‘block’ keyword is the same as ‘try block’, and ‘catch block’ is like ‘rescue’.
Wish to grab an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore today!
The most common strategies to debug playbooks are:
- Debug and register
- Use verbosity (verbosity level)
Some of its drawbacks are:
DevOps Engineers are among the highest paid professionals in the IT domain. What are you waiting for? Join DevOps Training in Hyderabad now!
- OS restrictions: It is dependent on OS, so the code on one operating system will not work on another.
- Once a playbook is running, adding hosts is not possible.
- Error reporting is mediocre.
Advantages of Ansible
- Ansible is free and open-source.
- It is agentless; it does not have a master–client model instead uses the SSH model.
- It has flexible system requirements.
- Ansible is developed in Python.
- It is lightweight and quick to deploy.
- Ansible uses YAML syntax in config files.
- Ansible has a large community base.
With this, we have come to the end of this Ansible cheat sheet tutorial. We have covered all the basics of Ansible here.
In our comprehensive DevOps training course, not only will you get to learn and implement Ansible with ample guidance from us, but you will also learn other important topics in DevOps such as Docker, Git/GitHub, Chef, Jenkins, Puppet, Selenium, Nagios, etc.
While studying Docker, Docker Cheat Sheet is very handy.
You will also get round-the-clock technical support from our DevOps experts who will help you with all your queries.
So, why wait? Join Intellipaat’s extensive DevOps Training and be a certified DevOps Engineer!
Watch this Ansible Tutorial for Beginners video to learn more: