| by Arround The Web | No comments

Ansible Command Module

In certain cases, what is required is to just run instructions on the target remote hosts immediately, similar to how you would do on a command prompt in windows. The Ansible command module is useful in this scenario. You will understand the Ansible command module in this article, including how it functions and how you can utilize it to deliver instructions to managed remote hosts.

In Ansible, if we want to issue some commands to make changes or update anything on the target remote host, we will use the command module. The Ansible Ad-hoc module is the default module when we are using the “command” module in the Ansible tool. The only executables that the command module can run are those that are on target distant hosts. The main aim of the command module in Ansible is to sidestep the local terminal.

There are some restrictions in ansible, for example, variables like $HOSTNAME or $HOME are not accessible and operations like “&” and “<" etc are not functional in the command module. Any other Ansible module can be used if you wish to utilize these parameters. The command module in Ansible is unaffected by the remote host environment, which is why its outcomes are regarded as being more dependable and secure. On the target remote hosts or servers, that are specified as independent servers or hosting groups, the Command module is largely used to carry out standard Ubuntu instructions.

Prerequisites for Utilizing the Command Module in Ansible

This tutorial includes practical demonstrations of Ansible. To use the Ansible command module, we presume that you have the requirements listed below:

  • We first need Ansible-related software on your system so that we can utilize the Ansible tool before we can implement the command module. We are using Ansible 2.12 or a later version for the tutorial.
  • As a result, we require an Ansible controller to execute the commands on remote hosts and the remote hosts that we want to focus on for implementations.

Let us explore the command module premise in greater detail and examine how Ansible implements this by throwing several scenarios into practice.

Example 01: Checking the Uptime of a Target Remote Host by Utilizing the Command Module of Ansible

Every potential Ansible professional or powerful operator, such as an Ansible controller, must possess a firm knowledge of some commands found in the Ansible configuration tool. We are going to use one of the most important commands of the command module which is the “uptime” command in Ansible. The command uptime displays the present time, the number of active hosts, the typical server load over the previous 1, 5, and 15 minutes, as well as the amount of time your device has been functioning. Following the choices you specify, it is also able to restrict the relevant content at the same time.

Let us start by creating the playbook where we will write the uptime command to verify the uptime of the remote host. The command we’ll use to create the playbook in the Ansible tool is as follows:

[root@master ansible]# nano command_module.yml

After writing the above command, the command_module.yml playbook will be created and launched into a new editor on the name of the playbook which is “command_module”. In this editor, we first provide the remote hosts that we want to target. Here, we have targeted “all” the remote hosts. To obtain the information related to the targeted remote hosts, we will use Ansible “Gathering Facts,” module. The data gathered is typically referred to as “facts” or “variables” in the Ansible tool.

In the Ansible tool, we can gather the data of remote hosts individually by using the specific command which is “setup”. But in reality, the gathering facts variable is performed by calling this setup module by default in Ansible playbooks. To gather the data, we will pass the “true” or “false” value. As shown, we have passed “false” because we do not want to obtain the information of the target host.

Next, we will list the tasks which we want to do in the command_module playbook. We will start by naming the task. Then, we used the “register” parameter so that we can track and store the output in this Ansible parameter. Now, we will pass the command we want to perform which is “uptime”. Then, we used the “debug” command to print the output of the variable.

- hosts:
    - all
  gather_facts: false
  tasks:
      - name: Execute the Uptime command over the Command module
        register: uptimeoutput
        command: "uptime"

      - debug:
          var: uptimeoutput.stdout_lines

Hitting Ctrl+X will terminate the playbook editor. Now, we will create the inventory file, which will contain the details about the target remote host. The command we will use to create the inventory file in Ansible is mentioned below:

[root@master ansible]# nano host.yml

A different editor called “host” will be opened with the inventory file. Since “all” hosts were mentioned in the playbook, we will now go over each host individually. We only use one host in the inventory file, which we will refer to as “host1,” and we will include its details there. We will begin by providing the IP address, then the host’s identity, the ansible tool’s credential, the name of the connection that was formed between the controller and the host, and finally the port number.

All:
  hosts:
    Host1:
      ansible_host: 192.168.3.229
      ansible_user: ansible
      ansible_password: *******
      ansible_connection: ssh
      ansible_port: 22

We will now launch the playbook and the inventory file that were both created after being written to by the command that is listed below:

[root@master ansible]# ansible-playbook command_module.yml –i host.yml

Here is the output after running successfully where we have the information of host1. We have the time, days, total users’ data, and the load average of the host1.

Example 02: Utilizing UNAME Command to Obtain the Host Name and the Version of the Remote Host

Here is the second example of a command module in Ansible where we are going to use the “uname” command. We are using “uname” to get the information of the remote host of Ansible like we want to get the name of the remote host and which version of the ansible tool. First, we will enter the recently created playbook which is command_module.yml and then modify the tasks and commands. To open the command_module.yml playbook, we will use the “nano” command:

[root@master ansible]# nano command_module.yml

In the playbook, we have altered the tasks that were shown below in the code snippet. As shown in the command variable, we have passed the “uname” command along with the “-a” option so that we can get the information of all the hosts.

- hosts:
    - all
  gather_facts: false
  tasks:
      - name: Execution of the UName Instruction
        register: unameout
        command: "uname -a"

      - debug:
          var: unameout.stdout_lines

We will save the modifications and close the playbook. Now, we will run the playbook to get the output of the “uname” command and see what we get. For executing the playbook, below is the command we will use:

[root@master ansible]# ansible-playbook command_module.yml –i host.yml

The result of successfully performing the aforementioned command is shown below. The hostname and version are shown in green font.

Conclusion

We have learned one of the modules of Ansible which is the command module. We have discussed how it works in Ansible. Further, we have learned the two basic scenarios of the command module by implementing the examples.

Share Button

Source: linuxhint.com

Leave a Reply