Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline variable in Ansible playbook without newlines (spaces)

Tags:

yaml

ansible

I am trying to define a "complex" (with quotes) multiline variable in Ansible. I don't want spaces as a result of the multiline setup. A "newline" at the end of each line is translated into a space. I have tried escaping the newline with "" as indicated in many questions/answers on the internet, but Ansible seems to ignore the "".

Code example:

      set_fact: 
        test: >-
          "v=DKIM1; k=rsa;" "p=MIIBIjANBgkqhkwetuwweeTYRjEFAAOCAQ8AMIIBCgKCA\
          QEAtJltC/t70wayxq0Rws5R7t+Q2GZ2llnY0nSHDkVnP9kDaYBTyHCk+OoiCehHjUr\
          9/60Rp7v5xDiS9ta5w8c4pyqgWCIvj7iCPVfVzEtSfOp0iwmvGvKeBMhY62+7BYyQ5\
          zOt45pJHVqQt78/L2S75AZY2RUVv85AXXUlx8Os54dBz8DsdnWilH+o0Ce+ZAor70i\
          z0TkEVCnuom" "3xv/bLbOyFI7EoxbhU2NVIer3uanPbl6HUr4B2P3y695yh4fi+oq\
          oOAM8Ah0rx0iruqcgoEPhNriEJ1PxW6ZDwFFnDgmR3hCaULQl4OtkWGwhG87ISBxC3\
          66xBjT+rTLys1yT0uu3QIDAQAB""

This leads to spaces everywhere there is a newline (at the end of the lines). E.g.:

"v=DKIM1; k=rsa;" "p=MIIBIjANBgkqhkwetuwweeTYRjEFAAOCAQ8AMIIBCgKCA (space!!!) QEAtJltC/t70wayxq0Rws5R7t+Q2GZ2llnY0nSHDkVnP9kDaYBTyHCk+OoiCehHjUr...

How can this be fixed?

like image 689
Arno Schots Avatar asked Oct 30 '25 20:10

Arno Schots


1 Answers

Use folded style and split the lines. Then join them. For example, given the simplified data

    test: |-
       "v=DKIM1; k=rsa;" "p=MIIB
       QEAtJltC/t70wayxq0Rws5R7"

The debug task

    - debug:
        msg: "{{ test|split('\n')|join }}"

gives

  msg: '"v=DKIM1; k=rsa;" "p=MIIBQEAtJltC/t70wayxq0Rws5R7"'

For Ansible less than 2.11 use split as a method

    - debug:
        msg: "{{ test.split('\n')|join }}"
like image 60
Vladimir Botka Avatar answered Nov 03 '25 09:11

Vladimir Botka