• Articles
  • Tutorials
  • Interview Questions

Ansible Cheat Sheet

Ansible User Handbook

For beginners new to Ansible and unfamiliar with its workings, it can take time to remember all the necessary commands at once. To address this issue, we have created an Ansible cheat sheet that serves as a handy reference for getting started with the basics of Ansible. This cheat sheet provides quick access to essential information, making it easier for beginners to navigate and utilize Ansible effectively. 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 get started.

Download a Printable PDF of This Cheat Sheet

 

Ansible Full Image

We, at Intellipaat, are keen to support our learners in all possible ways; 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 a powerful open-source automation engine that simplifies deployment, orchestration, and cloud provisioning tasks. It employs a playbook, which is a descriptive file written in YAML, a programming language that is easy for humans to read and understand. Ansible is specifically designed to handle multi-tier deployments efficiently. One of its key advantages is that it operates in an agentless manner, establishing connections between nodes using SSH.

Get 100% Hike!

Master Most in Demand Skills Now!

Become a DevOps master by learning end-to-end DevOps from this online DevOps Training in London!

How does it work?

Ansible connects nodes, pushes small programs called modules to the nodes, and then removes them when they are done.
How does it work
In the above diagram:

  • The management node controls the whole execution of the playbook.
  • The inventory file provides a 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 learn DevOps from scratch!

Environment Setup

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, yum, pkg, 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.

become a devops architect

Ansible Terms

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

YAML

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.

  • Key/Value pair:

Dictionary is represented in key/value pairs.

    • Example:
james:
name: james john
rollNo: 34
div: B
sex: male
  • Representing lists:

Each element in a list has to be written in a new line with ‘-‘ as a prefix.

    • Example:
countries:
- America
- China
- Canada
- Iceland
  • Lists inside a dictionary:

We can have lists inside a dictionary.

    • Example
james:
name: james john
rollNo: 34
div: B
sex: male
likes:
- maths
- physics
- english
  • Boolean terms are also used in YAML.

Learn more about DevOps from Intellipaat’s DevOps Training in Sydney!

Ad-hoc Commands

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]
Function Command
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 setup | 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”

 

Playbooks

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
hosts: testServer
become: yes
vars:
oracle_db_port_value: 1521
tasks:
-name: Install the Oracle DB
yum: <code to install the DB>
-name: Ensure the installed service is enabled and running
service:
name: <your service name>

Check out the Ansible Interview Questions to prepare for the interviews.

Some general tags in 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 for this DevOps Course in Toronto now!

Variables

It is similar to using variables in any other programming language.

hosts : <your hosts>
vars:
tomcat_port : 8080

Here, the tomcat_port is the variable, and it has been assigned to Port 8080.

Important Keywords

  • 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

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!

Troubleshooting

The most common strategies to debug playbooks are:

  • Debug and register
  • Use verbosity (verbosity level)

Playbook issues:

  • Quoting
  • Indentation

Some of its drawbacks are:

  • OS restrictions: It is dependent on the 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.


DevOps Engineers are among the highest-paid professionals in the IT domain. What are you waiting for? Join DevOps Training in Hyderabad now!

Advantages of Ansible

  • Ansible is free and open-source.
  • It is agentless; it does not have a master–client model but 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, the 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.

Watch this Ansible Tutorial for Beginners video to learn more:

Video Thumbnail

We hope this tutorial helps you gain knowledge of DevOps course. If you are looking to learn DevOps course online in a systematic manner with expert guidance and support then you can enroll to our DevOps Training.

Course Schedule

Name Date Details
DevOps Certification 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Big Data Engineering

Abhijit is a Technical Research Analyst specialising in Big Data and Azure Data Engineering. He has 4+ years of experience in the Big data domain and provides consultancy services to several Fortune 500 companies. His expertise includes breaking down highly technical concepts into easy-to-understand content.