Ansible
Ansible Commands Cheat Sheet
Practical Ansible commands for inventory management, connectivity, ad-hoc commands, playbooks and troubleshooting.
Essential Ansible Commands
ansible --versionCheck the installed Ansible version.
ansible-inventory -i inventory.ini --listDisplay the inventory in JSON format.
ansible all -i inventory.ini -m pingTest connectivity to all inventory hosts.
ansible servers -i inventory.ini -m pingTest connectivity to hosts in the servers group.
ansible servers -i inventory.ini -m command -a 'uptime'Run a command on remote servers.
ansible servers -i inventory.ini -m shell -a 'df -h'Execute a shell command on remote servers.
ansible servers -i inventory.ini -m setupCollect system facts from remote hosts.
ansible-playbook -i inventory.ini site.ymlRun an Ansible playbook.
ansible-playbook --syntax-check site.ymlCheck a playbook for syntax errors without executing it.
ansible-playbook --check site.ymlRun a playbook in check mode to preview changes.
ansible-playbook -i inventory.ini site.yml -vRun a playbook with verbose output.
ansible servers -i inventory.ini -m shell -a 'free -h'Check memory usage on remote servers.
ansible servers -i inventory.ini -m shell -a 'df -h /'Check root filesystem disk usage.
ansible-galaxy collection listList installed Ansible collections.
Ansible Troubleshooting Workflow
# Check Ansible installation ansible --version # Check inventory ansible-inventory -i inventory.ini --list # Test connectivity ansible all -i inventory.ini -m ping # Check server facts ansible servers -i inventory.ini -m setup # Test disk usage ansible servers -i inventory.ini -m shell -a "df -h" # Check playbook syntax ansible-playbook --syntax-check site.yml # Preview playbook changes ansible-playbook --check site.yml
DevOpsToolbox Tip
When troubleshooting Ansible, first verify the inventory and SSH connectivity with theping module. If connectivity works, check facts, permissions and the playbook syntax before investigating the individual task.