1

I have a variable set in an encrypted ansible vault yaml file which has multiple special characters, including < { , " ' [ and %.

my_var: <X%X[X{,"<X'L

I need Ansible to take the variable as a raw string, but it's inserting a \ before the double-quote (") - at least that is what debug shows.

ok: [10.10.10.10] => {
    "my_var": "<X%X[X{,\"<X'L"
}

How can I get ansible to treat the variable as a raw string?

user3155618
  • 359
  • 1
  • 5
  • 14
  • 2
    That double quote is being escaped as Ansible is showing you a JSON representation of your task as it's being processed. If you were to write that string into a file, that backslash would not show. – SYN Feb 27 '23 at 21:37

1 Answers1

1

What you see depends on the callback. No quotation and/or escaping is needed

shell> cat my_var.yml 
my_var: <X%X[X{,"<X'L

To test it, use yaml callback and iterate the string character by character

- hosts: localhost

  vars_files:
    - my_var.yml

  tasks:

    - debug:
        msg: |
          {% for i in range(my_var|length) %}
          {{ i % 10 }}{% endfor %}

          {% for c in my_var %}
          {{ c }}{% endfor %}

gives (ANSIBLE_STDOUT_CALLBACK=yaml)

  msg: |-
    0123456789012
    <X%X[X{,"<X'L

You'll get the same result when you encrypt the file

shell> ansible-vault encrypt my_var.yml
Encryption successful
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • thanks Vladimir - this makes sense and is very helpful. The issue I was having didn't turn out to be the password at all, ansible was reading it correctly. I was trying to use the Ansible uri module and needed to use the "force_basic_auth" setting. – user3155618 Mar 01 '23 at 15:56