具有日期格式的 ansible 操作文件

yie*_*eld 9 format date ansible

在ansible中,我想操作组成或组成的文件/目录/档案,如下所示:

我该怎么做。Ansible 似乎无法处理这个问题。(我怀疑)。那么,我做错了什么?

前任:

- name: create file with a date in name
  file: path=/path/somefile.`date +%y_%m_%d`

- name: unzip a file
  unarchive: path=/path/zomezip.`date +%y_%m_%d`.tar.gz bla bla....
Run Code Online (Sandbox Code Playgroud)

cee*_*yoz 18

设置一个变量,然后将它与 Ansible 的Jinja2 模板系统一起使用(看起来您正在尝试使用点运算符和反引号来执行 PHP)

vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d-%H%M') }}"
tasks:
  - name: create file with a date in name
    file: path="/path/somefile{{ date }}"
Run Code Online (Sandbox Code Playgroud)

或者在模板中使用查找本身:

  - name: create file with a date in name
    file: path="/path/somefile{{ lookup('pipe', 'date +%Y%m%d-%H%M') }}"
Run Code Online (Sandbox Code Playgroud)


Dav*_*nic 14

从 2.4 开始,您还可以使用strftime过滤器 ( doc ):

# Display year-month-day
{{ '%Y-%m-%d' | strftime }}

# Display hour:min:sec
{{ '%H:%M:%S' | strftime }}

# Use ansible_date_time.epoch fact
{{ '%Y-%m-%d %H:%M:%S' | strftime(ansible_date_time.epoch) }}

# Use arbitrary epoch value
{{ '%Y-%m-%d' | strftime(0) }}          # => 1970-01-01
Run Code Online (Sandbox Code Playgroud)