I'm using Jenkins multi-line string parameter to append new entries to a yaml file. But then I want to format those entries to match the yaml syntax.
The desired end result is:
var:
- subvar1
- subvar2
- subvar3
...
What I have done so far is:
I'm running a shell script which takes that multi-line string parameter (an environment variable) which looks like subvar1\nsubvar2\nsubvar3
and appends it into the yaml file. So, my line code looks like this:
printf "var:\n${HOSTNAMES}" >> ${WORKSPACE}/group_vars/all
The yaml file looks like this after that:
var:
subvar1
subvar2
subvar3
I am seeking assistance from you guys on how to add two spaces and a hyphen then a space before each line that comes after var:
EDIT: If SED or AWK is not a good idea to do this, what is a better way to do it?
{ printf 'var:\n' ; printf ' - %s\n' "${HOSTNAMES[@]}"; } >> wherever
?${HOSTNAMES[@]}
with a for loop.for h in ${HOSTNAMES[@]}; do echo " - ${h}" >> file; done
.