Making changes to user account in linux server with ansible

Viewed 223

I am trying set user accounts to expired using ansible playbook. Unfortunately, ansible creates the user and expires it if the user is not present, with following code. How do I make changes only to user account account that is present?

- name: Lock accounts
  when: ansible_os_family != "AIX"
  user:
    name: "{{ item }}"
    expires: 1541453077
  with_items: "{{ users }}"
1 Answers

Example tasks/main.yml:

- name: Check if user exists
  command: getent passwd {{user}}
  register: user_on_board
  ignore_errors: yes # continue with the playbook
- name: Lock accounts
  when: ansible_os_family != "AIX"
  user:
    name: "{{ item }}"
    expires: 1541453077
  when: user_on_board.rc == 0

Example playbook :

- include_tasks: tasks/main.yml
  with_items: "{{ users }}"
  loop_control:
    loop_var: user
Related