Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Make node_exporter.service template compatible with both Python 2 and 3 #33

Merged
merged 1 commit into from
Apr 13, 2018

Conversation

nikosgraser
Copy link
Contributor

When using this role in an environment where the default python
interpreter is python3, the ansible run fails when jinja2 tries to
process templates/node_exporter.service.j2.

This is because in Python 3, dict.items() returns a view object
which can't be indexed directly (in Python 2, this returns a list).

Casting the result of c.items() explicitly to a list allows indexing
under Python 3 while not changing the outcome in Python 2.

When executed in a Python 3 environment, the order
in which the items are looped over might be different since "Keys and
values are listed in an arbitrary order which is non-random, varies
across Python implementations, and depends on the dictionary’s history
of insertions and deletions."
(https://docs.python.org/3.6/library/stdtypes.html#dict-views)

Otherwise, this change is functionally identical.

When using this role in an environment where the default python
interpreter is python3, the ansible run fails when jinja2 tries to
process `templates/node_exporter.service.j2`.

This is because in Python 3, `dict.items()` returns a *view object*
which can't be indexed directly (in Python 2, this returns a list).

Casting the result of `c.items()` explicitly to a list allows indexing
under Python 3 while not changing the outcome in Python 2.

When executed in a Python 3 environment, the order
in which the items are looped over might be different since "Keys and
values are listed in an arbitrary order which is non-random, varies
across Python implementations, and depends on the dictionary’s history
of insertions and deletions."
(https://docs.python.org/3.6/library/stdtypes.html#dict-views)

Otherwise, this change is functionally identical.
Copy link
Collaborator

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lock
Copy link

lock bot commented Mar 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Mar 24, 2019
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants