In Ansible, how to specify current date plus 2 year in epoch? - ansible

I am working on writing a playbook to add a large number of users each semester for my college. I would like to set up these accounts to expire automatically two years after the playbook is run. I know I can specify expiration in epoch...but how do I look up the epoch for a future date in the midst of a playbook run?

Let’s say your target date is two years from tomorrow. Then you can use most any system’s date command to get its epoch value:
date -d '2015-09-22 + 2 years' '+%s'
Then in Ansible, you can register that date you’re targeting with something like (not tested):
- name: register end of semester expiry
command: date -d '2015-09-22 + 2 years' '+%s'
register: expiry_r
- name: create user accts with expiry
user: ... expires={{expiry_r.stdout}}

Related

Shell: How to calculate current MMYY to previous date (-15 months)

I am trying to calculate the current date MMYY - 15 months ago.
I get current date in the format I want with shell command: date +"%m%y"
I am trying to use this current date and calculate what the MMYY is 15 months ago, what command would show me this?
Not sure why you want to get the current MMYY and calculate "15 months ago" afterwards. You could just do it in one command:
date --date='15 months ago' +"%m%y"
man date and info date are your friend.

Best way to compare Timestamps in Linux shell/bash script? [duplicate]

This question already has answers here:
Bash script compare two date variables [duplicate]
(5 answers)
Closed 1 year ago.
I have to read a epoch timestamp (in seconds) from a directory /usr/local/healthcheck.txt on my Red Hate Enterprise Linux machine every ~10 minutes (polling). I need perform a comparison on the time to check if the timestamp in the healthcheck.txt file is OLDER than 50 minutes from the current time/timestamp OR if the healthcheck.txt is non-existent, to throw an error. The timestamp in the healthcheck.txt file generally looks like this (its in seconds, as stated above) :
1591783065
I was using date -d #1591783065 to convert the timestamp to Human Readable and get something like this:
Tue Jun 9 16:22:57 UTC 2020
What would be the best approach to compare the current timestamp to this timestamp in the file and check if its older than 50 minutes?
In Java , we have a Date package , and can just use compareTo to compare the times/dates, is there a simple way to do this with shell/bash scripts?
Why don't you stick with epoch-time? You can get the current time as seconds since epoch by
date +%s, so you just have to compare
if (( (healthcheck_time + 50*60) < $(date +%s) ))
then
# .... healthcheck older than 50 minutes
fi

Subtract 20 month from user provided date in yymm in unix

oldest_year_month_temp=201602
NUM_PART_RETAIN=20
oldest_year_month=`date --date="$(oldest_year_month_temp +%Y%m) - $NUM_PART_RETAIN month" "+%Y%m"`
Date is not coming as expected.
One easy way to do it would be to simply append a 01 to your input of yymm to provide a format date -d could read as the starting date, then simply subtract 20 months and output the resulting date in %y%m format. For example, if you provide the date 9910 (Oct. 1999), you can do:
$ date -d "991001 - 20 months" +%y%m
9802
Which returns Feb. 1998 (20 months earlier)
(note: the $ above just indicates a command by a normal user as opposed to # indicating a command by the super user (e.g. root))
Inside the $(...) there must be a command, e.g. $(date ...).
This should have been obvious from the error message you got, which was probably oldest_year_month_temp: no such command.
When reading from a variable, you must write a $ before its name.

Deriving URLs from date

I would like to automate a download of an image from a third party server with a CRON job and then upload the image to my website.
I have 2 issues:
First, the third party site changes the image name every day using the following logic:
http://thirdpartysite.com/ImageFinder.aspx?ReportID=FILENAME
where FILENAME is 26601 +14 for each day after 6 Oct 2014 (so 7 Oct would be 26615, 8 Oct would be 26629 etc).
How do I build this into a simple Linux bash script for use with wget?
Second, how do I upload this to my site via FTP (or similar) with the same script.
NOTE: I have permission to host the file on my site and have linked the original site / placed credit for the image.
Following the suggestions of #Abhay, first get the timestamp of Oct 6, let's store it in the variable $d0:
d0=$(date +%s -d 20141006)
Then store the timestamp of a target date, say Oct 8 and store it in $d1:
d1=$(date +%s -d 20141008)
Then you can calculate the difference and apply the required arithmetic operations in $((...)), like this:
echo $((26601 + 14 * (d1 - d0) / 60 / 60 / 24))
# outputs: 26629
The date command has one very good format: %s, which prints number of seconds since "epoch", which is the fixed date 1 January 1970, 00:00 UTC. I'll call it "timestamp". In combination with this, you can use the -d date-string, so that it prints the given date as number of seconds. Now you can take today's timestamp, subtract the timestamp of "6 Oct 2014" from it, and you get number of seconds between the two times. Now you can divide it by (60 * 60 * 24) to get it in number of days, and do further arithmetic to get the desired number, and make a file name out of it.
The date string formats that -d option takes are flexible, but as of now I am not sure whether it takes "6 Oct 2014" as is. Try a few permutations, or better, check the "info" page.

Create directories based on month name

I'm looking for a way to create the following structure. (starting from a set year, until the current year)
eg :
2008
- January
- Feburary
- ...
- December
2009
- January
- Feburary
- ...
- December
I've got some basic bash skills, but have not figured out the part on how to get the full month names and use those to create these structures.
Thank you.
A straightforward answer:
mkdir -p {2008..2013}/{January,February,March,April,May,June,July,August,September,October,November,December}
If the starting year is user-determined, e.g., in a variable:
starting_year=2008
current_year=$(date +%Y)
for ((y=starting_year;y<=current_year;++y)); do
mkdir -p "$y"/{January,February,March,April,May,June,July,August,September,October,November,December}
done

Resources