带有时间戳的Ansible模板

Ale*_*van 4 ansible ansible-playbook

我试图找出是否有方法在Ansible中为模板文件添加时间戳,这样每次我运行使用某个模板的Ansible playbook时,它会在目标文件中添加执行playbook的时间.

例如,我正在使用Ansible模板配置文件,我希望它在目标计算机上显示第一行的时间戳...

例如

cat something.conf_template
some config lines
Run Code Online (Sandbox Code Playgroud)

模板化后:

- template: src=/mytemplates/something.conf_template dest=/etc/something.conf owner=smth group=smth mode=0644
Run Code Online (Sandbox Code Playgroud)

内容应该是

cat something.conf
#in this comment is the timestamp, format irrelevant
some config lines
Run Code Online (Sandbox Code Playgroud)

你知道任何可以做到这一点的ansible模块吗?

Bru*_*e P 6

您只需添加# {{ ansible_managed }}到模板的顶部即可.此变量在配置文件中定义为默认值:

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

请注意,由于默认情况下它包含时间戳,因此每次调用模板任务时都会使用当前时间戳更新目标文件.

  • 考虑使用散列前缀更新您的答案,以确保该行不被解释为配置.在这种情况下,`#{{ansible_managed}}` (2认同)
  • 也可以使用`{{ ansible_management | 注释 }}`,因为这适用于多行 ansible_management 并且 `# {{ ... }}` 将仅注释第一行 (2认同)