add quotes around variable in ansible variables - postgresql

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... :-)

Related

Escape and preserve double quotes inside parameter in command line

I am testing group migration using ADMT cmd line, but the migration is failing for a specific case when the OU name contains double quotes.
ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc.CHILD.A.COM /td.COM /tdc.A.COM /to:"ParentOU/TEST!##$%^&*()_+{}|:"<>?[]\;',./" /intraforest:yes
In cmd this throws "> was unexpected at this time" and in powershell it keeps waiting for more parameters. The main purpose is to convert this to a c# script the migrates the users/groups but it failed in the testing phase with cmd/powershell. Is there any way to make this possible at least in C#?
I have tried escaping the double quotes with "", ^", ", `" but nothing seems to work. I have also tried assigning the value to a variable and using the variable in powershell. Using "" (as suggested in this Escaping special characters in cmd) is the only time the command runs but it still throws the following error.
Error: Unable to migrate groups. Unable to bind to container
'ParentOU/TEST!##$%^&()+{}|:<>?[];',./ /intraforest:yes'. Unable to
get distinguished name for
'A.COM/ParentOU/TEST!##$%^&;()+{}|:<>?[];',./ /intraforest:yes'. :
The parameter is incorrect. (0x80070057)
The same is working if I create another OU with same name except for the double quotes.
Please help in resolving this issue.
You can do solve it in different ways:
With escaping the characters with carets outside of the quote
setlocal EnableDelayedExpansion
set "to_param=ParentOU/TEST^!##$%^&*()_+{}|:"^^^^^<^^^^^>?[];',./"
call ADMT GROUP /to:"%%to_param%%"
The main problem will be the ADMT program, you need to know how it parses the arguments, particularly with regard to the rules how it escape quotes inside arguments.
You could test \" in set "to_param=ParentOU...\"^^^^^<^^^^^>?[];',./"

how do I use powershell variable in AWS CLI command

I am trying to run AWS CLI in Powershell 7 with a JSON string as parameter. AWS docs make it seem straightforward:
aws route53 ... --change-batch '{"Changes":[{"Action":"UPSERT"}]}'
however, I get an error:
Error parsing parameter '--change-batch': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
JSON received: {Changes:[{Action:UPSERT ...
So, it looks like double quotes are stripped at some point.
If I escape the quotes, command works:
aws route53 ... --change-batch '{\"Changes\":[{\"Action\":\"UPSERT\"}]}'
Now, I am trying to use a variable:
aws route53 ... --change-batch '{\"Changes\":[{\"Action\":\"UPSERT\", \"Value\":\"$MY_IP\"}]}'
but the variable is not resolved; that is, $MY_IP is passed to the server. I tried putting the whole string in double-quotes - but it looks like with double quotes internal quotes are removed even if I escape them. Also with backticks - it works as command continuation. I am looking at Microsoft docs - but the results I am getting are clearly different.
I don't think the problem has anything to do with AWS, but AWS CLI gave me a super twisted test case.
Fundamentally, PowerShell only performs string interpolation in double-quoted strings ("..."), also known as expandable strings: see this answer.
The surprising and unfortunate need to explicitly \-escape embedded " chars. in arguments for external programs applies irrespective of which quoting style you use, up to at least PowerShell 7.2.x: see this answer.
Thus, since you need a "..." string for string interpolation (so that variable reference $MY_IP is expanded to its value), you need two layers of escaping of embedded ":
`" (or "") in order to satisfy PowerShell's syntax requirements...
... and this escape sequence must additionally be escaped with \ in order for the external program to see the embedded " as such.
Therefore:
# Inside "...", pass-through " must be escaped as \`" (sic)
aws route53 ... --change-batch "{\`"Changes\`":[{\`"Action\`":\"`UPSERT\`", \`"Value\`":\`"$MY_IP\`"}]}"
You can ease the pain with the help of an (expandable) here-string, which obviates the need to escape the embedded " for PowerShell's sake:
# A here-string obviates the need to escape " for PowerShell's sake.
aws route53 ... --change-batch #"
{\"Changes\":[{\"Action\":\"UPSERT\", \"Value\":\"$MY_IP\"}]}
"#
Another option is to perform the \-escaping after the fact, programmatically:
aws route53 ... --change-batch (#"
{"Changes":[{"Action":"UPSERT", "Value":"$MY_IP"}]}
"# -replace '"', '\"')

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.

Jenkins to Powershell - Do I need to escape dollar signs in passed parameters?

I have a Jenkins pipeline that invokes a Powershell script via the Powershell plugin. The pipeline uses withCredentials to put the user/password for Powershell to use in SQL connections into variables. I pass them as properties on the command:
def psCmd="./Set-CheckmarxTeams -server ${server} -jkuser $sqluser -jkpass $sqlpass"
The script has them defined as parameters:
param ([string]$server='ad1hfddbst930\shared',[string]$jkuser,[string]$jkpass)
but the SQL connection using $jkuser and $jkpass fails. The password has a $ in the middle. I tried to Write-Host $jkpass and it only shows the part up to the $, but nothing after it. Do I need to modify the string before passing it in? If so, how?
"Escaping" the dollar sign is easy and good to know. In this case, #Richard Schaefer needed to pass the entire string of a password that included the dollar sign.
Storing a string in the $jkpass variable like so:
$jkpass = "thisisa$password"
Would output: thisisa
Therefore, storing the password with single quotes eliminates this issue.
$jkpass = 'thisisa$password'
This outputs: thisisa$password

What's wrong with this Bitvise Tunnelier SFTP command?

I'm trying to put together an SFTP command to be run through Powershell. The executable I have access to is SFTPC.exe (Bitvise Tunnelier)
The command I'm trying is
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls \"somefile.txt\""
According to the documentation at https://www.bitvise.com/files/sftpc-v4.14-usage.txt this should log in and run the command ls "somefile.txt" (quotes are escaped within the command parameter)
What actually happens is that I get another line for input, as if there's an unclosed quote.
I've tried adding an extra quote to the end
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls \"somefile.txt\"""
This connects and logs in, but the colland it tries to run is ls \somefile.txt"
Note the trailing quote and the leading slash.
So it looks like I'm missing something in the quote escaping, but I can't see what I might be doing wrong. I've also tried replacing double quotes with single in a couple of different places, experiments that usually end in a syntax error.
The escape character in powershell is not a backslash, it's the backtick.
Does the below work?
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls `"somefile.txt`""