Ansible ad-hoc commands
Common Use Cases and Examples
1. Ping Hosts
Check if the hosts are reachable.
ansible all -m ping
2. Gather Facts
Collect information about the remote hosts.
ansible all -m gather_facts
3. Run Shell Commands
Execute shell commands on remote hosts.
ansible all -m shell -a 'uptime'
ansible all -m command -a 'uptime'
4. Copy Files
Copy files from the local machine to remote hosts.
ansible all -m copy -a 'src=/path/to/local/file dest=/path/to/remote/destination'
5. Manage Packages
Install, update, or remove packages on remote hosts.
ansible all -m apt -a 'name=htop state=present' -b
6. Manage Services
Start, stop, or restart services on remote hosts.
ansible all -m service -a 'name=nginx state=started' -b
7. Create Users
Add a new user on remote hosts.
ansible all -m user -a 'name=Manasee state=present' -b
8. Modify File Permissions
Change the permissions of files or directories on remote hosts.
ansible all -m file -a 'path=/path/to/file mode=0755' -b
9. Target Specific Hosts
ansible webservers -m ping
10. Use Patterns
ansible 'webservers: !dbservers' -m ping