-2

I am very new to Ansible (still learning).

We have a script to create new a user, provide sudo access, define a password to be changed on first login, user details, account expiry date and then define password expiry information:

---
- name: Create user with key
  hosts: all
  user: ansible
  sudo: True

  vars:

    password: $6$VhuG16emtCgF0zrK$dzE8swYNnLQeUkdgcgpTqr0stY8enepxcske8ATLAERjORpKkqYcGD6r6ssTNe4EdwwRs2UjjiAM/8tWCeEnN/
    raw: Password@1

  tasks:

  - name: Create New User
    user: name=TestUser comment="Test User" createhome=yes groups=wheel state=present password={{ password }}
  - command: chage -E 2023-10-18 TestUser
  - command: chage -d 0 TestUser
  - command: chage -M 120 TestUser
    register: newuser

We would define the hosts in the playbook command using -i and then name of the host file (which contains the IPs of the machines the users account will be create on) such as:

ansible-playbook create-newuser1.yml -i hosts.servers

Currently to extend the users account we have to go to each host and extend the account locally:

sudo chage -E 2023-12-28 TestUser

This may sound like a straight forward question to an "expert" in Ansible but I am unable to find information on how to use a similar script above (I have tried tweaking the script above and failed) to extend the existing user accounts expiration date and define the hosts either in a file or in the playbook command.

Tried searching user module, unable to find anything. Also had quick glance in Managing user accounts using Ansible playbooks.

Still struggling.

UPDATE 1

Just had a brain wave would I just need to create a yml file (named: extend-TestUser.yml) stating:

- command: chage -E 2023-12-28 TestUser

Then state this command:

ansible-playbook extend-TestUser.yml -i hosts.servers
U880D
  • 8,601
  • 6
  • 24
  • 40
S.Mahmood
  • 129
  • 11

1 Answers1

3

You may have a look into the documentation user module – Manage user accounts and a minimal example playbook of

---
- hosts: test
  become: true
  gather_facts: false

  vars:

    PASSWORD: 'Password@1'

  tasks:

  - name: Gather date and time only
    setup:
      gather_subset:
        - "date_time"
        - "!min"
    when: ansible_date_time is not defined

  - name: Configure or update user
    user:
      name: "TestUser"
      password: "{{ PASSWORD | password_hash('sha512') }}"
      comment: "Test User"
      createhome: true
      group: "DefaultGroup"
      groups: wheel
      state: present
      expires: "{{ '%s' | strftime( (ansible_date_time.epoch | int) + (86400 * 120)  ) }}"

It shows how to extend the expiration date from the current day by 120 days and or set a new password.

Thanks To

U880D
  • 8,601
  • 6
  • 24
  • 40