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
parameter | Choices/defaults | comments |
chdir | Default: none | Transform into this directory prior to executing the command |
creates | Default: None | A filename (from ansible version 2.0), when a similar file exists already, this progression won't be executed. |
executable | Default: None | Transform the shell utilized to run the command. Ought to be a complete path to the executable. |
free_form (needed) | Default: None | The shell module consumes a free structure to execute as a string. There's not a genuine alternative named "free structure". |
removes | Default: None | A filename, when it doesn't exist, this progression won't be run. |
stdin (included in 2.4) | Default: None | Set the stdin of the command straightforwardly to the predetermined value. |
warn (included in 1.8) | Default: yes | On 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. | |
timeout | Default: 30 | A measure of time in seconds to sit tight for the normal strings. |
echo | Default: no | Regardless of whether to repeat out your reaction strings. |
login_prompt | Default: Login: | Login or username brief to anticipate |
password_prompt | Default: password: | Login or username brief to anticipate |
port | Default: 23 | Remote port to utilize |
pause | Default: 1 | Seconds to stop between each command gave |
prompts | Default: ['$'] | List of prompts expected prior to sending next order |
password | The password for the login |