π Ansible Ad-hoc Commands: A Quick Guide
Ansible is a powerful automation tool that simplifies configuration management, application deployment, and task automation. While playbooks are commonly used for complex automation, Ad-hoc commands provide a quick way to execute tasks on remote systems without writing a playbook.
π What are Ansible Ad-hoc Commands?
Ad-hoc commands are one-time command-line executions using Ansible without creating a playbook. These commands are useful for running quick tasks, such as checking system uptime, managing packages, or restarting services.
π Syntax of an Ad-hoc Command
The basic syntax of an Ansible Ad-hoc command is:
ansible <host-pattern> -m <module> -a “<arguments>”
<host-pattern>
: Specifies the target host or group from the inventory.-m <module>
: Specifies the Ansible module to use.-a "<arguments>"
: Passes arguments to the module.
β‘ Common Ansible Ad-hoc Commands
β 1. Check Connectivity (Ping Module)
ansible all -m ping
This verifies if all managed hosts are reachable.
π₯ 2. Gather System Information (Setup Module)
ansible all -m setup
This collects system facts about all hosts.
πββοΈ 3. Execute Shell Commands (Command Module)
ansible all -m command -a "uptime"
This retrieves system uptime from all hosts.
π 4. Copy Files to Remote Hosts (Copy Module)
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts_backup"
This copies the local /etc/hosts file to /tmp/hosts_backup on all remote hosts.
π¦ 5. Manage Packages (Yum and Apt Modules)
π Install a Package (Yum for RHEL-based systems)
ansible all -m yum -a "name=git state=present"
π Install a Package (Apt for Debian-based systems)
ansible all -m apt -a "name=git state=present"
This installs the Git package on all target nodes.
π 6. Start or Restart a Service (Service Module)
ansible all -m service -a "name=httpd state=restarted"
This restarts the Apache HTTP server.
π€ 7. Create a User (User Module)
ansible all -m user -a "name=devops state=present"
This creates a user named devops on all hosts.
π 8. Change File Permissions (File Module)
ansible all -m file -a "path=/tmp/hosts_backup mode=0644"
This sets the file permission of /tmp/hosts_backup to 0644.
πΎ 9. Check Disk Usage (Shell Module)
ansible all -m shell -a "df -h"
This checks the disk usage on all target nodes.
π 10. Reboot Remote Hosts (Reboot Module)
ansible all -m reboot
This safely reboots all managed hosts.
β 11. Remove a Package (Yum and Apt Modules)
π Remove a Package on RHEL-based Systems
ansible all -m yum -a "name=git state=absent"
π Remove a Package on Debian-based Systems
ansible all -m apt -a "name=git state=absent"
This removes the Git package from all managed hosts.
π 12. Change Ownership of a File (File Module)
ansible all -m file -a "path=/tmp/hosts_backup owner=root group=root"
This changes the owner and group of /tmp/hosts_backup to root.
π When to Use Ansible Ad-hoc Commands?
β‘ Quick administrative tasks
π Troubleshooting and debugging
β³ One-time configuration changes
π Testing Ansible module functionalities before using them in playbooks
π― Conclusion
Ansible Ad-hoc commands are a fast and efficient way to execute tasks across multiple servers. While they are useful for quick operations, playbooks should be used for repeatable and structured automation.
π Want to master Ansible and DevOps? Join our comprehensive DevOps training program and take your automation skills to the next level! π― Join us today!
DevOps with AWS Training
Take your DevOps skills to the next level with our AWS training! Gain hands-on experience, real-world project exposure, and industry-recognized expertise. Enroll today and step into a high-demand career