Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible templates with timestamps

I'm trying to figure out if there's way of adding a timestamp to a template file in Ansible so that everytime I run an Ansible playbook which uses a certain template, it adds in the destination file the time of the execution of the playbook.

For example, I'm templating a configuration file with Ansible and I want it on the destination machine to appear with a timestamp on the first line...

e.g.

cat something.conf_template
some config lines

After templating:

- template: src=/mytemplates/something.conf_template dest=/etc/something.conf owner=smth group=smth mode=0644

the contents should be

cat something.conf
#in this comment is the timestamp, format irrelevant
some config lines

Do you know any ansible module that can do this?

like image 847
Alexandra Ivan Avatar asked Sep 30 '15 12:09

Alexandra Ivan


1 Answers

You can simply add # {{ ansible_managed }} to the top of your template. This variable is defined in your configuration file to have a default of:

Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}

Note that since this contains a timestamp by default it means that every time your template task is invoked it will update the destination file with the current timestamp.

like image 90
Bruce P Avatar answered Oct 14 '22 13:10

Bruce P