Ansible Cheatsheet
#
Ansible is an open-source automation tool used for IT tasks such as configuration management, application deployment, and orchestration. It uses a simple, human-readable language (YAML) to describe automation jobs, which allows IT administrators to manage systems without needing to understand complex programming languages.
Ansible Basics
#
Command/Option | Example | Description |
---|
ansible –version | ansible --version | Display the version of Ansible installed |
ansible -m ping | ansible all -m ping | Ping all hosts in the inventory to check connectivity |
Inventory Management
#
Command/Option | Example | Description |
---|
ansible-inventory –list | ansible-inventory --list | List all hosts in the inventory |
ansible-inventory –graph | ansible-inventory --graph | Display inventory as a graph |
Running Ad-hoc Commands
#
Command/Option | Example | Description |
---|
ansible -m -a | ansible webservers -m yum -a "name=httpd state=present" | Run a module with arguments on a host or group of hosts |
ansible webservers -a “/usr/bin/uptime” | ansible webservers -a "/usr/bin/uptime" | Run a command on a group of hosts |
Playbook Management
#
Command/Option | Example | Description |
---|
ansible-playbook <playbook.yml> | ansible-playbook site.yml | Run a playbook |
ansible-playbook <playbook.yml> –check | ansible-playbook site.yml --check | Perform a dry run of a playbook |
ansible-playbook <playbook.yml> –diff | ansible-playbook site.yml --diff | Show differences between current and configured states |
Roles and Galaxy
#
Command/Option | Example | Description |
---|
ansible-galaxy install | ansible-galaxy install username.role_name | Install a role from Ansible Galaxy |
ansible-galaxy list | ansible-galaxy list | List installed roles |
ansible-galaxy init <role_name> | ansible-galaxy init my_role | Create a new role skeleton |
Vault
#
Command/Option | Example | Description |
---|
ansible-vault create | ansible-vault create secrets.yml | Create a new encrypted file |
ansible-vault edit | ansible-vault edit secrets.yml | Edit an encrypted file |
ansible-vault view | ansible-vault view secrets.yml | View an encrypted file |
ansible-vault encrypt | ansible-vault encrypt secrets.yml | Encrypt a file |
ansible-vault decrypt | ansible-vault decrypt secrets.yml | Decrypt a file |
Configuration Management
#
Command/Option | Example | Description |
---|
ansible-config list | ansible-config list | List all configuration options |
ansible-config dump | ansible-config dump | Display the current configuration |
Inventory and Hosts
#
Command/Option | Example | Description |
---|
ansible -m setup | ansible all -m setup | Gather facts about remote hosts |
ansible-playbook -i <playbook.yml> | ansible-playbook -i inventory.ini site.yml | Run a playbook with a specified inventory file |
Miscellaneous
#
Command/Option | Example | Description |
---|
ansible-doc | ansible-doc yum | Show documentation for a module |
ansible-pull -U | ansible-pull -U https://github.com/user/repo.git | Pull and apply a playbook from a VCS repository |
This cheatsheet covers the most commonly used Ansible commands and options, helping you to manage inventories, run ad-hoc commands, execute playbooks, manage roles and vaults, and configure settings effectively.