Elevate your Ansible skills! This Ansible cheat sheet will help you gain essential knowledge for interviews. You’ll become familiar with important Ansible concepts, terminology, and commonly used commands. This will enable you to confidently tackle questions during an interview.
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
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.
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.
Ansible connects nodes, pushes small programs called modules to the nodes, and then removes them when they are done.
In the above diagram:
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:
Multiple remote machines can be handled by a single control machine, and it is done through Ansible by default.
$ 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
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.
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.
james:
name: james john
rollNo: 34
div: B
sex: male
Each element in a list has to be written in a new line with ‘-‘ as a prefix.
countries:
- America
- China
- Canada
- Iceland
We can have lists inside a dictionary.
james:
name: james john
rollNo: 34
div: B
sex: male
likes:
- maths
- physics
- english
Boolean terms are also used in YAML.
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” |
It is the place where all the YAML files are stored and executed. It acts as a to-do list.
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>
Some general tags in YAML:
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.
Exception handling is similar to that of any other programming language.
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’.
The most common strategies to debug playbooks are:
Playbook issues:
Some of its drawbacks are:
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.
Source: https://intellipaat.com
#ansible #devops