Ansible Playbook – Unsafe Variable Values Break After Upgrading from Ansible 2.10 to 2.12

ansible-playbook

I have upgraded my ansible setup from ansible 2.10 to ansible 2.12 (from ansible 5) using homebrew on Mac OS.
Since then a playbook has stopped producing working results.

- name: Assemble consul cluster
  become: true
  hosts: consul_nodes
  roles:
    - role: consul
      vars:
        consul_version: "1.11.1"
        consul_install_upgrade: true
        consul_group_name: "consul_nodes"
        consul_addresses:
          dns: !unsafe '169.254.1.1 127.0.0.1 {{  GetPrivateIP }} {{ GetInterfaceIP  \"docker0\" }}' 
          http: !unsafe '169.254.1.1 {{  GetPrivateIP }} {{ GetInterfaceIP  \"docker0\" }}' 
          https: !unsafe '127.0.0.1 {{  GetPrivateIP }}' 
          grpc: "127.0.0.1"
        consul_client_address: "169.254.1.1" 
        consul_node_role: server
        consul_bootstrap_expect_value: 3
        consul_bootstrap_expect: true

The playbook contains unsafe variable values that should not be templated as their value should be passed through to a JSON configuration file written to disk.

It was working fine before upgrading to ansible 2.12

ansible [core 2.12.1]
  config file = None
  configured module search path = ['/Users/martin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/5.1.0/libexec/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/martin/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.1 (main, Dec  6 2021, 23:20:29) [Clang 13.0.0 (clang-1300.0.29.3)]
  jinja version = 3.0.3
  libyaml = True

Now the whole JSON that the playbook is creating is invalid. Quotes (") are escaped as (").

I suspect that the problem is related to the unsafe variables as with the working ansible version 2.10 I got the same bad result when not using !unsafe.
This may be just a coincidence.

Best Answer

Looks like this is a bug that has been reported to the project and they are working on a solution:

template (the lookup plugin) through to_nice_json adds double-quote #76443

Related Topic