Junior — Middle
Explain what is meant by Ad-hoc commands in Ansible and how they are used.
sobes.tech AI
Answer from AI
Ad-hoc commands in Ansible are one-time commands that are executed directly from the command line without writing playbooks. They are used for quickly performing simple tasks on remote hosts, such as copying files, installing packages, restarting services.
Example of using an ad-hoc command to install the nginx package on a group of webservers:
ansible webservers -m apt -a "name=nginx state=present" -b
Here:
webservers— the group of hosts from the inventory-m apt— Ansible module for managing packages in Debian/Ubuntu-a— module arguments-b— run with elevated privileges (sudo)
Ad-hoc commands are convenient for quick administration and debugging, but for repetitive and complex tasks, it is better to use playbooks.