This document provides a comprehensive list of useful Ansible commands and their explanations. Use this cheat sheet to enhance your Ansible workflow and troubleshoot common issues.
ansible-doc ping
Displays the documentation for the "ping" Ansible module.
ansible --help
Provides help information for the Ansible command, listing available subcommands and options.
ansible-playbook --help
Provides help information for the ansible-playbook
command, listing available options and their descriptions.
ansible --list-host all
Lists all hosts in the inventory.
ansible --list-host dev
Lists all hosts in the "dev" group.
ansible --list-host webservers
Lists all hosts in the "webservers" group.
ansible --list-host \!webservers
Lists all hosts that are NOT in the "webservers" group.
ansible --list-host webserver:devservers
Lists all hosts that are in either the "webserver" or "devservers" group.
ansible-playbook playbook.yml --check
Runs the playbook in check mode to show what changes would be made without actually making any changes.
ansible-playbook playbook.yml --check --diff
Runs the playbook in check mode and also shows the differences that would be applied.
ansible-playbook playbook.yml -vvv
Runs the playbook with very verbose output for debugging purposes.
ansible-playbook playbook.yml -l node1
Limits the execution of the playbook to the "node1" host only.
ansible-playbook playbook.yml --list-hosts
Lists all hosts that would be targeted by the playbook.
ansible-playbook playbook.yml --list-tasks
Lists all tasks that would be executed by the playbook.
ansible-inventory --list
Lists all hosts and groups in the inventory.
ansible-inventory --graph
Displays a graph of the inventory showing groups and their member hosts.
ansible-doc -t inventory -l
Lists all available inventory plugins.
ansible-doc -t inventory aws_ec2
Displays the documentation for the aws_ec2
inventory plugin.
ansible-inventory -i inventory_aws_ec2.yml --list --yaml
Lists the dynamic inventory defined in inventory_aws_ec2.yml
in YAML format.
ansible all -m setup
Gathers facts from all hosts in the inventory.
ansible all -m gather_facts
Gathers facts from all hosts using the gather_facts
module.
ansible node3 -m setup | grep ansible_distribution_version
Gathers facts from "node3" and filters the output for the ansible_distribution_version
fact.
ansible node1:node2 -m setup | grep ansible_os_family
Gathers facts from "node1" and "node2" and filters the output for the ansible_os_family
fact.
ansible all -m setup --tree /tmp/facts
Displays facts from all hosts and stores them indexed by hostname at /tmp/facts
.
ansible-config dump
Dumps the current Ansible configuration settings.
ansible-config dump | grep ROLE
Dumps the current Ansible configuration settings and filters for ROLE
.
ansible-config list | grep -E 'HOST_KEY|true'
Lists the Ansible configuration settings and filters for HOST_KEY
or true
.
ansible-config view
Displays the contents of your ansible.cfg
file.
- import_playbook: ping.yml
- import_playbook: shell.yml
- import_playbook: configure.yml
This example shows how to import and run multiple playbooks in a single playbook file.