lock_and_expire_user.yaml 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ---
  2. - name: "Finds a single user imputted into a prompt when playbook is run, then locks and expires the user"
  3. hosts: all
  4. gather_facts: yes
  5. vars_prompt:
  6. - name: username
  7. prompt: "User name to find"
  8. private: false
  9. tasks:
  10. - getent:
  11. database: passwd
  12. split: ':'
  13. register: user_info_list
  14. - name: "Show user names"
  15. debug:
  16. msg: "{{ getent_passwd | dict2items | map(attribute='key') | list }}"
  17. - name: "Set user_present to TRUE if username is present in list"
  18. set_fact:
  19. user_present: true
  20. when: username in getent_passwd | dict2items | map(attribute='key') | list
  21. - name: "Return FALSE if username is present in list"
  22. set_fact:
  23. user_present: false
  24. when: username not in getent_passwd | dict2items | map(attribute='key') | list
  25. - name: "Debug: output value of user_present"
  26. debug:
  27. msg: "user_present is {{ user_present }}"
  28. - name: "Debug: output info from username"
  29. debug:
  30. msg: "{{ getent_passwd | dict2items | selectattr('key', 'equalto', username) | list }}"
  31. when: user_present is true
  32. - name: "Lock and Expire user"
  33. user:
  34. name: "{{ username }}"
  35. password_lock: true
  36. expires: 1
  37. when: user_present is true
  38. - name: "Debug: output info from username AFTER user is locked and expired"
  39. debug:
  40. msg: "{{ getent_passwd | dict2items | selectattr('key', 'equalto', username) | list }}"
  41. when: user_present is true