Skip to content

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 }}"
Code language: YAML (yaml)

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 }}"
Code language: YAML (yaml)

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
Code language: YAML (yaml)

Or better yet, wrap them:

- shell: cmd: | cat <<EOF This is a test. EOF
Code language: YAML (yaml)

See also  How to Execute a Shell Script in NodeJS
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x