Search code examples
ansiblejinja2ansible-template

add the variable value to jinja2 template without changing the '\n' to new line


I have variable in ansible variable with the \n in it:

PRIVATE_KEY: "-----BEGIN PRIVATE KEY-----\\nnyRFqZbDSvw37GHNXTi0bpy3oV+3lqP2KbEjwGBfuA41hIGpZrIgGb5emno79IKs\nUXyEDnZyGTV1vbAo5x8VDWuMxSd2sBqtZNCm5exDOo78a/Tg8XLPy7NJHXn1Igb/\nGsFBNQiQ45EQrV/EflOHsT3eehvvUnqmh77lyVu7u461smgthehQfMFvnegjQE1o\nKhYYsk2rhHvz/N4UExK+E5e5yN3gJC7XZj30tenDoY/DWP565v44KXZLr2ec8998\nmulZQwww//Qc6N5M1xsyj6x2PsgBXH4hqt/BKkOUO4Z4ihbINoYb7i0BB0+RCqY/\nyYol4EQ1AgMBAAECggEAByhs4hatnCVzAanAnBcubrWjkDGKVVc9eB05bZrk83mC\nW+apQxcVSXWZCSZJB9He1eV4fpHA6TAFjtILHRut1IjUPH5jqOWvr1KuG+h1PfSQ\nWeSKg+9DSzt5Irxz3XlYUfnCOYfkmyiESux4s/4ZpWYAeasYDbXjlLMe7th5A9jZ\nZ4pAQMuEBcP54g+DLjUX9glebdHYJ9fEM2ekapu1UulMhMo/ioFU+mwxBUF+HORc\n2iFTlId7n4wHTBCZQfz6zWIQgcTHIt9d+hHPpvCh7BjsCNHujMCRJSR6An9vmA6I\nQUq/Y56dK/AbuEWCypl6ycheaBm0sg1bR030Fl3WawKBgQD4MIzS39Ac496n2t9X\n0eWu+McO/sX6uszyU5nLyKOQ7cNrxFOxwwuJ5pJckeanYbWr5Nf+Ev4g0hRxdYJg\nY5RrajUnwnY1HhJ95yYKopmbvpb3v87A+o51mZFgOUj1X6544+lqHsFcTBlc7WWo\nQV8xneSwTVIR8CQzkx0z90NAkwKBgQDazCT3XMLrAGDoPVjaVMOvOkTWIWbGuRXg\nLs0VjkNqTqtsqr04CWO89ilC3B4/tcRgR1P\nwAW3U5j1qIuVFDxk5ZKvs8cBZzfpCByIpcOyIjTzuEVkeoEW06xliU4q+uLuXKeN\ngwf41/oNFwKBgE4nE6EvoxwF0U4wCNZhnR7s7dnV9kfO3MyN8McP5hm0FS5xQx2d\n2ptRBNOrieM5THjVdgJmvqldHdzgcO6jTNBKQHTFsm1li9Mjn4IH07EMYgOf3Z49\nB/EWK4YOY+AIgitFMXIePysuN2pgwvspoq1AGAxAPNCzOuMWt0Ju4TmLAoGBANLO\n7q8GySbzbmtU8E8sbH5gfgJynDvMCK+/aAnk0D+VHMXOnBFpupdN9eNdgieQUm0R\nTHV9z0oJKdugxRi1hAyO6J8YF2MW3pPWL6u6+B15D0PMA4STTQnNBWjssonsLtHa\nJPt0yeApI3L0NXxw5iHOcWon4I4Y57cJEJ1KOrpxAoGBAOxInBKe6+gB9JIFXDpH\nLv+EQA3usMvLZsWWhUeusir5XsJ63JCGlUbQJM/9LzZq1dFQvIcrsN8XPYN0DAp0\nINRbzHBNaB3fwg3JOr+jM07pxKOcXxLBq7zHPnipNVwLX/ZQpbpxPfYb9nwgx7ml\nPR9q1zM07RzEgH2ZuXqSPcTB\n-----END PRIVATE KEY-----\n"

I want to inject this variable in jinja2 template with the same formant but it change the \n to new line which causes the config problem on the destination.

    "private_key": "-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDUH0JybzWEm7aB
nyRFqZbDSvw37GHNXTi0bpy3oV+3lqP2KbEjwGBfuA41hIGpZrIgGb5emno79IKs
UXyEDnZyGTV1vbAo5x8VDWuMxSd2sBqtZNCm5exDOo78a/Tg8XLPy7NJHXn1Igb/
KhYYsk2rhHvz/N4UExK+E5e5yN3gJC7XZj30tenDoY/DWP565v44KXZLr2ec8998
yYol4EQ1AgMBAAECggEAByhs4hatnCVzAanAnBcubrWjkDGKVVc9eB05bZrk83mC
W+apQxcVSXWZCSZJB9He1eV4fpHA6TAFjtILHRut1IjUPH5jqOWvr1KuG+h1PfSQ
WeSKg+9DSzt5Irxz3XlYUfnCOYfkmyiESux4s/4ZpWYAeasYDbXjlLMe7th5A9jZ
0eWu+McO/sX6uszyU5nLyKOQ7cNrxFOxwwuJ5pJckeanYbWr5Nf+Ev4g0hRxdYJg
Y5RrajUnwnY1HhJ95yYKopmbvpb3v87A+o51mZFgOUj1X6544+lqHsFcTBlc7WWo
QV8xneSwTVIR8CQzkx0z90NAkwKBgQDazCT3XMLrAGDoPVjaVMOvOkTWIWbGuRXg
Ls0VjkNqTqtsqr04CWO89ilC3B4/tcRgR1P/pwViJYJe8cO2KKiqB6zlgSUuma2Y
wAW3U5j1qIuVFDxk5ZKvs8cBZzfpCByIpcOyIjTzuEVkeoEW06xliU4q+uLuXKeN
gwf41/oNFwKBgE4nE6EvoxwF0U4wCNZhnR7s7dnV9kfO3MyN8McP5hm0FS5xQx2d
2ptRBNOrieM5THjVdgJmvqldHdzgcO6jTNBKQHTFsm1li9Mjn4IH07EMYgOf3Z49
B/EWK4YOY+AIgitFMXIePysuN2pgwvspoq1AGAxAPNCzOuMWt0Ju4TmLAoGBANLO
7q8GySbzbmtU8E8sbH5gfgJynDvMCK+/aAnk0D+VHMXOnBFpupdN9eNdgieQUm0R
THV9z0oJKdugxRi1hAyO6J8YF2MW3pPWL6u6+B15D0PMA4STTQnNBWjssonsLtHa
Lv+EQA3usMvLZsWWhUeusir5XsJ63JCGlUbQJM/9LzZq1dFQvIcrsN8XPYN0DAp0
INRbzHBNaB3fwg3JOr+jM07pxKOcXxLBq7zHPnipNVwLX/ZQpbpxPfYb9nwgx7ml
PR9q1zM07RzEgH2ZuXqSPcTB
-----END PRIVATE KEY-----

Is there any specific format settings that I can use that it will not replace \n into new lines and keep the same formatting.


Solution

  • Since it looks like you're using Ansible, you have a few options. You can serialize the string as JSON using the to_json filter:

        - copy:
            dest: out1.yaml
            content: |
              private_key: {{ PRIVATE_KEY|to_json }}
    

    That gets you:

    private_key: "-----BEGIN PRIVATE KEY-----\nnyRFqZbDSvw37...EgH2ZuXqSPcTB\n-----END PRIVATE KEY-----\n"
    

    Or you can create a properly formatted YAML literal block:

        - copy:
            dest: out2.yaml
            content: |
              private_key: |
                {{ PRIVATE_KEY|indent(2, first=False) }}
    

    That gets you:

    private_key: |
      -----BEGIN PRIVATE KEY-----
      nyRFqZbDSvw37GHNXTi0bpy3oV+3lqP2KbEjwGBfuA41hIGpZrIgGb5emno79IKs
      .
      .
      .
      PR9q1zM07RzEgH2ZuXqSPcTB
      -----END PRIVATE KEY-----