Print to standard output without escaping

If you want to print some values during debugging or show some message to user with debug module, you may notice that Ansible outputs JSON-encoded strings. There are some tricks to overcome this.

Let's say you want to print some command that should be executed, e.g.: "with this argument".

If you run this task:

- debug:
    msg: "with this argument"

You'll end up with:

ok: [localhost] => {  
    "msg": " \"with this argument\""

Note that double quotes are escaped, because this is JSON representations of object { "msg": "<string>" }.

You can use some of this tricks to print raw string without JSON-encoding it:

    # Print as loop item
    - name: Print command as loop item
        dummy: value # Just to make some task without output
        - 'Execute this: "with this argument"'

    # Print as task title
    # Not suitable for different commands per host, because task title is common for all hosts
    - name: 'Execute this: "with this argument"'
        msg: Execute command from task title

    # Print as pause statement
    # Not suitable for different commands per host, because pause task skips host loop (forced run_once:yes)
    - name: Print command as pause statment
        prompt: 'Execute this and press enter: "with this argument"'

This will produce the following output:

TASK [Print command as loop item] **********************************************  
ok: [localhost] => (item=Execute this: "with this argument")

TASK [Execute this: "with this argument"] ****************************  
ok: [localhost] => {  
    "msg": "Execute command from task title"

TASK [Print command as pause statment] *****************************************  
[Print command as pause statment]
Execute this and press enter: "with this argument":  
ok: [localhost]