Ansible Commands
Essential Ansible commands and playbook examples for automation.
Basic Commands
ansible --versionDisplay Ansible version
ansible all -m pingPing all hosts
ansible all -a "uptime"Run command on all hosts
ansible all -m setupGather facts from all hosts
ansible all -m setup -a "filter=ansible_distribution*"Gather specific facts
ansible-config dumpShow all configuration settings
ansible-config viewView current config file
ansible-inventory --listList all inventory hosts
ansible-inventory --graphDisplay inventory as graph
Playbook Commands
ansible-playbook playbook.ymlRun a playbook
ansible-playbook playbook.yml --checkDry run (check mode)
ansible-playbook playbook.yml --diffShow file differences
ansible-playbook playbook.yml -vVerbose output
ansible-playbook playbook.yml -vvvMore verbose output
ansible-playbook playbook.yml --syntax-checkCheck playbook syntax
ansible-playbook playbook.yml --list-tasksList all tasks
ansible-playbook playbook.yml --list-hostsList affected hosts
ansible-playbook playbook.yml --tags "config"Run only specific tags
ansible-playbook playbook.yml --skip-tags "deploy"Skip specific tags
ansible-playbook playbook.yml --start-at-task "task name"Start at specific task
ansible-playbook playbook.yml --stepStep-by-step execution
Inventory Management
ansible all -i inventory.ini --list-hostsList hosts from inventory
ansible-inventory -i inventory.ini --listShow inventory in JSON
ansible-inventory -i inventory.ini --graphShow inventory graph
ansible webservers -i inventory.ini -m pingPing specific group
ansible all --limit "host1,host2" -m pingLimit to specific hosts
ansible all --limit @retry_hosts.txt -m pingLimit to hosts from file
Ad-Hoc Commands
ansible all -m shell -a "df -h"Run shell command
ansible all -m copy -a "src=/file dest=/tmp/file"Copy file to hosts
ansible all -m file -a "path=/tmp/file state=absent"Remove file
ansible all -m service -a "name=nginx state=restarted"Restart service
ansible all -m apt -a "name=nginx state=latest" -bInstall package with sudo
ansible all -m user -a "name=john state=present"Create user
ansible all -m git -a "repo=https://github.com/user/repo dest=/opt/app"Clone git repo
ansible all -m systemd -a "name=nginx enabled=yes state=started"Manage systemd service
Privilege Escalation
ansible all -m shell -a "whoami" -bRun with sudo (become)
ansible all -m shell -a "whoami" -b --become-user=adminBecome specific user
ansible all -m shell -a "whoami" -b --become-method=suUse su instead of sudo
ansible all -m shell -a "whoami" -b -KPrompt for become password
ansible-playbook playbook.yml -b -KRun playbook with become and password
Variables & Extra Vars
ansible-playbook playbook.yml -e "version=1.0"Pass extra variable
ansible-playbook playbook.yml -e "@vars.json"Load variables from JSON file
ansible-playbook playbook.yml -e "@vars.yml"Load variables from YAML file
ansible-playbook playbook.yml -e "env=production debug=false"Pass multiple extra variables
Vault (Encryption)
ansible-vault create secrets.ymlCreate encrypted file
ansible-vault edit secrets.ymlEdit encrypted file
ansible-vault view secrets.ymlView encrypted file
ansible-vault encrypt file.ymlEncrypt existing file
ansible-vault decrypt file.ymlDecrypt file
ansible-vault rekey secrets.ymlChange vault password
ansible-playbook playbook.yml --ask-vault-passRun playbook with vault password
ansible-playbook playbook.yml --vault-password-file .vault_passUse vault password file
ansible-vault encrypt_string "secret" --name "db_password"Encrypt string value
Galaxy (Roles)
ansible-galaxy init myroleCreate new role structure
ansible-galaxy install geerlingguy.nginxInstall role from Galaxy
ansible-galaxy install -r requirements.ymlInstall roles from requirements file
ansible-galaxy listList installed roles
ansible-galaxy remove geerlingguy.nginxRemove installed role
ansible-galaxy search nginxSearch for roles
ansible-galaxy collection install community.generalInstall collection
ansible-galaxy collection listList installed collections
Connection Options
ansible all -m ping -u usernameConnect as specific user
ansible all -m ping -kPrompt for SSH password
ansible all -m ping --private-key ~/.ssh/id_rsaUse specific SSH key
ansible all -m ping -c localUse local connection
ansible all -m ping -c paramikoUse Paramiko connection
ansible all -m ping -T 30Set connection timeout (seconds)
Debugging
ansible-playbook playbook.yml -vvvMaximum verbosity
ansible-playbook playbook.yml --stepInteractive step mode
ansible-playbook playbook.yml --start-at-task "task name"Start at specific task
ANSIBLE_DEBUG=1 ansible-playbook playbook.ymlEnable debug output
ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook playbook.ymlKeep remote temp files
ansible-playbook playbook.yml --check --diffDry run with differences
Performance & Optimization
ansible-playbook playbook.yml -f 10Set parallelism (forks) to 10
ANSIBLE_PIPELINING=1 ansible-playbook playbook.ymlEnable SSH pipelining
ansible-playbook playbook.yml --forks 50Run on 50 hosts in parallel
ansible all -m setup --tree /tmp/factsCache facts to directory
Common Playbook Examples
---
- hosts: all
tasks:
- name: Ping
ping:Simple ping playbook
- name: Install nginx
apt:
name: nginx
state: presentInstall package task
- name: Copy config
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
notify: restart nginxCopy file with handler
- name: Ensure service
service:
name: nginx
state: started
enabled: yesManage service task