ansible ad hoc commands
Ansible ad-hoc commands are one-line Ansible commands that can be used to perform quick and simple tasks on remote hosts without the need for a playbook or script. They are useful for tasks that need to be performed immediately and don't require a lot of customization.
Here are some examples of Ansible ad-hoc commands:
- Ping all hosts in the inventory:
ansible all -m ping
This command pings all the hosts in the inventory file to verify that Ansible can connect to them.
- Check the disk space usage on a host:
ansible host1 -m shell -a 'df -h'
This command runs the df -h
command on host1
and returns the disk space usage.
- Install a package on a group of hosts:
ansible web_servers -m apt -a 'name=nginx state=latest' --become
This command installs the latest version of the nginx package on all hosts in the web_servers
group using the apt
module. The --become
option is used to run the command with sudo privileges.
- Restart a service on a group of hosts:
ansible database_servers -m service -a 'name=mysql state=restarted' --become
This command restarts the mysql service on all hosts in the database_servers
group using the service
module. The --become
option is used to run the command with sudo privileges.
Ad-hoc commands can be very useful for quick and simple tasks, but for more complex tasks, it's recommended to use playbooks, which allow for more customization and reusability.