Skip to content

Commit fd668d1

Browse files
committed
avoid /per-rule replacing Ansible vars with interpreted values
As the comment suggests, awk was interpreting at least \n and & as special and corrupting the value. See also ComplianceAsCode/content#14343 (comment) Signed-off-by: Jiri Jaburek <comps@nomail.dom>
1 parent c1620e1 commit fd668d1

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

per-rule/runner.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,26 @@ if [[ -f $variables_file ]]; then
8484
# - name: Some title
8585
# vars:
8686
# var_something: '100'
87-
# use awk instead of sed because key/values may contain quotes
88-
# and awk allows us to work around that by passing variables via CLI options
89-
awk -i inplace -v key="$key" -v value="$value" \
90-
"{ print gensub(\"^([[:space:]]+)\"key\":.*\", \"\\\1\"key\": '\"value\"'\", 1) }" \
91-
"$playbook"
87+
# NOTE that the value may have any number of any special characters
88+
# like \n or & or ' or " or whatever
89+
# - sed would easily mangle it because it can't take a verbatim value
90+
# - awk also mangles & (gensub) or even \n (substr+print)
91+
# - bash printf can print anything except 0x00 (which is fine)
92+
while IFS= read -r playbook_line; do
93+
if [[ $playbook_line =~ ^([[:space:]]+)$key: ]]; then
94+
# vars:
95+
# var_something: |1
96+
# 100
97+
printf '%s%s: |1\n' "${BASH_REMATCH[1]}" "$key"
98+
while IFS= read -r value_line; do
99+
# prefix each value line with the original indent + 1 space
100+
printf '%s %s\n' "${BASH_REMATCH[1]}" "$value_line"
101+
done <<<"$value"
102+
else
103+
printf '%s\n' "$playbook_line"
104+
fi
105+
done < "$playbook" > "$playbook.tmp"
106+
mv -vf "$playbook.tmp" "$playbook"
92107
fi
93108
done < "$variables_file"
94109
fi

0 commit comments

Comments
 (0)