vs code formats yaml file with extra spaces between brackets - visual-studio-code

I use vscode as an editor and have several yaml files in the project where parameter replacement occurs. However, it re-formats only one file with extra spaces between brackets, a file named service.yaml used by helm in our ci/cd pipeline. See below for before and after.
and after
I was wondering if the name of the file has particular significance for vscode or any other extensions... Thanks.

Uncheck Bracket Spacing in Extensions/YAML.
Edit: There is a caveat. This will format {{ foo }} or { { foo } } to {{foo}}, which is not necessarily syntactically correct in terms of Jinja templating.

One of the way how to resolve this issue - wrap values into the quotes.
Like this:
app: "{{ .Values.name }}"
Probably not the best solution, but it works for me.

Related

how to use escape sequences in strings in github actions expressions

the goal is to write a github actions expression to match "there is /command on a line by itself" -- as such a few edge cases need to be handled (exactly at the beginning, exactly at the end, and somewhere in the middle).
the github actions expressions provide a few helpful expressions -- notably contains(haystack, needle), format(fmt, var0, var1, ...)
using these I should be able to construct something like:
if: contains(format('\r\n{0}\r\n', github.event.comment.body), '\r\n/command\r\n')
however this doesn't seem to work as expected -- it is always skipped unless the comment is exactly /command
it appears that github takes strings very literally!
this little demo:
- run: python3 -c 'print(os.environ["TEST"])'
env:
TEST: ${{ format('\r\n{0}\r\n', '/command') }}
produces the following output:
'\\r\\n/command\\r\\n'
the double backslashes being a literal \ when printed
fortunately there's a bit of hack you can do by utilizing fromJSON (which will process escape sequences)
using our little demo:
- run: python3 -c 'print(os.environ["TEST"])'
env:
TEST: ${{ format(fromJSON('"\r\n{0}\r\n"'), '/command') }}
(note that you have to have double-quotes embedded in the single quotes, we're parsing a json string)
now produces:
'\r\n/command\r\n'
success \o/
adapting that to the original question:
if: contains(format(fromJSON('"\r\n{0}\r\n"'), github.event.comment.body), fromJSON('"\r\n/command\r\n"'))

What is the hyphen after the ENV variable DOCKER_REGISTRY for in a docker-compose file

In my docker-compose files, my image tag is like so
image: ${DOCKER_REGISTRY-}webapi
However the ENV variable is just DOCKER_REGISTRY, so what's the hyphen for?
Also, now I'm wanting to push these images to ECR, when I set the DOCKER_REGISTRY to my ECR instance, there is no /, between the DOCKER_REGISTRY and webapi part, so I guess I need to change it to look like image: ${DOCKER_REGISTRY-}/webapi? But maybe only for my non-dev docker-compose files?
The - is part of the syntax for variable substitution, which is based on the standard described here: https://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html#tag_02_06_02
${SOME_VAR-<default value if variable is not set>}
In this case, no default value is set, so it's redundant.
Don't add a /. it's not a path, it's the name of the image.

adding single quotes to helm value

In my values.yaml file for helm, I am trying to create a value with quotes but when I run it, it gives a different result
values.yaml
annotation: '"ports": {"88":"sandbox-backendconfig"}}'
{{ .Values.annotation }}
what shows when I do dry run
"ports": {"88":"sandbox-backendconfig"}}
how can I make the single quotes around it show also
When the Helm YAML parser reads in the values.yaml file, it sees that the value of annotation: is a single-quoted string and so it keeps the contents of the value without the outer quotes.
As the YAML spec suggests, you can include single quotes inside a single-quoted string by doubling the quote. It might be more familiar to make this a double-quoted string and use backslash escaping. A third possibility is to make this into a block scalar, which would put the value on a separate line, but wouldn't require any escaping at all.
annotation: '''"ports": {"88":"sandbox-backendconfig"}}'''
annotation: "'\"ports\": {\"88\":\"sandbox-backendconfig\"}}'"
annotation: >-
'"ports": {"88":"sandbox-backendconfig"}}'
I'm not sure what context you're trying to use this in, but if this is a more structured format, you can use Helm's toYaml or toJson functions to build up the annotation value for you.
# values.yaml
ports:
'88': sandbox-backendconfig
# templates/some-resource.yaml
annotations: {{ printf "\"ports\": %s" (toJson .Values.ports) | squote }}
Check the method below,
Values.yaml
annotation: '"ports": {"88":"sandbox-backendconfig"}}'
Template
{{ .Values.annotation | squote }}
This should resolve your issue.
squote will put single quotes around the deduced value.

Passing xml data to helm chart

Let's say there is a config.xml file outside the chart directory. Can this file be copied to a directory inside the container?
If its inside the chart directory, its pretty easy to use configMap as in
{{ (tpl (.Files.Glob "myconf/*").AsConfig . ) | indent 2 }}
Since the file is outside chart directory its not supported in helm2(although there is some talk of supporting in helm3).
Thought of putting as in
key: |
<tag>abc</tag>
Then read in the configMap and put as file.
Is there any elgant way to do it?
Yes. All examples I see until now use this way.

add quotes around variable in ansible variables

I've got problem with one of ansible playbooks (enrise.postgresql).
It accepts variable postgresql_listen_addresses (list of values to listen_addresses in /etc/postgresql/9.3/main/postgresql.conf).
I want to use value ansible_default_ipv4.address, but ansible provide it without quotes, and postgress wants it with single quotes (like '192.168.0.1').
I have variable like this:
postgresql_listen_addresses: '{{ [ ansible_default_ipv4.address ] }}'
When address is without quotes, postgress complains about unquoted IP address (syntax error).
How can I add quotes around value of ansible_default_ipv4.address?
Thanks.
UPD:
I was able to solve it with this ugly code. If someone can better, please help.
print_quotes: "'%s'"
postgresql_listen_addresses: [ "{{print_quotes|format(ansible_default_ipv4.address) }}"
You can also use the quote filter:
postgresql_listen_addresses: "{{ ansible_default_ipv4.address | quote }}"
See Playbooks Filters.
Did you already try this?
postgresql_listen_addresses: "[ '{{ansible_default_ipv4.address}}' ]"
or this if postgress expects double quotes instead of single.
postgresql_listen_addresses: '[ "{{ansible_default_ipv4.address}}" ]'
I have no simple way of testing so I only have hope to support the theory... :-)