Ansible Role shell

This role executes arbitrary shell commands on the host.

Note that this role is not idempotent by default, consider setting creates, removes or run_once (see below).



What it does


Executes shell commands.

Optional Role Variables



Default Value

shell__commands__host_var /

List of dictionaries containing the commands to execute. Subkeys:

  • name: Mandatory, string. A user-defined name for the commands. Must be unique. The commands are sorted by name and executed in that order.
  • commands: Mandatory, string. Shell commands to execute.
  • state: Optional, string. Whether to execute the commands or not. One of present or absent. Defaults to present.
  • chdir: Optional, string. Change into this directory before running the command. Defaults to null.
  • creates: Optional, string. A filename, when it already exists, the commands not be run. If run_once: true is set, this will be set to /etc/ansible/facts.d/shell_{{ item["name"] }}.state. Defaults to null.
  • removes: Optional, string. A filename, when it does not exist, the commands will not be run. Defaults to null.
  • run_once: Optional, bool. Only run the commands once. This creates a state file (/etc/ansible/facts.d/shell_{{ item["name"] }}.state) and sets creates correspondingly. Defaults to false.
  • stdin: Optional, string. Set the stdin of the command directly to the specified value. Defaults to null.
  • stdin_add_newline: Optional, bool. Whether to append a newline to stdin data. Defaults to true.
  • user: Optional, string. User to run the commands as. Defaults to null (most likely root).



# optional
  - name: '100-setup-step2'
    commands: |
      touch test2
      echo "test2 $(date)" >> /tmp/log
      sleep 3
    chdir: '/tmp'
    creates: '/tmp/test2'
  - name: '010-setup-step1'
    commands: |
      touch test1
      echo "test1 $(date)" >> /tmp/log
      sleep 3
    run_once: true
    chdir: '/tmp'
    user: 'linuxfabrik'


The Unlicense

Author Information

Linuxfabrik GmbH, Zurich