Ansible automates servers over SSH with no agent. Commands take an inventory (list of hosts) with -i. Always test with --check --diff first.
Click any command to copy it.
| Command | What it does |
|---|
ansible all -i inventory.ini -m ping | Test connectivity and Python on all hosts. |
ansible web -i inventory.ini -a "uptime" | Run a command on the web group. |
ansible web -i inventory.ini -b -m apt -a "name=nginx state=present update_cache=yes" | Install a package with sudo (become). |
ansible web -i inventory.ini -b -m service -a "name=nginx state=restarted" | Restart a service. |
ansible all -i inventory.ini -m setup -a 'filter=ansible_distribution*' | Show OS facts. |
ansible all -i inventory.ini -m copy -a 'src=file dest=/tmp/file' | Copy a file to every host. |
| Command | What it does |
|---|
ansible-playbook -i inventory.ini site.yml | Run a playbook. |
ansible-playbook -i inventory.ini site.yml --check --diff | Dry run showing what would change. |
ansible-playbook site.yml --limit web1 | Run only on one host or group. |
ansible-playbook site.yml --tags nginx | Run only tasks with a tag. |
ansible-playbook site.yml -e "version=1.2.3" | Pass an extra variable. |
ansible-playbook site.yml --syntax-check | Check the playbook for syntax errors. |
ansible-playbook site.yml --list-tasks | List tasks without running them. |
ansible-playbook site.yml -vvv | Very verbose output for debugging. |
ansible-playbook site.yml --start-at-task 'Task name' | Resume from a specific task. |
| Command | What it does |
|---|
ansible-vault create secrets.yml | Create an encrypted file. |
ansible-vault edit secrets.yml | Edit an encrypted file. |
ansible-vault encrypt vars.yml | Encrypt an existing file. |
ansible-vault decrypt vars.yml | Destructive: Decrypt a file in place, leaving secrets in plain text on disk. |
ansible-vault view secrets.yml | Show the contents without saving them. |
ansible-playbook site.yml --ask-vault-pass | Prompt for the vault password when running. |