Ansible Command

Ansible Command

The command module in ansible is used to run commands or contents in the remote objective system. On the other hand, it is also used to execute orders on a remote hub.

This command module is utilized to execute straightforward Linux commands or statements on a remote hub or server, a portion of the host group, or an independent server referenced in the host group.

Ansible Command Module and Shell Module

The shell module comes in a role when we want to runs command in remote servers, in your decision's shell. Of course, the commands are always executed on the/bin/sh shell. Here you can utilize the various activities, for example, '|', '<', '>' and so forth, and ansible climate variables, for example, $HOME.

If the processing of commands is not executed by the command module through a shell, it doesn't uphold the above tasks.

You can provide the commands of your choice to run a similar way you give it on a UNIX shell, command name following the contentions or arguments.

 - name: Running a command utilizing the shell module
    shell: ls - lrt > test.txt 

The main command records all the documents in the recent folders and composes them to the record, test.txt.

 - name: Running a command utilizing the command module
    order: my.txt 

The above illustration shows the material or content of the my.txt document.

Changing the Default Directory

The commands will consistently execute in the default directory. You can transform and indicate the path of a directory to execute the command using the chdir parameter. This parameter is accessible for both kinds of modules, whether command or shell module. 

We can change the default shell by determining the required shell's absolute path in the executable parameter.

 - -  hosts: loc
    tasks:
     - name: ansible order with chdir and executable parameters
        order: ls - lrt
        args:
             chdir:/home/Ansible/command_chdir_example
             executable:/receptacle/slam 

The above illustration uses the "Bourne Again Shell" by providing the outdated path/bin/bash. Furthermore, changed the registry or directory to /home/Ansible/command_chdir_example.

Executing Multiple Commands

If you want to execute various commands, you can offer them to both shell and command modules using the "with_items".

Illustration 1:

 - hosts: loc
    tasks:
     - name: Ansible order module various orders
        command: "contact "
        with_items:
            - my.txt
            - my1.txt
            - my2.txt
        args: chdir:/root/ansible 

Illustration 2:

  - hosts: loc
     tasks:
     - name: Ansible shell module various orders
        shell: "cat {{ item }} | grep ansible"
        with_items:
        - my.txt
        - my1.txt
        - my2.txt
        args:
           chdir:/root/ansible 

In the above illustrations, we can see that we wish to run three documents; my.txt, my1.txt, and my2.txt. As I provided the {{item}} keyword in the command, it will be supplied with a list component in every cycle. It guarantees that the degree of space of "with_item" is similar to the module name.

Requirements

The following prerequisites are required on the host that executes the given modules.

  • Python version  2.6
  • Pexpect version  3.3

Parameters

parameterChoices/defaultscomments
chdirDefault: noneTransform into this directory prior to executing the command  
createsDefault: NoneA filename (from ansible version 2.0), when a similar file exists already, this progression won't be executed.  
executableDefault: NoneTransform the shell utilized to run the command. Ought to be a complete path to the executable.  
free_form (needed)Default: NoneThe shell module consumes a free structure to execute as a string. There's not a genuine alternative named "free structure".  
removesDefault: NoneA filename, when it doesn't exist, this progression won't be run.
stdin (included in 2.4)Default: NoneSet the stdin of the command straightforwardly to the predetermined value.  
warn (included in 1.8)Default: yesOn the off chance that command admonitions are on in ansible.cfg, don't caution about this specific line whenever set to true/false.
command (needed) The command module takes command to execute.
Responses (needed) Planning anticipated string/regex and string to react with. If the reaction is a rundown, progressive matches return progressive responses. Rundown usefulness is new in 2.1.
timeoutDefault: 30A measure of time in seconds to sit tight for the normal strings.
echoDefault: noRegardless of whether to repeat out your reaction strings.
login_promptDefault: Login:Login or username brief to anticipate
password_promptDefault: password:Login or username brief to anticipate  
portDefault: 23Remote port to utilize  
pauseDefault: 1Seconds to stop between each command gave  
promptsDefault: ['$']List of prompts expected prior to sending next order
password   The password for the login