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

WIP: Allow certain, simple Jinja values to be unquoted. #36331

Open
wants to merge 3 commits into
base: devel
from

Conversation

Projects
None yet
3 participants
@ingydotnet

ingydotnet commented Feb 16, 2018

This allows the following to be unquoted:

foo: {{ bar }}

The above is valid YAML but semantically means something like this:

{ "foo": { { "bar": null}: null} }

This patch looks for events of that form coming from the parser, and
reconstructs the key bar into a string node of value "{{ bar }}".

SUMMARY

You can test this with the simple files here: https://gist.github.com/anonymous/48b8744dcafd8f6e6bdac3803e77aa4b

This is just a proof of concept. It only works for simple values that happen to be valid YAML:

- {{ foo }} # works
- {{ foo | bar }} # works
- {{ foo[42] }} # not valid yaml
- {{ 'foo' }} # would be same as {{ foo }} 
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

YAML Loader

ANSIBLE VERSION

ADDITIONAL INFORMATION

Allow certain, simple jinja values to be unquoted.
This allows the following to be unquoted:
```
foo: {{ bar }}
```

The above is valid YAML but semantically means something like this:
```
{ "foo": { { "bar": null}: null} }
```

This patch looks for events of that form coming from the parser, and
reconstructs the key `bar` into a string node of value "{{ bar }}".

@ingydotnet ingydotnet changed the title from Allow certain, simple jinja values to be unquoted. to WIP: Allow certain, simple Jinja values to be unquoted. Feb 16, 2018

@ansibot

This comment has been minimized.

Contributor

ansibot commented Feb 16, 2018

The test ansible-test sanity --test pep8 [explain] failed with 2 errors:

lib/ansible/parsing/utils/yaml.py:53:13: E125 continuation line with same indent as next logical line
lib/ansible/parsing/utils/yaml.py:64:1: E302 expected 2 blank lines, found 1

click here for bot help

ingydotnet added some commits Feb 16, 2018

Handle unquoted Jinja string values properly
This works properly now:
```
unquoted: {{ foo }}
quoted: {{ 'foo' }}
```
Don't allow {{ 'foo' }} unquoted jinja value
The unquoted jinja feature only works when the unquoted value is valid
YAML.

Don't allow `foo: {{ 'bar' }}` when the internal value is a quoted
string. This form is dubious and probably never seen in the wild. We
just don't give it special treatment so it fails same as before.

Also added check to assert jinja key is a string.
@ingydotnet

This comment has been minimized.

ingydotnet commented Feb 18, 2018

I added a couple commits to solidify the logic.

The logic is:

  • If all of these are true:
    • YAML is valid
    • A node is a mapping with one pair
    • The pair's key is a mapping
    • The pair's value is null
    • The key mapping's key is an unquoted string
  • Then:
    • Load the node as that string with jinja double curlies around it
  • Else:
    • Use old/existing logic

The result is that certain unquoted Jinja values will work. If they don't work, they will simply fail in the same way they always did.

@ansibot

This comment has been minimized.

Contributor

ansibot commented Feb 18, 2018

The test ansible-test sanity --test pep8 [explain] failed with 3 errors:

lib/ansible/parsing/utils/yaml.py:53:13: E125 continuation line with same indent as next logical line
lib/ansible/parsing/utils/yaml.py:60:17: E125 continuation line with same indent as next logical line
lib/ansible/parsing/utils/yaml.py:67:1: E302 expected 2 blank lines, found 1

click here for bot help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment