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

templar: ensure that exceptions are handled, fix 'AttributeError' #48792

Merged
merged 2 commits into from
Nov 29, 2018

Conversation

pilou-
Copy link
Contributor

@pilou- pilou- commented Nov 16, 2018

SUMMARY

Fix AttributeError:

The full traceback is:
Traceback (most recent call last):
  File "ansible/lib/ansible/template/vars.py", line 106, in __getitem__
    value = self._templar.template(variable)
  File "ansible/lib/ansible/template/__init__.py", line 430, in template
    disable_lookups=disable_lookups,
  File "ansible/lib/ansible/template/__init__.py", line 678, in do_template
    res = j2_concat(rf)
  File "<template>", line 11, in root
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ansible/lib/ansible/executor/task_executor.py", line 144, in run
    res = self._execute()
  File "ansible/lib/ansible/executor/task_executor.py", line 535, in _execute
    self._task.post_validate(templar=templar)
  File "ansible/lib/ansible/playbook/task.py", line 271, in post_validate
    super(Task, self).post_validate(templar)
  File "ansible/lib/ansible/playbook/base.py", line 364, in post_validate
    value = templar.template(getattr(self, name))
  File "ansible/lib/ansible/template/__init__.py", line 475, in template
    disable_lookups=disable_lookups,
  File "ansible/lib/ansible/template/__init__.py", line 430, in template
    disable_lookups=disable_lookups,
  File "ansible/lib/ansible/template/__init__.py", line 678, in do_template
    res = j2_concat(rf)
  File "<template>", line 9, in root
  File "ansible/lib/ansible/template/__init__.py", line 236, in resolve_or_missing
    val = super(AnsibleContext, self).resolve_or_missing(key)
  File "lib/python3.7/site-packages/jinja2/runtime.py", line 217, in resolve_or_missing
    return resolve_or_missing(self, key)
  File "lib/python3.7/site-packages/jinja2/runtime.py", line 129, in resolve_or_missing
    return context.parent[key]
  File "ansible/lib/ansible/template/vars.py", line 110, in __getitem__
    msg = getattr(e, 'message') or to_native(e)
AttributeError: 'JSONDecodeError' object has no attribute 'message'

Unit test provided.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/template/vars.py

ADDITIONAL INFORMATION

Simple reproducer:

- hosts: localhost
  vars:
    not_json: "{{ 'test str' | from_json }}"
  tasks:
    - command: "echo {{ not_json }}"

@pilou- pilou- changed the title templar: ensure that exceptions are handled, fix 'AttributeError' WIP: templar: ensure that exceptions are handled, fix 'AttributeError' Nov 16, 2018
@ansibot
Copy link
Contributor

ansibot commented Nov 16, 2018

Hi @pilou-, thank you for submitting this pull-request!

click here for bot help

@ansibot ansibot added WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback. small_patch labels Nov 16, 2018
lib/ansible/template/vars.py Show resolved Hide resolved
'message' attribute is deprecated since Python 2.6 and not available
with Python 3.

Simple reproducer:

    - hosts: localhost
      vars:
        not_json: "{{ 'test str' | from_json }}"
      tasks:
        - command: "echo {{ not_json }}"
@pilou- pilou- force-pushed the fix_object_has_no_attribute_message branch from 7f34cd1 to 84c3ff8 Compare November 17, 2018 00:23
@pilou- pilou- changed the title WIP: templar: ensure that exceptions are handled, fix 'AttributeError' templar: ensure that exceptions are handled, fix 'AttributeError' Nov 17, 2018
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. core_review In order to be merged, this PR must follow the core review workflow. labels Nov 17, 2018
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Nov 20, 2018
@bcoca bcoca self-assigned this Nov 20, 2018
@sivel sivel mentioned this pull request Nov 21, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 26, 2018
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Nov 27, 2018
@bcoca bcoca merged commit 62c0503 into ansible:devel Nov 29, 2018
@gundalow
Copy link
Contributor

@pilou- You OK to backport + changelog this?

pilou- added a commit to pilou-/ansible that referenced this pull request Nov 30, 2018
…sible#48792)

* templar: ensure that exceptions are handled

* Fix AttributeError: object has no attribute 'message'

'message' attribute is deprecated since Python 2.6 and not available
with Python 3.

Simple reproducer:

    - hosts: localhost
      vars:
        not_json: "{{ 'test str' | from_json }}"
      tasks:
        - command: "echo {{ not_json }}"

(cherry picked from commit 62c0503)
@pilou-
Copy link
Contributor Author

pilou- commented Nov 30, 2018

@gundalow: backport PR created.

abadger pushed a commit that referenced this pull request Dec 4, 2018
…8792)

* templar: ensure that exceptions are handled

* Fix AttributeError: object has no attribute 'message'

'message' attribute is deprecated since Python 2.6 and not available
with Python 3.

Simple reproducer:

    - hosts: localhost
      vars:
        not_json: "{{ 'test str' | from_json }}"
      tasks:
        - command: "echo {{ not_json }}"

(cherry picked from commit 62c0503)
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. python3 small_patch support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants