Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template error while templating string: #84433

Closed
1 task done
jyothi-balla opened this issue Dec 5, 2024 · 6 comments
Closed
1 task done

template error while templating string: #84433

jyothi-balla opened this issue Dec 5, 2024 · 6 comments
Labels
affects_2.13 bot_closed bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions.

Comments

@jyothi-balla
Copy link

jyothi-balla commented Dec 5, 2024

Summary

We first try to do a lookup vault to fetch the password, the password has characters "{%!" the below is how the rendered template file looks like

  create: true
  admin_user: "user1"
  admin_password: !unsafe "4{%!<----"
  users:
    "pgsqladmin":
      password: !unsafe "4{%!<----"
    "applicationscourtorders":
      context_user: true
      password: "password"

during a combine step

- name: Combine db variables from item-specific service values
  set_fact:
    merged_db_vars: "{{ merged_db_vars | combine(specific_service_vars.ansible_facts.db, recursive=True) }}"
  when: specific_service_vars.ansible_facts.db is defined

we get the following error
" Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected char '!' at 3. String: 4{%!<-ka---\n"

Have tried multiple options using to_yaml, to_json, quote
Can someone please help in getting this problem solved?

Issue Type

Bug Report

Component Name

jinja2

Ansible Version

$ ansible --version
ansible [core 2.13.13]
  config file = None
  configured module search path = ['/home/azureadmin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/lib64/python3.8/site-packages/ansible
  ansible collection location = /home/azureadmin/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/ansible/bin/ansible
  python version = 3.8.8 (default, Aug 25 2021, 16:13:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.4
  libyaml = True

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all
-

OS / Environment

centos8

Steps to Reproduce

We first try to do a lookup vault to fetch the password, the password has characters "{%!" the below is how the rendered template file looks like

  create: true
  admin_user: "user1"
  admin_password: !unsafe "4{%!<----"
  users:
    "pgsqladmin":
      password: !unsafe "4{%!<----"
    "applicationscourtorders":
      context_user: true
      password: "password"

during a combine step

- name: Combine db variables from item-specific service values
  set_fact:
    merged_db_vars: "{{ merged_db_vars | combine(specific_service_vars.ansible_facts.db, recursive=True) }}"
  when: specific_service_vars.ansible_facts.db is defined

Expected Results

I expected this to combine

Actual Results

" Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected char '!' at 3. String: 4{%!<-ka---\n\"

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. affects_2.13 labels Dec 5, 2024
@ansibot
Copy link
Contributor

ansibot commented Dec 5, 2024

Files identified in the description:

None

If these files are incorrect, please update the component name section of the description or use the component bot command.

@ansibot
Copy link
Contributor

ansibot commented Dec 5, 2024

@jyothi-balla ansible-core 2.13 is not supported and no longer receives bug fixes. Please test against one of the supported versions of ansible-core, preferably the most recent one, to see whether the bug has been fixed.

click here for bot help

@s-hertel s-hertel added needs_verified This issue needs to be verified/reproduced by maintainer and removed needs_triage Needs a first human triage before being processed. labels Dec 10, 2024
@mkrizek
Copy link
Contributor

mkrizek commented Jan 6, 2025

Just by running what was provided in the issue description, I was not able to reproduce it on the devel branch:

- hosts: localhost
  gather_facts: false
  vars:
    v:
      admin_password: !unsafe "4{%!<----"
      users:
        pgsqladmin:
          password: !unsafe "4{%!<----"
  tasks:
    - debug:
        msg: "{{ merged_db_vars | default({}) | combine(v, recursive=True) }}"
TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "admin_password": "4{%!<----",
        "users": {
            "pgsqladmin": {
                "password": "4{%!<----"
            }
        }
    }
}

PLAY RECAP **********************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

@mkrizek mkrizek added the needs_info This issue requires further information. Please answer any outstanding questions. label Jan 6, 2025
@Akasurde
Copy link
Member

Akasurde commented Jan 8, 2025

@jyothi-balla You are using {% in the string which wants to get a tag like if, endif etc.

For example

"{% if True %}SOMEVALUE{% endif %}"

but you are providing ! which the Jinja template engine does not expect, causing templating to fail.

I would suggest adding a condition in password generation which will not have {, % in consecutive order like {% or %}

@ansibot
Copy link
Contributor

ansibot commented Jan 22, 2025

@jyothi-balla This Issue is waiting for your response. Please respond or the Issue will be closed.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Feb 4, 2025

@jyothi-balla You have not responded to information requests in this Issue so we will assume it no longer affects you. If you are still interested in this, please create a new Issue with the requested information.

click here for bot help

@ansibot ansibot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2025
@sivel sivel removed the needs_verified This issue needs to be verified/reproduced by maintainer label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.13 bot_closed bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions.
Projects
None yet
Development

No branches or pull requests

6 participants