Ad hoc Command

Command-line tools with ansible ad-hoc command are used to automate task on single or multiple managed nodes.

Shell Commands

Reboot a company’s servers in 10 parallel forks at a single time. For this, we need to set up the SSHagent for connection.

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

To run reboot for all your company servers in a group, named ‘abc’, in 10 parallel forks −

$ ansible abc -a "/sbin/reboot" -f 10

Transferring File

You can make the use the Ad-hoc commands for doing SCP (Secure Copy Protocol) lots of files in parallel on multiple machines.

  • Transferring file to multiple servers/machines
    $ ansible abc -m copy -a "src = /etc/yum.conf dest = /tmp/yum.conf"
    
  • Creating new directory
    $ ansible abc -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory" 
    
  • Deleting whole directory and files.
    $ ansible abc -m file -a "dest = /path/user1/new state = absent"
    

Managing Packages

The following command checks if apt package is installed or not, but does not update it

$ansible abc -m apt -a "name = demo-nginx-1 state = present"

The following command checks if the package is not installed.

$ ansible abc -m apt -a "name = demo-nginx-1 state = absent" 
Subscribe Now