Table of contents
1.
Introduction to Ansible 
2.
Ansible Ad hoc Commands
3.
Parallelism and Shell Commands
4.
File Transfer
4.1.
Transferring Files to Many Servers/Machines
4.2.
Creating New Directory
4.3.
Deleting the Whole Directory and Files
5.
Managing Packages
6.
Managing Services
7.
Managing Users and Groups
8.
Access All Facts
9.
Frequently Asked Questions
9.1.
What purposes does Ansible serve?
9.2.
Is Ansible a CI tool?
9.3.
What is an Ansible ad hoc command?
9.4.
How can I view hosts for Ansible?
9.5.
What commands does Ansible accept?
10.
Conclusion
Last Updated: Mar 27, 2024

Ansible Ad hoc Commands

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction to Ansible 

Several IT tools, such as intra-service orchestration, cloud provisioning, and application deployment, can be deployed automatically, thanks to the open-source Ansible IT engine. Deployment is straightforward due to the lack of agents and specialized security infrastructure.

Ansible

Ansible was developed with multi-tier deployment in mind. Ansible models IT architecture by specifying how all of your systems are interconnected rather than managing one system at a time. Since Ansible is entirely agentless, it connects your nodes using SSH to function (by default). However, Ansible allows you to use another connection technique, such as Kerberos.

Let's come to Ansible Ad hoc Commands!

Also check this out, ping command in linux

Ansible Ad hoc Commands

One of the easiest uses for Ansible is ad-hoc commands. You utilize these when you want to issue commands to a server or group of servers. Although the ad-hoc commands are not saved for later use, they offer a quick approach to communicating with the required servers.

Ansible Ad hoc Commands

The /usr/bin/ansible command-line tool is used by the Ansible ad-hoc command to automate a single task on one or more managed nodes. Although the Ad-hoc instructions are quick and straightforward, they cannot be reused. The Ad-hoc commands serve as a showcase for Ansible's ease of use and strength.

Syntax:

ansible <hosts> [-m <module_name>] -a <"arguments"> -u <username> [--become]


Explaining Syntax terms:

  • hosts: It might be a record in the inventory file. Use all or "*" to indicate every host in the inventory.
     
  • module_name: It is a choice-based parameter. Ansible offers hundreds of modules, including shell, yum, apt, file, and copy. It is the command by nature.
     
  • arguments: The values that the module requires should be given. It may vary depending on the module being utilized.
     
  • username: This identifies the user account Ansible can run commands under.
     
  • become: If we wish to do activities that require sudo privilege, we can use this optional parameter. It turns out to be false by default.

Parallelism and Shell Commands

The company's server can be rebooted simultaneously in 12 parallel forks. You must configure the SSH(secure socket shell) agent for connection to do this.

$ ssh-agent bash 
$ ssh-add ~/.ssh/id_rsa


Using 12 simultaneous forks, reboot all of the company's servers in the group "XYZ" as follows:

$ Ansible XYZ -a "/sbin/reboot" -f 12


Ansible will automatically execute the aforementioned ad-hoc commands under the current user account. To alter this behavior, you must pass the username in ad-hoc commands in the manner described below.

$ Ansible xyz -a "/sbin/reboot" -f 12 -u username

File Transfer

For SCP (secure copy protocol), which transfers several files simultaneously across many servers or workstations, you can utilize ad-hoc instructions.

File transfer

Transferring Files to Many Servers/Machines

$ Ansible Xyz -m copy -a "src = /etc/yum.conf dest = /tmp/yum.conf"

Creating New Directory

$ Ansible Xyz -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory" 

Deleting the Whole Directory and Files

$ Ansible Xyz -m file -a "dest = /path/user1/new state = absent"

Managing Packages

Yum(Red Hat Enterprise Linux uses YUM as its main package management tool for setting up, deleting, and managing software packages) and apt(APT, also referred to as the Advanced Package Tool, is a group of tools that can be used to install, update, uninstall, and manage software packages on Debian and its derived operating systems, such as Ubuntu and Linux Mint) both support the Ad-hoc commands. The following list of ad-hoc yum commands.

Managing Packages

The following command does not update the yum package; it only determines whether it is installed or not.

$ Ansible Xyz -m yum -a "name = demo-tomcat-1 state = present"


The following command checks the package is not installed.

$ Ansible Xyz -m yum -a "name = demo-tomcat-1 state = absent" 


The command checks the latest version of the package is installed.

$ Ansible Xyz -m yum -a "name = demo-tomcat-1 state = latest"

Managing Services

Make sure the service is running on each web server.

$ ansible webservers -m service -a "name=httpd state=started"  


Alternatively, you might restart a service on each web server.

$ ansible webservers -m service -a "name=httpd state=restarted"  


Make sure a service is terminated:

$ ansible webservers -m service -a "name=httpd state=stopped"  

Managing Users and Groups

Ad-hoc commands let you manage, add, and delete user accounts on your managed nodes.

$ ansible all -m user -a "name=foo password=<crypted password here>"  
$ ansible all -m user -a "name=foo state=absent"  

Access All Facts

The implementation of conditional statements in a playbook can be done using facts. The following Ad-hoc command will allow you to access all of your facts' Ad-hoc information:

$ Ansible all -m setup

Frequently Asked Questions

What purposes does Ansible serve?

Ansible can be used to provision bare metal servers, virtualized hosts and hypervisors, network devices, and your environment's underlying infrastructure.

Is Ansible a CI tool?

A CI/CD(continuous integration and continuous delivery/continuous deployment) process can leverage Ansible, a potent tool for IT automation, to configure the target environment before deploying the application on it.

What is an Ansible ad hoc command?

Ansible ad hoc commands automate a single task on one or more managed nodes using the /usr/bin/ansible command-line tool. Although they are quick and simple, ad hoc commands are not reusable.

How can I view hosts for Ansible?

The --list-hosts option is available. All of the host IPs from your inventory file will be displayed.

What commands does Ansible accept?

To execute any scripts or commands on the remote target system, use the Ansible all command.

Conclusion

This article discussed the Ansible Ad hoc Commands, what is Ansible and Ad hoc Commands, Parallelism And Shell Commands file transfer, etc. You can also see other suggested articles to learn more about Ansible,

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass