Ansible interpreting rendered template as list

Hi,

I have a requirement to render a jinja template which starts and ends with square brackets and expands some variables. I’m finding that Ansible interprets the result as a YAML list but I need Ansible to treat it as a string. It seems the behaviour changes depending on whether a literal string or jinja template is used.

The following simple example illustrates the difference (see output of debug1 and debug2).

Does anyone know how to get debug1 to render output like debug2 but from a jinja template?

Playbook:

tasks:

  • name: debug1
    debug:
    msg: “[1,{{ 2 }}]”
  • name: debug2
    debug:
    msg: “[1,2]”

Output:

TASK [debug1] ******************************************************************
ok: [127.0.0.1] => {
“msg”: [
1,
2
]
}

TASK [debug2] ******************************************************************
ok: [127.0.0.1] => {
“msg”: “[1,2]”
}