How to Write Multiline Shell Scripts in Ansible

0 min read 122 words

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
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags