Ansible

Remote Code Execution Using Ansible Command Module

For those of you working in the field of IT and Networking, working on servers must be a part of your daily routine.

Configuring different servers and applications manually can be quite a hassle if there are many systems. Fortunately, automation software and frameworks get the job done much more easily.

By using these applications, you can fully automate the management and configuration process using only a few lines of code. When it comes to IT automation of any kind, Ansible does the trick!

Ansible is a tool with a plethora of features. Talking about all of them in a single guide would be unjust, as each of these features deserves a guide.

This guide is a detailed tutorial on the Ansible Command Module. We’ll go over the basics of the module, along with syntax and features, with the help of some examples.

What is the Ansible Command Module?

As the name suggests, the Command Module makes it possible for a remote server/host to be able to run different commands. These remote users are referred to as nodes in a network.

The Ansible Command Module allows you to run multiple commands on these nodes. These nodes can exist as independent servers or parts of a group. That being said, there are some limitations that you should know about.

Limitations of the Command Module

The Command Module can be used for running simple baseline commands for the shell. The only limitation is that it cannot be used to execute more complex commands.

The Ansible Command Module cannot execute multiple commands in a single statement. Commands using the symbols <, >, |, etc., are not compatible with the Command Module. In such cases, the Shell module is definitely a better choice.

The Ansible Command Module has some requirements that need to be met before you can use it. These requirements are covered in the next section of this guide.

Prerequisites of the Ansible Command Module

To use the Ansible Command Module, you require the following:

  • A system that has Ansible installed.
  • A couple of hosts. In case you’re testing your servers, we recommend using virtual machines. Software like Oracle VirtualBox, Vagrant, and VMware Workstations are perfect for the job.

We’d also recommend that you have some basic know-how of how to execute commands in the Terminal to ensure that you know what you’re doing.

Once the prerequisites are met, we can proceed to learn how to use the Ansible Command Module.

Using the Ansible Command Module

If you have any previous experience working with shell scripts, then using the Ansible Command Module should be a walk in the park. In case you haven’t done that, worry not, as we’ll guide you on how to use the Command Module with the help of different examples.

The first thing that you must do is create an “ansible_hosts” file. This file will allow you to group your hosts, which will be useful for executing commands.

To create a group of hosts, type the following in the “ansible_hosts” file

$ ansible testservers --list -i ansible_hosts

        hosts (2):
         <Name of host/Virtual machine >
         <Name of host/Virtual machine >

In our case,

$ ansible testservers --list -i ansible_hosts
        hosts (2):
         VM1
         VM2

These hosts should now be grouped together under the alias “testservers.” Let us use this file as some examples.

Using the Command Module to Find Host Runtime

In this example, we’ll use the Ansible Command Module to find out how long our hosts have been running.

This can be done in two ways. The first is using Ad-Hoc, a quick way to execute functions via a single command. The second is by writing a script in the playbook.

To find the runtime using Ad-Hoc, use the following command:

        $ ansible testservers -m command -a uptime -i ansible_hosts

This should provide you with the output in the following syntax:

<Host/Virtual Machine name>| CHANGED | rc=0 >>
        <time> up <uptime in minutes>,  <number of users>,  <load average>

To execute the command as a Playbook, type the following:

To execute the command as a Playbook, type the following:
- name: <Give a suitable name to the script>
    hosts: <name of hostgroup>
    tasks:
      - name: <Give a suitable name to the task>
        register: uptimeoutput
        command: "uptime"

      - debug:
          var: uptimeoutput.stdout_lines

In our case, the script should look as follow:

- name: Find how long the hosts have been running.
    hosts: testservers
    tasks:
      - name: Command to find the uptime using the Command Module
        register: uptimeoutput
        command: "uptime"

      - debug:
          var: uptimeoutput.stdout_lines

With that, you can run this playbook by executing the following command into the Linux terminal:

ansible-playbook testbook.yml

The output should be similar to that of the Ad-Hoc command.

Using the Command Module to Find Disk Usage

The $df -h command is used to find out the disk usage on a system. The same command can be integrated with Ansible Command Module to find its hosts’ disk usage.

To find the disk usage using Ad-Hoc, use this command:

        $ ansible testservers -m command -a "df -h" -i ansible_hosts

To find the disk usage using Playbook, run the following script:

name: Servers disk usage
    hosts: testservers
    tasks:
      - name: execute the  $df -h command.
        register: dfout
        command: "df -h"

      - debug:
          var: dfout.stdout_lines

Using the Command Module to Restart Server

The Ansible Command Module can be used to restart a single server. The –limit parameter is used to limit this execution.

To execute the command using Ad-Hoc, type the following,

$ ansible testservers -m command -a "httpd -k restart" -i ansible_hosts -b --limit VM1

To execute the command using the Playbook, run the following script:

---
  - name: restart the webserver
    hosts: testservers
    tasks:
      - name: RestartWebServer
        register: httpdresout
        become: yes
        command: "httpd -k restart"
        when: ansible_hostname == "VM1"
      - debug:
          var: httpdresout.stdout_lines

This should restart your server. It should be evident that VM2 has been skipped.

Aside from the examples as mentioned above, there are a lot of commands that can be executed using the Ansible Command Module. Documentation of these commands and options can be found on the official Ansible documentation online.

Conclusion

We hope this guide helped you learn about the Ansible Command Module. We covered the module’s basics, its prerequisites, and some examples of how to use it. With this, we hope you have a pleasant experience working on Ansible.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.