Ansible Cron作业无法逃脱百分号“%”

时间:2019-03-15 04:31:25

标签: python linux bash cron ansible

尝试创建cron作业以收集dpkg.logs并将其发送到s3存储桶。 任务流程如下:

- name: Configure cron job to export patch logs
  cron:
    name: export patch logs daily
    minute: 0
    hour: 0
    user: root
    cron_file: patch_logs
    job: "/usr/local/bin/aws s3 cp /var/log/dpkg.log s3://'{{ patch_logs_bucket }}'/dpkg.log.$(hostname).$(date +\%F)"

但是,在man crontab中描述的ss,必须转义百分号。这就是为什么在百分号前会看到一个反斜杠。

man (5) crontab:

Percent-signs (%) in the command, unless escaped with backslash (\), 
will be changed into newline characters, and all data after the 
first % will be sent to the command as standard input.

问题是,Ansible无法执行任务:

--> Action: 'converge'
ERROR! Syntax Error while loading YAML.
  found unknown escape character '%'

The error appears to have been in '/Users/<user>/<directory>/<to>/<ansible_project>/tasks/base.yml': line 132, column 115, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    cron_file: patch_logs
    job: "/usr/local/bin/aws s3 cp /var/log/dpkg.log s3://'{{ patch_logs_bucket }}'/dpkg.log.$(hostname).$(date +\%F)"
                                                                                                                  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我试图忽略百分号的转义,只是将其保留为$(date +%F),但是cron作业创建错误。 有什么想法吗?!

1 个答案:

答案 0 :(得分:0)

尝试逃避反冲

\\%F