I'm planning to create and run an ansible playbook (or a .sh script) that will detect the last user login, then if the login date is older than 30 days proceed to shutdown the server, otherwise stop the playbook.
My knowledge in ansible is basic and scripting too. :(
I'll execute this on my RHEL servers 6.x and 7.x
last | grep test
test pts/1 127.0.0.1 Mon Sep 9 10:53 - 10:53 (00:00)
Save this output, if the date is older than 30 days, shutdown the server. How could i set this output as a variable into the next command ?
if [ "$(date +%D)" != "30" ];then
shutdown -h now
fi
This is how i solved the problem
---
- name: Check last login
hosts: REDHAT
tasks:
- name: Last logged user
shell: last | grep myusername
register: output
ignore_errors: yes
- name: shutdown server
shell: shutdown -h now
become: yes
when: (output.stdout|int - ansible_date_time.epoch|int) < 2592000