Skip to content

Commit 0eaa5b6

Browse files
swalkinshawclaude
andauthored
Fix jinja2-in-conditional deprecation warning in users sudoer assert (#1675)
Ansible conditionals are already evaluated as Jinja2 expressions, so wrapping them in {{ }}/{% %} delimiters triggers a deprecation warning ("conditional statements should not include jinja2 templating delimiters") and is on a path to becoming a hard error. Rewrite the two `assert` conditions as delimiter-free filter expressions. `selectattr(...) | first | default(...)` reproduces the original `{% for ... %}...{% else %}...{% endfor %}` no-match handling. Fixes #1657 Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 0fed5dd commit 0eaa5b6

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

roles/users/tasks/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
- name: Fail if root login will be disabled but admin_user will not be a sudoer
1717
assert:
1818
that:
19-
- "{% for user in users if user.name == admin_user %}{{ 'sudo' in user.groups }}{% else %}{{ false }}{% endfor %}"
20-
- "{% for user in vault_users | default([]) if user.name == admin_user %}{{ user.password is defined }}{% else %}{{ false }}{% endfor %}"
19+
- "{{ 'sudo' in (users | selectattr('name', 'equalto', admin_user) | map(attribute='groups') | first | default([])) }}"
20+
- "{{ (vault_users | default([]) | selectattr('name', 'equalto', admin_user) | first | default({})).password is defined }}"
2121
msg: |
2222
When `sshd_permit_root_login: false`, you must add `sudo` to the `groups` for admin_user (in `users` hash), and set a password for admin_user in `vault_users` (in `group_vars/{{ env }}/vault.yml`). Otherwise Ansible could lose the ability to run the necessary sudo commands. {% if sudoer_passwords is defined or vault_sudoer_passwords is defined %}
2323

0 commit comments

Comments
 (0)