DevTools

Ansible CLI

Ansible commands for IT automation and configuration management.

35 commands
Windows MacOS Linux
#automation #configuration-management

Ad-hoc Commands

Ping all hosts in inventory
ansible all -m ping
Run a shell command on a group
ansible webservers -m shell -a 'uptime'
Copy a file to all hosts
ansible all -m copy -a 'src=file.txt dest=/tmp/file.txt'
Install a package with sudo
ansible all -m apt -a 'name=nginx state=present' -b
Restart a service on all hosts
ansible all -m service -a 'name=nginx state=restarted' -b
Gather facts from all hosts
ansible all -m setup

Playbook Execution

Run a playbook
ansible-playbook site.yml
Run with a specific inventory file
ansible-playbook site.yml -i inventory.ini
Run on a subset of hosts
ansible-playbook site.yml --limit webservers
Run only tasks with specific tags
ansible-playbook site.yml --tags deploy
Skip tasks with specific tags
ansible-playbook site.yml --skip-tags test
Dry run without making changes
ansible-playbook site.yml --check
Pass extra variables to playbook
ansible-playbook site.yml -e 'env=production'

Inventory

Display full inventory as JSON
ansible-inventory --list -i inventory.ini
Show inventory as a tree graph
ansible-inventory --graph
List all hosts in inventory
ansible all --list-hosts
Show variables for a specific host
ansible-inventory --host myserver

Vault

Create a new encrypted file
ansible-vault create secrets.yml
Edit an encrypted file in place
ansible-vault edit secrets.yml
Encrypt an existing file
ansible-vault encrypt vars.yml
Decrypt an encrypted file
ansible-vault decrypt secrets.yml
Change the vault password
ansible-vault rekey secrets.yml
Run playbook prompting for vault password
ansible-playbook site.yml --ask-vault-pass

Galaxy

Create a new role skeleton
ansible-galaxy init myrole
Install a role from Galaxy
ansible-galaxy install geerlingguy.docker
Install a collection from Galaxy
ansible-galaxy collection install community.general
List installed roles
ansible-galaxy list
Remove an installed role
ansible-galaxy role remove geerlingguy.docker

Configuration

Show Ansible version and config path
ansible --version
Show non-default configuration values
ansible-config dump --only-changed
List all available configuration options
ansible-config list
Debug facts for localhost
ansible localhost -m debug -a 'var=ansible_facts'

Quick Commands

Test connectivity to all hosts in inventory
ansible all -m ping
Execute an Ansible playbook
ansible-playbook site.yml
Encrypt a file with Ansible Vault
ansible-vault encrypt secrets.yml