right now I am using a shell script in ansible that would be much more readable if it was on multiple lines
- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }}"
Just not sure how to allow multiline script in Ansible shell module
Ansible uses YAML syntax in its playbooks. YAML has a number of block operators:
The
>
is a folding block operator. That is, it joins multiple lines together by spaces. The following syntax:Would assign the value
This text has multiple lines\n
tokey
.The
|
character is a literal block operator. This is probably what you want for multi-line shell scripts. The following syntax:Would assign the value
This text\nhas multiple\nlines\n
tokey
.You can use this for multiline shell scripts like this:
There is one caveat: Ansible does some janky manipulation of arguments to the
shell
command, so while the above will generally work as expected, the following won't:Ansible will actually render that text with leading spaces, which means the shell will never find the string
EOF
at the beginning of a line. You can avoid Ansible's unhelpful heuristics by using thecmd
parameter like this:https://support.ansible.com/hc/en-us/articles/201957837-How-do-I-split-an-action-into-a-multi-line-format-
mentions YAML line continuations.
As an example (tried with ansible 2.0.0.2):
The shell command is collapsed into a single line, as in
ls --color /home