title: Ansible Vault inline variable with to_nice_json: Unexpected templating type error occurred...not JSON serializable
body: <p>I typically configure my projects by settings configuration variables in <code>vars/main.yml</code> and rendering a subset of those out to a JSON via <code>to_nice_json</code>.</p>
  
  <p>Consider an example of the <code>vars/main.yaml</code> like the one below:</p>
  
  <pre><code>  # Application Configuration Settings.
    config:
  
      dev:
        # General Settings.
        logger_level: DEBUG
  
        # PostgreSQL Server Configuration Settings.
        sql_host: "localhost"
        sql_port: 5432
        sql_username: "someuser"
        sql_password: "somepassword"
        sql_db: "somedb"
  </code></pre>
  
  <p>which I render out via a Jinja2 template and the <code>template</code> module with the following content:</p>
  
  <pre><code>{{ config.dev | to_nice_json }}
  </code></pre>
  
  <p>Recently I tried to use Ansible Vault to encrypt the sensitive bits, e.g., the <code>sql_password</code> through the <code>encrypt_string</code> command as such:</p>
  
  <p><code>ansible-vault encrypt_string --vault-id .ansible-vault-password "somepassword" --name 'sql_password'</code></p>
  
  <p>and inline the encrypted version directly in the YAML file like this:</p>
  
  <pre><code># Application Configuration Settings.
  config:
  
      dev:
        # General Settings.
        logger_level: DEBUG
  
        # PostgreSQL Server Configuration Settings.
        sql_host: "localhost"
        sql_port: 5432
        sql_username: "someuser"
        sql_password: !vault |
        $ANSIBLE_VAULT;1.1;AES256
        35383832623937353934636538306539623336633336643430396662323161333838333463653764
        3839653635326166303636643664333466376236626137310a323839373862626237643162303535
        35333966383834356239376566356263656635323865323466306362323864356663383661333262
        3165643733633262650a663363653832373936383033306137633234626264353538356630336131
        3063
        sql_db: "somedb"
  </code></pre>
  
  <p>However, when the <code>to_nice_json</code> filter is applied I get the following error:</p>
  
  <pre><code>fatal: [myrole]: FAILED! =&gt; {"changed": false, "msg": "AnsibleError: Unexpected templating type error occurred on ({{ config.dev | to_nice_json }}\n): somepassword' is not JSON serializable"}
  </code></pre>
  
  <p>As can be see, the variable is property decrypted but it errors out when serialising to JSON. If I wrap the inline vault variable in double-quotes, however, the decryption doesn't happen and the resulting JSON contains the entire vault blob.</p>
  
  <p>Am I missing something? Is this issue with the <code>to_nice_json</code> filter or am inlining it the wrong way?</p>