Ansible is a powerful automation tool used for configuration management, application deployment, and task automation. It simplifies complex tasks and ensures consistency across environments.
Whether you’re a beginner or an experienced user, this Ansible cheat sheet will help you streamline your automation workflows.
Essential Ansible Commands
1. Inventory Management
Ansible uses an inventory file to define the hosts and groups of hosts on which commands and playbooks will run.
| Command/File | Description |
|---|---|
/etc/ansible/hosts | Default location for the inventory file. |
ansible-inventory --list | Lists all hosts and groups in the inventory. |
ansible-inventory --graph | Displays a visual representation of the inventory. |
[group-name] | Defines a group of hosts in the inventory file. |
[group-name:vars] | Defines variables for a specific group. |
Example Inventory File:
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
[webservers:vars]
ansible_user=admin
ansible_ssh_private_key_file=/path/to/key.pemCode language: JavaScript (javascript)
2. Ad-Hoc Commands
Ad-hoc commands are used to execute quick tasks on remote hosts without writing a playbook.
| Command | Description |
|---|---|
ansible <group> -m <module> -a "<arguments>" | Runs a module on a group of hosts. |
ansible all -m ping | Pings all hosts in the inventory. |
ansible webservers -m shell -a "uptime" | Runs the uptime command on all webservers. |
ansible dbservers -m yum -a "name=httpd state=present" | Installs the httpd package on all dbservers. |
ansible all -m copy -a "src=/file.txt dest=/tmp/file.txt" | Copies a file to all hosts. |
3. Playbook Execution
Playbooks are YAML files that define automation tasks.
| Command | Description |
|---|---|
ansible-playbook <playbook.yml> | Runs a playbook. |
ansible-playbook <playbook.yml> --limit <group> | Runs a playbook on a specific group of hosts. |
ansible-playbook <playbook.yml> --start-at-task="task-name" | Starts a playbook at a specific task. |
ansible-playbook <playbook.yml> --check | Performs a dry run without making changes. |
ansible-playbook <playbook.yml> --tags <tag-name> | Runs only tasks with a specific tag. |
Example Playbook:
- name: Install and start Apache
hosts: webservers
become: yes
tasks:
- name: Install Apache
yum:
name: httpd
state: present
- name: Start Apache service
service:
name: httpd
state: started
4. Modules
Modules are the building blocks of Ansible, used to perform specific tasks.
| Module | Description |
|---|---|
yum | Manages packages on Red Hat-based systems. |
apt | Manages packages on Debian-based systems. |
service | Manages services. |
copy | Copies files to remote hosts. |
file | Manages files and directories. |
shell | Executes shell commands. |
template | Renders a template file and copies it to the remote host. |
user | Manages user accounts. |
debug | Prints debug messages during playbook execution. |
5. Variables and Facts
Variables allow you to customize playbooks, and facts provide information about remote hosts.
| Command | Description |
|---|---|
ansible <host> -m setup | Displays all facts about a host. |
{{ variable_name }} | Syntax to use variables in playbooks. |
vars: | Defines variables in a playbook. |
hostvars | Accesses variables from other hosts. |
Example:
- name: Use variables
hosts: all
vars:
app_port: 8080
tasks:
- name: Print variable value
debug:
msg: "The app port is {{ app_port }}"Code language: JavaScript (javascript)
6. Conditionals and Loops
Conditionals and loops allow you to control task execution and repeat tasks.
| Syntax | Description |
|---|---|
when: <condition> | Executes a task only if the condition is true. |
with_items: <list> | Iterates over a list of items. |
loop: | Modern alternative to with_items. |
Example:
- name: Install multiple packages
hosts: all
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop:
- httpd
- mariadb
- phpCode language: JavaScript (javascript)
7. Roles
Roles are reusable units of automation that organize playbooks and tasks.
| Command | Description |
|---|---|
ansible-galaxy init <role-name> | Creates a new role structure. |
roles/ | Directory where roles are stored. |
ansible-galaxy install <role-name> | Installs a role from Ansible Galaxy. |
Example Role Structure:
roles/
common/
tasks/
handlers/
templates/
files/
vars/
defaults/
meta/
8. Handlers
Handlers are tasks that run only when notified by other tasks.
| Syntax | Description |
|---|---|
notify: <handler-name> | Triggers a handler after a task. |
handlers: | Defines handlers in a playbook. |
Example:
- name: Restart Apache
hosts: webservers
tasks:
- name: Install Apache
yum:
name: httpd
state: present
notify: Restart Apache service
handlers:
- name: Restart Apache service
service:
name: httpd
state: restarted
Common Ansible Challenges and Solutions
| Challenge | Solution |
|---|---|
| Permission denied errors | Use become: yes to escalate privileges. |
| Slow playbook execution | Use strategy: free in the playbook to speed up execution. |
| Undefined variables | Ensure variables are defined in the inventory, playbook, or role. |
| Idempotency issues | Use modules that ensure idempotency (e.g., yum, apt). |
| Debugging playbooks | Use the debug module to print variable values and task outputs. |
FAQs About Ansible
ansible-playbook and ansible? ansible is used for ad-hoc commands.ansible-playbook is used to execute playbooks.
Use the command:ansible-playbook <playbook.yml> --syntax-check
Yes, Ansible supports Windows using the win_* modules (e.g., win_package, win_service).
Use Ansible Vault to encrypt sensitive data:ansible-vault create <file.yml>
ansible-vault edit <file.yml>
ansible-playbook <playbook.yml> --ask-vault-pass
Pro Tips for Using Ansible
- Use Roles for Reusability: Organize your playbooks into roles for better maintainability.
- Leverage Tags: Use tags to run specific tasks or groups of tasks.
- Test Playbooks: Use
--checkand--diffto test playbooks before execution. - Use Ansible Galaxy: Explore and reuse roles from Ansible Galaxy to save time.
- Document Your Playbooks: Add comments and descriptions to make playbooks easier to understand.