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

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.)

Related

Generate hash of newly downloaded file

I'd like my bash script to perform an action every time new file is downloaded to /Downloads (generate hash of downloaded file and send it to API). So far I've been trying to make use of "inotify-tools", but it works only for newly created file and that won't do.
Script should work like this:
I download a file via browser (normal way)
Script notices new file and is executed automatically
Thanks in advance for help :D
You can use /etc/crontab to check ~/Downloads folder at startup and every n minutes. Script that will run every nth minute can do either
Keep the number of files. If number decreases script updates cache. And if number increases then gets the latest created file (or modified) and sends that file's hash to the api via curl.
Keep the name of files. If a file no longer exists, script then updates the cache of file names. If a new file appears again hashes and sends hash to the api via curl.
You can keep cache of files under /tmp.
If you can provide an example scenario I can write a simple script

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.

ansible stat check last modified time

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

How can I use Ansible lineinfile to remove all but a few specific lines?

I'm attempting to ensure all but a few specific lines are removed from a file. I can get halfway there with the following task.
- name: ensure only the correct lines are present
lineinfile: dest=/path/to/file
regexp="pattern1|pattern2[0-9]*|pattern3[0-9]*"
state=present
backup=yes
Ultimately I want to ensure that pattern1, pattern2[0-9]*, and pattern3[0-9]* are the only lines that remain in this file. I've attempted to come up with a regex that negates this one and then specify state=absent but I've been unsuccessful so far.
If you want only specific lines in your file, why don't you just transfer that file with your desired lines to the remote server? You can use copy module to transfer that file as is or template module to dynamically substitute some variables inside or even assemble module to generate a file from some fragments(such as config).

Resources