Python Ansible cron作业未转义百分号“%1”`
尝试创建一个cron作业来收集dpkg.log并将其发送到s3存储桶。 任务流程如下:Python Ansible cron作业未转义百分号“%1”`,python,linux,bash,cron,ansible,Python,Linux,Bash,Cron,Ansible,尝试创建一个cron作业来收集dpkg.log并将其发送到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.
- 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 }}"
-->操作:“converge”
错误!加载YAML时出现语法错误。
找到未知的转义字符“%”
错误似乎出现在“/Users/////tasks/base.yml”中:第132行第115列,但可能是
根据确切的语法问题,在文件中的其他位置。
令人不快的一行似乎是:
cron_文件:补丁_日志
作业:“/usr/local/bin/aws s3 cp/var/log/dpkg.log s3://{{patch\u logs\u bucket}}}'/dpkg.log.$(主机名)。$(日期+\%F)”
^这里
我们可能错了,但这一个看起来可能是一个问题
缺少引号。当模板表达式使用括号时,请始终引用括号
开始一个值。例如:
有以下项目:
-{{foo}}
应写为:
有以下项目:
-“{{foo}}”
我试图忽略百分比的转义,将其保留为$(date+%F)
,但cron作业创建错误。
有什么想法吗 尽量避开反斜杠
\\%F