Ansible is a powerful open-source automation tool that is meant for configuration management and application deployment. It works with agentless architecture and a push-based mechanism. It uses YAML syntax for orchestrating the infrastructure efficiently across different environments. It will be effective and preferable in managing both small-scale setups and large-scale enterprise systems. Ansible helps in streamlining operations, enhancing productivity and scalability.
.webp)
In this article "Ansible Cheat Sheet", we will explain in short and effectively the concepts of Ansible such as Ansible, Ansible Architecture, Ansible Installation, Ansible Inventory, Ansible ad-hoc commands, ansible Playbook commands, ansible-vault commands, ansible-navigator commands, ansible-galaxy commands. In this, we covered all the essential commands of respective concepts effectively detailing their functionalities. In addition, we discussed the troubleshooting commands and document commands for guiding you to be effective with troubleshooting skills and exploring more options of the ansible features. Now, let's start by going through this article.
What Is Ansible?
Ansible is an open-source configuration management tool that automates and manages things such as software provisioning, configuration management, and application deployment across different environments.
What Is Ansible Architecture?
The Ansible comes as Agentless architecture with a push-based mechanism. It means there is no need for pre-installation of software on worker nodes to connect to the manager node.
The system one which Ansible is installed is treated as the Manager Node and the Other systems that are managed by Manager Node For establishing the connection to worker nodes, Ansible Manager uses some kind of protocol, for Linux systems ssh protocol and for Windows systems WinRM protocol is mostly used in behind.

The information of worker nodes such as username, password, IP address, and protocol are specified in a file in Ansible manager known as Inventory.
Ansible Keywords
Ansible keywords are the fundamental elements that are used for structuring and defining the automation of tasks in the Playbooks with YAML syntax. These are the elements that helps in organizing the tasks and helps in orchestrating the automation process effectively.
Ansible Keywords
| Description
|
|---|
play
| It termed for defining the set of tasks that to be executed On remote hosts.
|
|---|
task
| An Action performed by ansible on remote hosts.
|
|---|
role
| it is an organized structure directory with files containing a predefined set of tasks, handlers, variables and files
|
|---|
Module
| These are Reusable, Standalone ansible scripts sued to perform tasks on Worker nodes.
|
|---|
vars
| It is used for defining the variables that can be used through out the playbook for dynamic configurations.
|
|---|
register
| This keyword helps in capturing the ouptut of the tasks and stores in a variable for later use in playbook.
|
|---|
notify
| This keyword used to trigger the handlers when specific conditions are matched such as service state changes.
|
|---|
Ansible Modules
In ansible modules are the reusable scripts that helps in carrying out specific tasks on remote systems such as managing the packages, files, services or executing commands. It helps in automating the infrastructure management efficiently, consistently at scale.
Ansible Modules
| Usage
|
|---|
apt
| This module manages the packages on Debian and Ubuntu Systems.
|
|---|
yum
| This module manages the packages on Red Hat/ Cent OS systems
|
|---|
copy
| This module helps in copying the files from local or Remote system to Destination system.
|
|---|
file
| This module manages the files and directories in local or Remote system.
|
|---|
service
| This module manages the services in the ansible
|
|---|
shell
| This module helps in executing shell commands on remote hosts.
|
|---|
template
| This module help in using the Jinja2 templates allowing dynamic content usage.
|
|---|
cron
| This module helps in managing the cron jobs, including the creation, modification and removal.
|
|---|
git
| This module helps in managing the repositories allowing tasks such as cloning, pulling and pushing.
|
|---|
Ansible Installation Commands
Installation of Ansible with commands varies slightly with respective operating systems. Every operating system comes with different package manager, so installation commands also keeps on varying. The following helps in finding installation command with respective operating system.
Operating System
| Commands
|
|---|
Ubuntu/Debian
| sudo apt-get install ansible
|
|---|
Red Hat/CentOS
| sudo yum install ansible
|
|---|
Using Pip software
| sudo pip install ansible
|
|---|
Ansible Playbook
- An Ansible playbook is a YAML file that contains a series of tasks that to be executed on remote hosts.
- Defining a task in playbook known as play and A playbook can have more than one play.
- The Execution of playbook provides the output in color-coded signifying the information such as
Color
| Description
|
|---|
Green
| Successfully Run and Configured
|
|---|
Cyan/Blue
| No Changess
|
|---|
Yellow/Orange
| Skipped
|
|---|
Red
| Error Raised
|
|---|
Ansible Playbook Command - Options
ansible-playbook -i inventory_path -u remote_user -k -f 3 -T 20 -t my_tag -M module_path myplaybook.yaml
- Here the options are detailed as follows:
Options
| Description
|
|---|
-i
| It used for specifying inventory path where the worker nodes information specified inventory file exists.
|
|---|
-u
| Used for specifying the user to connect over SSH (remotely) and run play.
|
|---|
-k
| It used for asking password which used for while SSH Authentication
|
|---|
-f
| It used for allocating forks that helps in number of tasks to execute parallel on remote hosts.
|
|---|
-T
| It used for setting time-out for remote connection establishment. It prevents in hanging the manager Node in Unreachable connections.
|
|---|
-t
| It is used to specify the tags, which helps to run the tasks that only contains this tags
|
|---|
-M
| It is used for defining module path that helps in loading local module.
|
|---|
-b
| It helps with running the play with elevated privileges ( uses become )
|
|---|
-K
| It used for prompting the enter password that required for escalation privilege (such as sudo password )
|
|---|
Ansible Adhoc Commands
Ansible Adhoc commands are the manual way of running the ansible commands for local or remote hosts configuration.
Functions
| Commands
|
|---|
Checking Connectivity To Hosts (Worker Nodes)
| ansible <group> -m ping
|
|---|
Executing The Command On Hosts
| ansible <group> -a "<command>"
|
|---|
Executing Shell Commands On Hosts
| ansible <group> -m shell -a "<command>"
|
|---|
Gathering Facts From All Hosts
| ansible all -m setup
|
|---|
Copying A File To All Hosts
| ansible all -m copy -a "src=file.txt dest=dest.txt"
|
|---|
Restarting A Service On All Hosts
| ansible all -m service -a "name=<service_name> state=restarted"
|
|---|
Ansible Playbook Commands
Ansible playbook commands are the one that used while running the playbook. On using options we can enhance extra functionality while executing the play run. The following tables illustrates some of functionality and its commands.
Functions
| Commands
|
|---|
Execute The Playbook
| ansible-playbook playbook.yml
|
|---|
Check The Playbook Syntax
| ansible-playbook playbook.yml --syntax-check
|
|---|
Check The Mode of Playbook ( Dry Run )
| ansible-playbook playbook.yml --check
|
|---|
Execute The Playbook Start At Task
| ansible-playbook playbook.yml --start-at-task <task_name>
|
|---|
Limit Playbook Execution To Specific Hosts
| ansible-playbook playbook.yml -l <host/group>
|
|---|
Skip Tasks With Specified Tags
| ansible-playbook playbook.yml --skip-tags <tag>
|
|---|
Ansible Vault Commands
The ansible vault commands are used for storing the senstive data instead of exposing them in playbooks directly.
Functions
| Ansible Vault Commands
|
|---|
Encrypt Vault File
| ansible-vault encrypt file.yml
|
|---|
Edit An Encrypted File
| ansible-vault edit file.yml
|
|---|
Decrypt Vault File
| ansible-vault decrypt file.yml
|
|---|
View Vault File
| ansible-vault view file.yml
|
|---|
Encrypt A String
| ansible-vault encrypt_string 'your_secret_string'
|
|---|
Rekey An Encrypted File
| ansible-vault rekey file.yml
|
|---|
Ansible Role Commands
Ansible Roles are an effective way of organizing and reusing the Ansible content. The following are the some effective ansible Role Commands explaining its functionality.
Functions
| Ansible Role Commands
|
|---|
Initialize The Role
| ansible-galaxy init role_name
|
|---|
Install The Role
| ansible-galaxy install author.role_name
|
|---|
Ansible Inventory Commands
Ansible inventory commands helps in providing the essential functionalities for managing and interacting with inventory.
Functions
| Ansible Inventory Commands
|
|---|
Listing Hosts
| ansible-inventory --list
|
|---|
Graphical View
| ansible-inventory --graph
|
|---|
Filter The Hosts By Pattern
| ansible all -i inventory.ini --pattern 'web*'
|
|---|
Updating Dynamic Inventory
| ansible-inventory --refresh
|
|---|
Ansible Connectivity Checking Commands
Ansible Connectivity Commands helps in checking the connectivity with hosts systems and accessibility of hosts in your inventory.
Functions
| Ansible Connectivity Commands
|
|---|
Ping The Hosts
| ansible all -m ping
|
|---|
Check SSH Connectivity
| ansible all -m ping -c paramiko
|
|---|
Verify The Connectivity To Specific Host
| ansible <hostname> -m ping
|
|---|
Ansible Galaxy Commands
Ansible Galaxy is a community hub for sharing Ansible roles, collections and other content. It act as a centralized repository where users are able to discover, download and contribute to Ansible content making easier of infrastructure automation.
Functions
| Ansible Galaxy Commands
|
|---|
Search Role
| ansible-galaxy search role_name
|
|---|
Install a Role
| ansible-galaxy install author.role_name
|
|---|
Listing Install Roles
| ansible-galaxy list
|
|---|
Creating A Requirements File For Roles
| ansible-galaxy install -r requirements.yml
|
|---|
Ansible Document Commands
Ansible Document Commands helps in knowing more information about particular modules and playbook such as its providing functionalities, syntax and options.
Functions
| Commands
|
|---|
To See The Documentation Of The Module
| ansible-doc module_name
|
|---|
To See Information On Playbooks
| ansible-doc playbook
|
|---|
Ansible Playbook Commands For Assign Variables
Ansible playbook commands for assigning variables enables the users to provide dynamic values to tasks and playbooks.
Functions
| Commands
|
|---|
Defining Variables In Playbook
| vars:
var_name: value
|
|---|
For Adding Variables To Playbooks
| ansible-playbook playbook.yml --extra-vars "var_name=value"
|
|---|
For Providing File Containing Variables
| ansible-playbook playbook.yml -e @vars.yml
|
|---|
Ansible Navigator Commands
Ansible Navigator provides a set of commands that support the navigation and exploration with ansible resources.
Functions
| Commands
|
|---|
List Available Actions
| ansible-navigator actions
|
|---|
Search Modules
| ansible-navigator search-modules <keyword>
|
|---|
Execute Playbooks
| ansible-navigator run playbook.yml
|
|---|
Viewing Inventory
| ansible-navigator inventory
|
|---|
Interacting With Ansible Collections
| ansible-navigator collection
|
|---|
Ansible Troubleshooting Commands
Ansible Troubleshooting commands in helps in better understanding of the issue and resolving them efficiently.
Functions
| Commands
|
|---|
To Increase The Verbosity Of Playbook Execution
| ansible-playbook playbook.yml -v
|
|---|
To Check Syntax Of Playbook
| ansible-playbook playbook.yml --syntax-check
|
|---|
Gathering Facts From Hosts
| ansible all -m setup
|
|---|
We came to the end of the article, in this Ansible Cheat Sheet we discussed all the essential commands and concepts that needed as a DevOps Professional to work effective with ansible tool. Try on keeping it handy for quick reference and streamline your configuration management tasks effortlessly.
People Also Read
|
|---|
Docker Cheat Sheet : Complete Guide (2024)
| Read
|
|---|
Kubectl Command Cheat Sheet
| Read
|
|---|
What Is Ansible
| Read
|
|---|
Ansible Cheat Sheet - FAQ's
What Is Ansible And Why To Use It?
Ansible is an open-sourcely available automation tool that is used for configuration management and application deployments. It helps in simplifying the tasks , enhancing the productivity and scalability of repetitive tasks.
How Do I Install Ansible On My System?
Install of ansible can be done so simple with one command but based on the operating system the command keep on varying with slightly with package manager. For Ubuntu/Debian system uses `sudo apt-get install ansible` and on Red Hat/CentOS systems uses `sudo yum install ansible` and alternatively you can install with using pip software also the command as follows `sudo pip install ansible`
How Can I Troubleshoot Ansible Tasks?
Troubleshooting of ansible can be done by increasing the verbosity with -v option, Checking the syntax of the yaml with --syntax-check and gathering the facts from the hosts with ansible all -m setup helps in resolving the issues during the playbook execution.
What Is The Diffference Between Ansible Ad-hoc Commands And Playbooks?
Ansible ad-hoc commands are used for quick on-off tasks, that can be executed directly from the command line. Whereas playbook are defined the configuration of tasks in yaml syntax code and will be helpful for more complex automation workflows.
What Are Ansible Playbooks And How Do They Work?
Ansible playbooks the files containing a series of tasks that to be executed on remote hosts. They define their desired state of the system. While the executing the playbook the defined tasks are executed sequentials ensuring the hosts system desired configurations.
How Can I Manage Sensitive Data In Ansible?
You can manage the sensitive data in ansible using ansible-vaults which is feature that helps in encrypting the secured data files. Example of encrypting file with ansible-vault is as follows:
ansible-vault encrypt file.yml
Similar Reads
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
GeeksforGeeks Master Sheet - List of all Cheat Sheets
In this Master Sheet, weâll cover all the important cheat sheets like SDE Sheets, DSA Topics Sheets, HTML, CSS, JavaScript, React, Angular, Bootstrap, jQuery, Python, Data Science, C, C++, Java, Computer Networks, Company Wise SDE sheets, etc. What is a Master Sheet ? A selection of comprehensive re
10 min read
Geeksforgeeks Cheatsheets - All Coding Cheat Sheets Collections
Cheatsheets are short documents that contain all the most essential information about a specific technology in short, such as its syntax, commands, functions, or its features. Sheets are designed to help users to learn quickly and these Sheets are given the basics Overview of the technology without
4 min read
Oracle Interview Experience | Set 32 (On-campus for Global Business Unit)
1st Round : Online Test (60 min) 4 sections : Aptitude, Software Skills, Computer Science Knowledge, Coding Skills Each sections has subsections. 2nd Round : Coding Round (2 questions 60 min) 1st Que :- Given a start time and end time in a digital format, you need to count the occurrence of each dig
4 min read
How to prepare for Infosys - The Complete guide
This article provides an insight of "how to prepare for infosys" from beginning to end. Question Paper Structure Online Test.( Duration â 95 Minutes.)Three Sections.Sectional Cut-off applied, No Negative marking.Sections are to be completed one by one, canât switch in between.Auto saved once stipula
4 min read
Microsoft Interview | Set 6
I am a guy from CSE dept, IIT-BHU and currently in 4th year. I am placed in microsoft now and all the credit goes to geeksforgeeks for sharpening and improving my coding skills. Thanks a lot guys for such a wonderful site. Below is my interview experience, if its of any help to my fellow job seekers
3 min read
Amazon Interview Experience | Set 149 (On-Campus for Internship)
Round 1 (Online): It had 20 MCQs and 2 coding questions for 90 minutes. Questions were from Operating Systems, Data structures, Aptitude etc. Most of them were exactly same as on https://www.geeksforgeeks.org/. In MCQs there was â 0.25 marking for every wrong answer and +1 for right answer. Coding
3 min read
Quiz Game in C
Quiz Games are one of the most interactive and mind blogging games of all time. Whether it be a child or teen or an adult, these Quiz Games fascinate people of all age groups. In this article, we will make a simple Quiz Game using C programming language. Functionalities of a Quiz GameThe Quiz Game p
5 min read
ServiceNow Interview Experience | Set 6 (On-campus)
Online round Solve 30 questions in 30 minutes. Multiple choice questions. Questions were based on exceptions, inheritance, recursion, bitwise operators, sorting, searching, time complexities, C and Java input/output questions, OS and around 5 aptitude questions. Most of the questions were basic and
4 min read
C++ Exercises - C++ Practice Set with Solutions
Do you want to improve your command on C++ language? Explore our vast library of C++ exercise questions, which are specifically designed for beginners as well as for advanced programmers. We provide a large selection of coding exercises that cover every important topic, including classes, objects, a
15+ min read