How to Write Multiline Shell Scripts in Ansible


If you need to write a shell script in Ansible, you probably have something like this:

- name: iterate user groups
  shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }}
  with_items: "{{ users }}"

But how do you write multiline shell scripts with this format?

How to write Multiline shell scripts

- name: iterate user groups
  shell: |
    groupmod -o -g {{ item['guid'] }} {{ item['username'] }} 
    do_some_stuff_here
    and_some_other_stuff
  with_items: "{{ users }}"

Just note that Ansible can do some strange things with manipulations of arguments, so you may want to follow something like this:

- shell: |
    cat <<EOF
    This is a test.
    EOF

Or better yet, wrap them:

- shell:
    cmd: |
      cat <<EOF
      This is a test.
      EOF