Ansible CLI cheatsheet

Hey everyone, I would like to share some of the basic commands of ansible as I am practising ansible. Well, it is another helpful tool that describes how machines should be configured or what actions should be taken on them (systems). 

What is ansible?
Ansible is 
  • Cross-platform configuration management tool
  • An open-source software
  • Application deployment tool
  • An intra-service orchestration tool
Basically, it is a radically simple IT automation engine.
Introduction
This cheat sheet-style guide provides a quick reference to CLI commands which are commonly used.
Glossary
  1. Node:- A system where Ansible is installed and configured to connect to other systems.
  2. Inventory File:-  A file that contains information about the servers Ansible controls, typically located at /etc/ansible/hosts
  3. Playbook:- A YML file containing a series of tasks to be executed on a remote server.
  4. Role:- A collection of playbooks and other files that are relevant to a goal such as installing a web server.
  5. Play:- A play can have several playbooks and roles, included from a single playbook that acts as an entry point.
Testing Connectivity to Nodes
The ping module will test if you have valid credentials for connecting to the nodes defined in your inventory file
  1. Check all hosts are able to connect by ansible:- ansible all -m ping
  2. Check all hosts with a particular user is able to connect by ansible:-ansible all -m ping -u svastikkka
  3. Check all hosts are able to connect by Ansible with custom ssh:-  ansible all -m ping --private-key=~/.ssh/custom_id
  4. Check all hosts are able to connect by Ansible with custom password-based authentication:- ansible all -m ping --ask-pass
  5. If the user needs to provide a password in order to run sudo commands:- ansible all -m ping --ask-become-pass
  6. To point a Custom Inventory File:- ansible all -m ping -i MY_CUSTOM_INVENTORY
with ansible-playbook
  1. Check all hosts with a particular user is able to connect by ansible:- ansible-playbook MYPLAYBOOK.yml -u svastikkka
  2. Check all hosts are able to connect by Ansible with custom ssh:-  ansible-playbook MYPLAYBOOK.yml --private-key=~/.ssh/custom_id
  3. Check all hosts are able to connect by Ansible with custom password-based authentication:- ansible-playbook MYPLAYBOOK.yml --ask-pass
  4. If the user needs to provide a password in order to run sudo commands:- ansible-playbook myplaybook.yml --ask-become-pass
  5. To point a Custom Inventory File:- ansible-playbook MYPLAYBOOK.yml -i MY_CUSTOM_INVENTORY
Running ad-hoc Commands
To execute a command on a node, use the -a option followed by the command you want to run in double-quotes.
  1. Try to run uname -a command:- ansible all -a "uname -a"
  2. Try to run a command that installs the package vim on server1 from your inventory:- ansible server1 -m apt -a "name=vim
  3. Try to dry run to predict how the systems would be affected by your command:- ansible server1 -m apt -a "name=vim" --check
Running Playbooks
  1. To run a playbook and execute all the tasks defined within it, use the ansible-playbook command:- ansible-playbook MYPLAYBOOK.yml
  2. To overwrite the default hosts option in the playbook and limit execution to a certain group or host, include the option -l in your command: ansible-playbook -l server1 MYPLAYBOOK.yml
  3. Getting information about a Play (list all tasks that would be executed by a play without making any changes to the remote servers):- ansible-playbook MYPLAYBOOK.yml --list-tasks
  4. List all hosts that would be affected by a play, without running any tasks on the remote servers:- ansible-playbook MYPLAYBOOK.yml --list-hosts
  5. To list all tags available in a play, use the option --list-tags:- ansible-playbook myplaybook.yml --list-tags
  6. To define a new entry point for your playbook:- Ansible will then skip anything that comes before the specified task, executing the remaining of the play from that point on.
    1. Requires a valid task name as argument:- ansible-playbook MYPLAYBOOK.yml --start-at-task="Set Up Nginx"
  7. To only execute tasks associated with specific tags:- ansible-playbook MYPLAYBOOK.yml --tags=mysql,nginx
  8. If you want to skip all tasks that are under specific tags:- ansible-playbook MYPLAYBOOK.yml --skip-tags=mysql

Comments

Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius