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?
- 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
- Node:- A system where Ansible is installed and configured to connect to other systems.
- Inventory File:- A file that contains information about the servers Ansible controls, typically located at /etc/ansible/hosts
- Playbook:- A YML file containing a series of tasks to be executed on a remote server.
- Role:- A collection of playbooks and other files that are relevant to a goal such as installing a web server.
- Play:- A play can have several playbooks and roles, included from a single playbook that acts as an entry point.
- Check all hosts are able to connect by ansible:- ansible all -m ping
- Check all hosts with a particular user is able to connect by ansible:-ansible all -m ping -u svastikkka
- Check all hosts are able to connect by Ansible with custom ssh:- ansible all -m ping --private-key=~/.ssh/custom_id
- Check all hosts are able to connect by Ansible with custom password-based authentication:- ansible all -m ping --ask-pass
- If the user needs to provide a password in order to run sudo commands:- ansible all -m ping --ask-become-pass
- To point a Custom Inventory File:- ansible all -m ping -i MY_CUSTOM_INVENTORY
with ansible-playbook
- Check all hosts with a particular user is able to connect by ansible:- ansible-playbook MYPLAYBOOK.yml -u svastikkka
- Check all hosts are able to connect by Ansible with custom ssh:- ansible-playbook MYPLAYBOOK.yml --private-key=~/.ssh/custom_id
- Check all hosts are able to connect by Ansible with custom password-based authentication:- ansible-playbook MYPLAYBOOK.yml --ask-pass
- If the user needs to provide a password in order to run sudo commands:- ansible-playbook myplaybook.yml --ask-become-pass
- To point a Custom Inventory File:- ansible-playbook MYPLAYBOOK.yml -i MY_CUSTOM_INVENTORY
- Try to run uname -a command:- ansible all -a "uname -a"
- Try to run a command that installs the package vim on server1 from your inventory:- ansible server1 -m apt -a "name=vim
- Try to dry run to predict how the systems would be affected by your command:- ansible server1 -m apt -a "name=vim" --check
- To run a playbook and execute all the tasks defined within it, use the ansible-playbook command:- ansible-playbook MYPLAYBOOK.yml
- 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
- 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
- List all hosts that would be affected by a play, without running any tasks on the remote servers:- ansible-playbook MYPLAYBOOK.yml --list-hosts
- To list all tags available in a play, use the option --list-tags:- ansible-playbook myplaybook.yml --list-tags
- 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.
- Requires a valid task name as argument:- ansible-playbook MYPLAYBOOK.yml --start-at-task="Set Up Nginx"
- To only execute tasks associated with specific tags:- ansible-playbook MYPLAYBOOK.yml --tags=mysql,nginx
- If you want to skip all tasks that are under specific tags:- ansible-playbook MYPLAYBOOK.yml --skip-tags=mysql
Comments
Post a Comment
Please give us your valuable feedback