简体   繁体   English

Ansible cron 作业不会转义百分号 `%`

[英]Ansible cron job does not escape percent sign `%`

Trying to create a cron job to collect dpkg.logs and ship them to an s3 bucket.尝试创建一个 cron 作业来收集 dpkg.logs 并将它们发送到 s3 存储桶。 The task flows as below:任务流程如下:

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

But, ss described in man crontab, percent-signs must be escaped.但是,ss 在 man crontab 中描述,百分号必须被转义。 That is why you see a backslash before percent sign.这就是为什么您会在百分号前看到反斜杠。

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.

Problem is, Ansible fails to execute the task:问题是,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 }}"

I have tried to ignore the escape of percent and just leave it as $(date +%F) , but cron job is created wrongly.我试图忽略百分比的转义并将其保留为$(date +%F) ,但是错误地创建了 cron 作业。 Any ideas?!有任何想法吗?!

尝试逃避反斜杠

\\%F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM