-
-
Notifications
You must be signed in to change notification settings - Fork 607
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
Avoid loop.first when working with users and vault_users data #729
Conversation
I've got the same problem. Thanks for the PR, would be great to see it merged soon. 👍 |
Excellent debugging 👍 We might want to look into creating more custom filters/modules to deal with complex things like these. I'd have a little more confidence in them being pure python vs a mix of Jinja filters + Python functions. |
After wrestling to simplify, trying many different options, I decided to revert my original commit, then add a commit to just remove the instances of
In addition to removing
That moment of definition doesn't fail, but the templating failure occurs later, when Ansible uses the
@swalkinshaw mentioned this:
As a viable alternative to the commit described above, I created a
It's more code, thus more liability, but it simplifies some things. Example from the "Setup users" task: - password: '{% for user in vault_users | default([]) if user.name == item.name and user.password is defined %}{{ user.password | password_hash("sha512", user.salt | default("") | truncate(16, true, "") | regex_replace("[^\.\/a-zA-Z0-9]", "x")) }}{% else %}{{ None }}{% endfor %}'
+ password: "{{ trellis_users[item.name].password_hash }}" Let me know if you'd prefer this |
Let's just go with what you have now. The 👍 |
Fixes 'variable referenced before assignment in enclosing scope' error that appeared with python 2.7.12 and its apparent change in handling the {% if loop.first %} jinja control structure.
This reverts commit 70411af.
06ce3a3
to
2597b55
Compare
Fixes 'variable referenced before assignment in enclosing scope' error that appeared with python 2.7.12 and its apparent change in handling the {% if loop.first %} jinja control structure.
2597b55
to
0425db5
Compare
Fixes
variable referenced before assignment in enclosing scope
error reported in https://discourse.roots.io/t/8488/. Python 2.7.12 seems to handle the{% if loop.first %}
jinja control structure differently than 2.7.11. Examples ofloop.first
in Trellis: first, second, third.loop.first
This PR removes use of
{% if loop.first %}
, which was used in the context of retrieving userpassword
fromvault_users
(a list) orgroups
fromusers
(a list). To retrieve a specific user from the list, the code looped over all users, selecting the user with the correctname
.The purpose of
loop.first
in this looping was to only retrieve one value per user in case someone had naively listed a particular user multiple times inusers
orvault_users
. However, this use ofloop.first
causes the error above with in python 2.7.12.There is no failure if the instances of
{% if loop.first %}
are removed, but then there would be an error in the rare case that someone naively duplicates a user in these lists. So, this PR takes a different approach, avoiding{% ... %}
blocks as much as possible.salt
optionalThis PR also fixes a templating error that would have occurred if
salt
(fromvault_users
) were undefined. Password creation in the Setup users task will no longer fail if someone omits thesalt
.first
filterThe
first
filter throws an error if applied to an empty list. Some variables are defined in two steps to avoid such an error.The revised definition of
ansible_become_pass
is split in two assignments. First thepasswords
temporary variable is created. Thenpasswords | ternary(passwords | first, None)
only applies thefirst
filter ifpasswords
is not an empty list. (The temporarypasswords
list variable would be empty if no password were defined foradmin_user
invault_users
.)The other example is that
user_secrets_list
gathers the list of passwords and salts for a user. This could be an empty list if the user has no secrets invault_users
, or there could be multiple secrets for users who are duplicated invault_users
. Theuser_secrets
variable only applies thefirst
filter if theuser_secrets_list
is not empty.