Introduction to ad hoc commands

Ad hoc commands in Ansible are used to perform tasks or operations that are needed on an ad hoc basis or only once based upon the requirement. In other words, these are tasks that a user wants to be performed on the fly but doesn’t want to be saved for later use. A quick example of an use case for Ansible ad hoc commands could be to quickly fetch the version information of the group of managed nodes for some other use as a one time task. As this is a quick information need and does not need to be repeated, we would use an ad hoc task to perform this request.

As we proceed with the chapter, there will be some additional switches (extra options that we pass to Ansible commands), that would be introduced based upon the requirements. Invoking the ansible command only will produce all the values that can be passed as options or parameters:

Some examples of ad-hoc commands are as follows:

  1.  Let us say we need to ping the same set of devices, but now in parallel (the default is sequential but to make tasks faster, we would use parallelism in our approach):
ansible myrouters -m ping -f 5
  1. If we want use a separate username instead of the default configured one:
ansible myrouters -m ping -f 5 -u <username>
  1. If we want to enhance the session (or use sudo or root):
ansible myrouters -m ping -f 5 -u username --become -k (-k will ask for password)

For a separate username, we use the --become-user switch.

  1. For executing a specific command, we use the -a option (Let us say we want to fetch the show version of the routers in myrouters list in a parallel method ):
ansible myrouters -a "show version" -f 5 

The 5 is the default value for number of parallel threads, but to change this value again, we can modify it in the Ansible configuration file.

  1. Another example is to copy a file from source to destination. Let us say we need to copy a file from the current source to multiple servers that are under, let's say, the servers group:
ansible servers -m copy -a "src=/home/user1/myfile.txt dest=/tmp/myfile.txt"
  1. We want to start the httpd on the web servers:
ansible mywebservers -m service -a "name=httpd state=started"

In reverse, if we want to stop the httpd:

ansible mywebservers -m service -a "name=httpd state=stopped"
  1. As another important example to look at, let us say we want to run a long running command like show tech-support, but do not want to wait for it in the foreground. We can specify a timeout (600 seconds in our case) for this:
ansible servers -B 600 -m -a "show tech-support"

This would return a jobid that can be referred later on for the update. Once we have the jobid, we can check the status of that particular jobid using the command given as follows:

ansible servers -m async_status -a "jobid"
  1. There is an additional command that provides all the information about a particular node that Ansible can fetch and work upon:
ansible localhost -m setup |more 

The output to view the facts on the local machine (localhost) is as follows:

  1. Another ad hoc command that is commonly used is the shell command. This is used to control the overall OS, or shell, or root scenarios. Let us see an example to reboot the managed nodes in the servers group:
ansible servers -m shell -a "reboot"

If we want to shut down the same set of servers instead of reboot:

ansible servers -m shell -a "shutdown"

This way, we can ensure that using the ad hoc task we can quickly perform basic tasks on individual or groups of managed nodes to quickly get results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.243.106