Bootstrap FreeKB - Ansible - Run a command using the shell module
Ansible - Run a command using the shell 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.

Python is not need to use these modules. 

In this example, the ps command will be invoked. register and debug can be used to print output to the console.

---
- hosts: localhost
  tasks:
  - name: ps command
    ansible.builtin.shell: ps
    register: out

  - debug:
      var: out
...

 

Which should return something like this.

TASK [ps]
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.

---
- hosts: localhost
  tasks:
  - name: ps command
    ansible.builtin.shell: ps
    register: out
    ignore_errors: true

  - ansible.builtin.debug:
      var: out.rc

  - ansible.builtin.debug:
      var: out.stdout

  - ansible.builtin.debug:
      var: out.stderr
...

 


Often, a return code of 0 or 1 is ok when using the shell 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
    ansible.builtin.shell: 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
    ansible.builtin.shell: 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
    ansible.builtin.shell: 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
    ansible.builtin.shell: 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
    ansible.builtin.shell: 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 9327a7 in the box below so that we can be sure you are a human.