How to merge profiles' user.vendor-data?

I actually did something today. This reuses Kusalananda’s answer to merge arrays in JSON with jq. I haven’t understood the limitation (nor have I been able to use the alternative posted later on in the thread) and I need to filter out yq’s --- but that’s minor.

Here is a snippet of the code:

echo '#cloud-config'

yq \
  --yaml-output \
  --slurp \
  '(.[0] | keys[]) as $k | reduce .[] as $item (null; .[$k] += $item[$k])' \
  "${vendor_data_file}" \
  "${new_data_file}" \
  | \
  sed \
  '/^---$/ d'

And you can use it as

incus profile set "${profile}" cloud-init.user-data="$(foo.sh cloud-init-vendor-data.yaml cloud-init-user-data.yaml)"

I can’t say if there are issues or limitations but it’s been working for me and my simple usage.

2 Likes