Create-yml-from-bash-include

From I Will Fear No Evil
Revision as of 12:55, 15 October 2021 by Chubbard (talk | contribs) (Created page with "[https://stackoverflow.com/questions/41598377/create-bash-script-to-create-yaml-files-from-existing-linux-etc-hosts-files|Source site] This is going to be VERY fragile, but d...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

site

This is going to be VERY fragile, but does work for simple yaml. Overall it is last resort, but can get you out of a jam.

{
  printf 'host_entries:\n'
  while read -r -a line; do
    [[ ${line[0]} ]] || continue             # skip blank lines
    [[ ${line[0]} = "#"* ]] && continue      # skip comments
    [[ ${line[0]} = 127.0.0.1 ]] && continue # skip localhost

    set -- "${line[@]}" # assign words read from line to current argument list
    ip=$1; shift        # assign first word from line to ip
    for name; do        # iterate over other words, treating them as names
      printf "  %s:\n    ip: '%s'\n" "$name" "$ip"
    done
  done
} </etc/hosts >yourfile.yaml

This is WRONG but works for this simple case.  Creating a YML file from a vars file should be less problematic however since it is key=value

This will allow creating a YML file with bash substitutions.
example is for bosh!!
https://www.starkandwayne.com/blog/bashing-your-yaml/