ansible stat check last modified time - linux

I have a situation where I need to check the last modified date for a file using ansible. Normally stat in linux has the properties for the file like Modified,access and changed. I know p.stat.isdir and p.stat.pw_name exist but do we have a similar option using ansible stat to check last modified date of a file?

Ansible has a module with the same name stat.
Its return values contain mtime which is what you are looking for.
mtime
    Time of last modification
    Returned: success, path exists and user can read stats
Source: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html#return-stat/mtime

Related

How to take backup of file without changing its time-stamp with Ansible playbook

How to take backup of file without changing its time-stamp with Ansible playbook? I tried backup=yes but the problem is like it changes the timestamp os the file.
Code:- dest={{item}} state=absent regexp='TLSv1' backup=yes with_items: ('{{certs_dir.stdout_lines}}')
I'm retracting my initial "It can't be done" response - it should be possible by using a series of plays, but it's not very pretty.
If you really need the backup file to keep the time-stamp, you might want to put in an official request on the developer mailing list.
Use the stat module on the initial file to retrieve the file timestamp
Register the backup file name in the return value backup_file from the file or copy module.
Use the command module to call the touch command to set the time of the backup_file to the original time. (The Ansible stat module does not adjust file timestamps.)

Creating alias in Linux to a file or directory whose path changes with time

I want to create an alias to a specific log file in Linux. The only issue is the path to the file has a directory with time stamp. Every time this file is created (every time I run a script), its path changes because of time stamp. Here is an example:
$OUT_HOME/logs/misc/2017-03-20-11-23-24-3541-machine_name/commands/logfile.txt
Is there a smart way to create an alias to this file?
If this folder with changing name is created by running script in which you want to create those symlink, then simply changing part assign to some variable which will be used during folder creation and symlink creation.
You could try somethink like that :
alias my_alias=$(echo $OUT_HOME/logs/misc/$(date +"%Y-%m-%d-%H-%M-%S-%4N")-machine_name/commands/logfile.txt)

Node.js file rotation

I have a process that periodically gets files from a server and copy them with SFTP to a local directory. It should not overwrite the file if it already exists. I know with something like Winston I can automatically rotate the log file when it fills up, but in this case I need a similar functionality to rotate files if they already exist.
An example:
The routine copies a remote file called testfile.txt to a local directory. The next time it's run the same remote file is found and copied. But now I want to rename the first testfile.txt to testfile.txt.0 so it's not overwritten. And so on - after a while I'd have a directory of files with the name testfile.txt.N and the most recent testfile.txt.
What you can do is you can append date and time on the file name that gives every filename a unique name and also helps you archive it.
For example you text.txt can be either 20170202_181921_test.txt or test_20170202_181921.txt
You can use a JavaScript Date Object to get date and time.
P.S show your code of downloading files so that I can add more to that.

How does modifying a directory affect its Parent Directory timestamp in Linux?

For example, if I create a directory, check its timestamp, then create a directory inside the first directory a minute later, the timestamp for the mtime will change to when the new directory was created. However if I wait another minute and create a third directory inside the second, the second directory will update the mtime to the third directory's time while the first directory will still have the 2nd directory's original mtime. Despite the contents of the first directory changing when the third directory is created, the mtime doesn't change.
Is there documentation as to how linux looks to change mtime? Or is it only able to see direct children changes to update mtime?
This is the closest I could find. From the stat man page:
The field st_mtime is changed by file modifications, for example, by
mknod(2), truncate(2), utime(2) and write(2) (of more than zero
bytes). Moreover, st_mtime of a directory is changed by the creation
or deletion of files in that directory. The st_mtime field is not
changed for changes in owner, group, hard link count, or mode.
-Source
This wording implies to me that a modification in mtime of a nested directory is not considered a modification by the parent directory, since it does not qualify as a mknod, truncate, utime, or write operation.
Hope this helps!

why the file's Date Modified attribute changed,But I'm sure the file is not modified at all

I have a little script to create a directory listing
You can use it like
./createDiectoryListing.sh SOME_DIRECTORY
It will create a index.html show the list of the file, Here is an example
But i found sometime the file's date modified attribute changed to the time i ran the script.
In my script, i just use md5sum to caculate the hash of the file, I never change the content.
It's really annoying, all the file has the same modified date,after i ran the script
I don't know why, anyone can help me out thanks.
Okay. I figured it out. the reason is i use git to manage the file. when switch between branches, the date attribute is changed.

Resources