FreeKB - Ansible shell module (invoking a shell command)
Ansible - shell module (invoking a shell command)

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). /usr/bin/python needs to exist on the managed node to use these modules. If Python does not exist, the raw module can be used instead. 

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

- name: ps command
  shell: "ps"

 

The following should be produced.

TASK [ps command]
changed: [server1.example.com]

 

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


non-zero return code

If non-zero return code is being displayed in the output of the play, refer to our article on resolving non-zero return code.

 


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.

 


Arguments

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

- 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.

- 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.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |