problem quoting string

221 views
Skip to first unread message

gregory....@gmail.com

unread,
Feb 20, 2023, 10:03:54 AM2/20/23
to Ansible Project
Hello, 

having something like this in my playbook
vars:
  somevar: 
     command:
            - run: | 
                    "somecommand 'somestring' | awk '{print $2}'"

tasks:
   - name: output var to file
      copy:
          content: "{{ somevar | to_nice_yaml(sort_keys=false,indent=2) }}"
           dest: /tmp/tempfile
 
when I look at /tmp/tempfile it looks like ansible doubles apostrophes:
command:
     -- run: '"somecommand ''somestring'' | awk ''{print $2}''"

is there any way to make ansible produce file without extra apostrophes?

thanks.

Dick Visser

unread,
Feb 20, 2023, 10:42:14 AM2/20/23
to ansible...@googlegroups.com
When I try that code I get something different altogether, note the
single dash for the list item:


~$ cat /tmp/tempfile
command:
- run: '"somecommand ''somestring'' | awk ''{print $2}''"

'

But this seems OK as that is what ansible is instructed to do.

So, the better question is what are you trying to do?
What do you want that file to look like - exactly?

Also, you ask for yaml, and when that is emitted, ansible (or the yaml
library) emits whatever is needed to make it valid YAML.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/75403ea8-3d4d-4293-97cf-5ba77cfb1878n%40googlegroups.com.

Vladimir Botka

unread,
Feb 20, 2023, 11:57:17 AM2/20/23
to gregory....@gmail.com, ansible...@googlegroups.com
Let's simplify the variable for testing

cmd: |
"somecommand 'somestring'"


1) The filters *to_yaml* and *to_nice_yaml* will quote the strings

- copy:
dest: /tmp/tempfile
content: "{{ cmd|to_yaml }}"

gives (note the empty line and the closing single-quote

shell> cat /tmp/tempfile
'"somecommand ''somestring''"

'

Remove the trailing new line by adding the dash to the block

cmd: |-
"somecommand 'somestring'"

now the task creates the file

shell> cat /tmp/tempfile
'"somecommand ''somestring''"'

In a single-quoted block, the single-quote is the only character that
have to be escaped; by itself. This is the reason of the 'extra
apostrophes'.

2) Add the dash to your block to remove the new line

somevar:
command:
- run: |-
"somecommand 'somestring' | awk '{print $2}'"

The filter *to_nice_yaml* will quote the string

- copy:
dest: /tmp/tempfile
content: "{{ somevar|to_nice_yaml }}"

gives valid YAML with the list *command* and single item

shell> cat /tmp/tempfile
command:
- run: '"somecommand ''somestring'' | awk ''{print $2}''"'

Read the file and use the command

- include_vars:
file: /tmp/tempfile
- command: "echo {{ command.0.run }}"
register: out
- debug:
var: out.stdout

gives probably what you want

out.stdout: somecommand 'somestring' | awk '{print $2}'

The run-string was properly expanded and used in the command line.

3) If you really want to get rid of the 'extra apostrophes' you'll
have to expand the strings in the template. For example,

- copy:
dest: /tmp/tempfile
content: |
{% for key,val in somevar.items() %}
{{ key }}:
{% for i in val %}
{% for k,v in i.items() %}
- {{ k }}: {{ v }}
{% endfor %}
{% endfor %}
{% endfor %}

gives

shell> cat /tmp/tempfile
command:
- run: "somecommand 'somestring' | awk '{print $2}'"

Note that now the double-quotes are not part of the string stored
in command.0.run


HTH,

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages