Ansible troubleshooting - user module password_expiry_min bug and workaround
By Luca Berton · Published 2024-01-01 · Category: installation
Join Luca Berton on Ansible Pilot as we troubleshoot the user module bug, exploring effective workarounds through live Playbooknstrations.

Introduction
Today we're going to talk about Ansible troubleshooting, specifically about the user module bug and possible workaround. I'm Luca Berton and welcome to today's episode of Ansible PilotSee also: Ansible Fix 'Missing sudo Password' Error: Become Configuration
Demo
The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the user module bug triage and possible workaround!
error code
• userbug_error.yml---
- name: user module Playbook
hosts: all
become: true
vars:
myuser: "example"
tasks:
- name: password expiration
ansible.builtin.user:
name: "{{ myuser }}"
password_expire_min: 7
password_expire_max: 90
error execution
$ ansible-playbook -i Playbook/inventory troubleshooting/userbug_error.yml.yml
PLAY [user module Playbook] **********************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [demo.example.com]
TASK [password expiration] *******************************************************************************
changed: [demo.example.com]
PLAY RECAP ***********************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $ ansible-playbook -i Playbook/inventory user\ expiration/user.yml
PLAY [user module Playbook] **********************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [demo.example.com]
TASK [password expiration] *******************************************************************************
ok: [demo.example.com]
PLAY RECAP ***********************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $ ssh devops@demo.example.com
Last login: Mon Nov 8 17:09:16 2021 from 192.168.43.5
[devops@demo ~]$ sudo su
[root@demo devops]# chage -l example
Last password change : Nov 08, 2021
Password expires : Feb 06, 2022
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 7
We expected a 7 value for Minimum number of days between password change but we obtain 0.
Troubleshoot
• bug report - user module can't handle password expiration parameters correctly #75017 • pull request - user module password expiration fixes #75390workaround
• userbug_workaround.yml---
- name: user module Playbook
hosts: all
become: true
vars:
myuser: "example"
tasks:
- name: password min expiration
ansible.builtin.user:
name: "{{ myuser }}"
password_expire_min: 7
- name: password max expiration
ansible.builtin.user:
name: "{{ myuser }}"
password_expire_max: 90
workaround execution
$ ansible-playbook -i Playbook/inventory troubleshooting/userbug_workaround.yml
PLAY [user module Playbook] **********************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [demo.example.com]
TASK [password min expiration] ***************************************************************************
changed: [demo.example.com]
TASK [password max expiration] ***************************************************************************
ok: [demo.example.com]
PLAY RECAP ***********************************************************************************************
demo.example.com : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $ ssh devops@demo.example.com
Last login: Wed Nov 10 10:38:59 2021 from 192.168.43.5
[devops@demo ~]$ sudo su
[root@demo devops]# chage -l example
Last password change : Nov 08, 2021
Password expires : Feb 06, 2022
Password inactive : never
Account expires : never
Minimum number of days between password change : 7
Maximum number of days between password change : 90
Number of days of warning before password expires : 7
Conclusion
Now you know better how to troubleshoot the Ansible user module bug.See also: Ansible 'Missing Required Arguments' Error: Fix Missing Module Parameters
Related Articles
• using become for sudo in Ansible • static and dynamic Ansible inventory • understanding Ansible rolesWorkaround Summary
If you encounter the password_expiry_min bug, use one of these approaches:
Option 1: Use chage Command Instead
- name: Create user without password_expiry_min
ansible.builtin.user:
name: "{{ username }}"
state: present
password: "{{ vault_user_password | password_hash('sha512') }}"
- name: Set password expiry with chage
ansible.builtin.command: >
chage --mindays {{ password_min_days }}
--maxdays {{ password_max_days }}
--warndays {{ password_warn_days }}
{{ username }}
changed_when: true
Option 2: Pin to a Fixed Ansible Version
# Use a version where the bug is fixed
pip install ansible-core==2.14.0
Option 3: Use Ansible Vault for All User Passwords
- name: Manage users securely
ansible.builtin.user:
name: "{{ item.name }}"
password: "{{ item.password | password_hash('sha512') }}"
state: present
groups: "{{ item.groups | default(omit) }}"
shell: "{{ item.shell | default('/bin/bash') }}"
loop: "{{ vault_user_list }}"
no_log: true
See also: Ansible troubleshooting - AWS Failed to import the required Python library (botocore or boto3)
FAQ
Has this bug been fixed?
Check the Ansible GitHub issues for the current status of the password_expiry_min parameter bug. Pin to a version where it works correctly for your use case.
How do I report Ansible module bugs?
File an issue at https://github.com/ansible/ansible/issues with the Ansible version, OS, module name, minimal reproduction playbook, and full error output with -vvv.
What is the difference between password_expiry_min and password_expiry_max?
password_expiry_min sets the minimum number of days between password changes. password_expiry_max sets the maximum number of days before a password must be changed. Both map to chage settings on Linux.
Category: installation
Watch the video: Ansible troubleshooting - user module password_expiry_min bug and workaround — Video Tutorial