fbpx

πŸš€ 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

/5

πŸ“ Knowledge Check - Quiz

Test your understanding of Ansible Ad-hoc commands by answering the following multiple-choice questions. 🧠

1 / 5

How can you change the permissions of a file using Ansible Ad-hoc commands?

2 / 5

How can you install the nginx package on all remote hosts using Ansible?

3 / 5

What is the primary difference between Ansible Ad-hoc commands and playbooks?

4 / 5

What command would you use to reboot all managed hosts?

5 / 5

Which module would you use to check if a server is reachable?

Your score is

0%

🎯 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

Leave a Comment

Your email address will not be published. Required fields are marked *

Open chat
Hello, Good day!!
How can we help you?