Bootstrap FreeKB - Ansible - Run a command using the command module
Ansible - Run a command using the command module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

Typically, the shellcommand, or script modules are used to invoke a command on a managed node (e.g. target systems).

Python (e.g. /usr/bin/python) needs to exist on the managed node to use these modules.

  • shell
  • command (this article)

Python is not need to use these modules. 

In this example, the ps command will be invoked on the managed nodes.

---
- hosts: localhost
  tasks:
  - name: ps command
    command: ps
...

 

The following should be produced.

TASK [ps command]
changed: [localhost]

 

By default, no stdout is printed. register and debug can be used to print output to the console.

---
- hosts: localhost
  tasks:
  - name: ps command
    command: ps
    register: out

  - name: print out
    debug:
      var: out
...

 

Which should return something like this.

TASK [print out]
ok: [localhost] => {
    "out": {
        "changed": true, 
        "cmd": "ps", 
        "delta": "0:00:02.674260", 
        "end": "2021-08-03 23:34:44.388683", 
        "failed": false, 
        "failed_when_result": false, 
        "msg": "non-zero return code", 
        "rc": 1, 
        "start": "2021-08-03 23:34:41.714423", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "root     122418 122378  0 23:41 ?        00:00:00 bash", 
        "stdout_lines": [
          "root     122418 122378  0 23:41 ?        00:00:00 bash"
        ]
    }
}

 

Often, you just want to get the rc (return code) and stdout and stderr. For debugging purposes, you may need to include ignore_errors: true so that errors are ignored so that you can print the rc (return code), stdout and stderr. Then, use meta: end_play so that the playbook does not continue through the next tasks.

---
- hosts: localhost
  tasks:
  - name: ps command
    command: ps
    register: out
    ignore_errors: true

  - name: return code
    debug:
      var: out.rc

  - name: stdout
    debug:
      var: out.stdout

  - name: stderr
    debug:
      var: out.stderr

  - meta: end_play
...

 


Often, a return code of 0 or 1 is ok when using the command module. failed_when can be used to fail when the rc (return code) is not 0 or 1. Check out my article on resolving non-zero return code.

---
- hosts: localhost
  tasks:
  - name: ps command
    command: ps
    register: out
    failed_when: out.rc not in [ 0, 1 ]
...

 


Become (root, sudo)

If you do not have permission to issue a certain shell command, permission denied will be returned. To resolve this, the become module can be used to become root.

 


The chdir argument can be used to change into a certain directory on the managed node before running the command.

---
- hosts: localhost
  tasks:
  - name: invoke example.sh
    command: example.sh
    args:
      chdir: /tmp
...

 

Let's say the creates parameter contains /tmp/foo.txt. If /tmp/foo.txt exists, the task will not be run. If /tmp/foo.txt does not exist, the task will be run.

---
- hosts: localhost
  tasks:
  - name: invoke example.sh
    command: example.sh
    args:
      creates: /tmp/foo.txt
...

 

Let's say the removes parameter contains /tmp/foo.txt. If /tmp/foo.txt exists, the task will be run. If /tmp/foo.txt does not exist, the task will not be run.

---
- hosts: localhost
  tasks:
  - name: invoke example.sh
    command: example.sh
    args:
      removes: /tmp/foo.txt
...

 

Let's say you have a script that prompts for input, like this.

What is your name?

 

The stdin parameter can be used to pass standard input to the script.

---
- hosts: localhost
  tasks:
  - name: invoke example.sh
    command: example.sh
    args:
      stdin: "John Doe"
...



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter bcc916 in the box below so that we can be sure you are a human.