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