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

Support Jinja2 in dict keys #19664

Closed
wants to merge 1 commit into from
Closed

Conversation

jtyr
Copy link
Contributor

@jtyr jtyr commented Dec 23, 2016

ISSUE TYPE

Feature Pull Request

COMPONENT NAME

Templar

ANSIBLE VERSION
$ ansible --version
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

SUMMARY

This patch allows to use Jinja2 expressions in dictionary keys. Using this patch, the output of the following play

---
- hosts: all
  connection: local
  gather_facts: no
  vars:
   test_var: aaa
   test_list:
     - "{{ test_var }}"
     - "{{ test_var }}": bbb
  tasks:
    - debug:
       var: test_list

will result into this output:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "test_list": [
        "aaa", 
        {
            "aaa": "bbb"
        }
    ]
}

instead of to this:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "test_list": [
        "aaa", 
        {
            "{{ test_var }}": "bbb"
        }
    ]
}

fixes #19956

@dano0b
Copy link
Contributor

dano0b commented Dec 23, 2016

Thank you for this pull request, hope it will be merged soon. I really missed this feature.

@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 feature_pull_request needs_triage Needs a first human triage before being processed. labels Dec 24, 2016
@dagwieers
Copy link
Contributor

Seems useful, but I have to wrap my head around the implications though.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 2, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jan 2, 2017
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 3, 2017
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jan 4, 2017
This patch allows to use Jinja2 expressions in the key of a dictionary.
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 4, 2017
@jasperla
Copy link
Contributor

I think this is an improvement over the current syntax that is required for Jinja2 expressions as keys:

key: key2  
var4: "{  
  '{{ key }}': value2
}"

@bcoca
Copy link
Member

bcoca commented Jan 11, 2017

As discussed in IRC, there is an issue with interdependent keys or overridden keys happening at 'template time' will be subject to dictionary order and not predictable.

d['x'] = 1
d['y'] = 2
d['z'] = {{ d['x'] + d['y']}}
d[{{variable}}] = 11
variable = 'x'

Since d[variable] does not get resolved until consumption (not definition) the value of d['z'] depends on templating/dictionary order so it can be either 3 or 13 on different runs.

The 'working dict' has these keys until templated 'x', 'y', 'z', '{{variable}}' and we won't know that {{variable}} is 'x' until it is templated on consumption but the user has no way of guaranteeing that d['z'] is templated before or after d['{{variable}}'] is.

@jtyr
Copy link
Contributor Author

jtyr commented Jan 11, 2017

After long IRC discussion with @bcoca I decided to close this PR as the proposed solution could lead to ugly implications.

For those interested in templating dict keys, please consider to use the solution described by @jasperla above.

@jtyr jtyr closed this Jan 11, 2017
@jtyr jtyr deleted the jtyr-dict_key_template branch January 11, 2017 17:53
@jtyr
Copy link
Contributor Author

jtyr commented Jan 25, 2017

@jasperla Actually your workaround doesn't work. The result is a sting and not a dict. So there is absolutely no way how to template dict keys in Ansible ;o( Or am I wrong, @bcoca?

@angystardust
Copy link
Contributor

@jtyr you're wrong. I was going mad trying to understand how to pass the key of the dict as a variable but after some failed tries, i managed to make it work using the following syntax:

networks: "{ '{{ net }}': {'gateway': '{{ gateway }}', 'network': '{{ vlan }}'}}"

@jtyr
Copy link
Contributor Author

jtyr commented Jan 26, 2017

@angystardust Thanks for sharing the more complex example. Thanks to your example, I have just realized that I was mixing the block and the inline format which resulted to a string. The rule is that I have to use the inline format only:

# Inline format only - this results to dict
networks: "{
  '{{ net }}': {
    'gateway': '{{ gateway }}',
    'network': '{{ vlan }}'
  }
}"

# Mixture of the block and the inline format - this results to string
networks: "{
  '{{ net }}':
    'gateway': '{{ gateway }}',
    'network': '{{ vlan }}'
}"

@Yannick91
Copy link

As I explain in my thread I bump on the exact same issue (#41096).
The only way around this to use vars is by using the shell module and put variables in that command (curl command for example)

However, I fixed it for now with fix templates (for each environment) that I import as tasks in my main playbook.

Kind regards,

Yannick

@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 feature This issue/PR relates to a feature request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jinja templating applied inconsistently
8 participants