Ansible Basic Cheat Sheet Tutorial

Ansible Basic Cheat Sheet Tutorial

Last updated on 29th Sep 2020, Blog, Tutorials

About author

Saranraj (Sr DevOps Engineer )

He is a Proficient Technical Expert for Respective Industry & Serving 11+ Years. Also, Dedicated to Imparts the Informative Knowledge to Freshers. He Share's this Blogs for us.

(5.0) | 16225 Ratings 1155

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.

How does it work?

Ansible connects nodes, pushes small programs called modules to the nodes, and then removes them when they are done.

 called-modules

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.
Subscribe For Free Demo

Error: Contact form not found.

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, 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-getupdate
  • $ sudo apt-get install ansible
  • Run the ansible version command to make sure that it is installed properly.

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.

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 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”
Course Curriculum

Start Your Ansible learning Journey from Ansible Training & Certification Course

Weekday / Weekend BatchesSee Batch Details

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>

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 on 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)

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’.

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 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.
Ansible Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Conclusion

This guide covers some of the most common Ansible commands you may use when provisioning servers, such as how to execute remote commands on your nodes and how to run playbooks using a variety of custom settings.

There are other command variations and flags that you may find useful for your Ansible workflow. To get an overview of all available options, you can use the help command:

Are you looking training with Right Jobs?

Contact Us

Popular Courses