bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

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.

Learn DevOps in 16 hrs from experts

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.

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.

How does it work

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.

Environment setup

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

Run Ansible version command to make sure it was installed properly.

Terms

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.

YAML

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 pair

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

Each element has to be written in a new line with “-” as the prefix

  • countries:
    – America
    – China
    – Canada
    – Iceland

Lists inside the dictionary:

  • james:
    name: james john
    rollNo: 34
    div: B
    sex: male
    likes:
    – maths
    – physics
    – english

Boolean terms are also used in YAML

Wish to Learn DevOps? Click Here

Ad-hoc commands

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]
FUNCTION COMMANDS
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”

Playbooks

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

Variables

It is as similar to using variables in programming languages

  • Eg: – hosts : <your hosts>
    vars:
    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

Exception handling

  • 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.
Become DevOps Certified in 16 hrs.
CLICK HERE

Troubleshooting

The most common strategies to debugging the playbooks are

  • Debug and register
  • Use verbosity (verbosity level)

Playbook issues:

  • Quoting
  • Indentation

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.

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.

Download a Printable PDF of this Cheat Sheet

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!

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Ansible Basic Cheat Sheet"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Ansible Basic Cheat Sheet.