[英]How to quote difficult and long vars in ansible?
I want to create some vars such as delay
and size
in ansible我想在 ansible 中创建一些变量,例如delay
和size
delay: */5 * * * * root stat --printf="\%Y\t\%n\n" /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_backup_delay{directory="\2"} \1/p' > /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ && mv /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ /var/lib/node_exporter/textfile_collector/backup_delay.prom
size: */5 * * * * root du -sb /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_directory_size_bytes{directory="\2"} \1/p' > /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ && mv /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ /var/lib/node_exporter/textfile_collector/directory_size.prom
But when i start playbook i receive an error但是当我开始剧本时我收到一个错误
PLAY [all] ***************************************************************************************************************************************************************************************************************************************************************
ERROR! Syntax Error while loading YAML.
did not find expected alphabetic or numeric character
The error appears to be in '/home/dude/gitlab.com/office-deploy/code/inventories/host_vars/main1/main.yml': line 1, column 9, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
delay: */5 * * * * root stat --printf="\%Y\t\%n\n" /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_backup_delay{directory="\2"} \1/p' > /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ && mv /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ /var/lib/node_exporter/textfile_collector/backup_delay.prom
^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote, make sure the
line ends with the same set of quotes. For instance this arbitrary
example:
foo: "bad" "wolf"
Could be written as:
foo: '"bad" "wolf"'
Can anyone say to me which quotes i should use?谁能告诉我应该使用哪些引号?
I use '''...''', ' "..." ' and nothing work我使用 '''...''', ' "..." ' 没有任何效果
According your description of the syntax problem and the comment about YAML multiline strings , I've created a small test with Block Scalar Style : folded
and Block Chomping : strip
根据您对语法问题的描述和关于YAML 多行字符串的评论,我用Block Scalar Style : folded
和Block Chomping : strip
创建了一个小测试
---
- hosts: localhost
become: false
gather_facts: false
vars:
DELAY: >-
*/5 * * * * root stat --printf="\%Y\t\%n\n" /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_backup_delay{directory="\2"} \1/p' > /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ && mv /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ /var/lib/node_exporter/textfile_collector/backup_delay.prom
SIZE: >-
*/5 * * * * root du -sb /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_directory_size_bytes{directory="\2"} \1/p' > /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ && mv /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ /var/lib/node_exporter/textfile_collector/directory_size.prom
tasks:
- name: Show var
debug:
var: DELAY
- name: Show msg
debug:
msg: "{{ SIZE }}"
resulting into an output of导致 output 的
TASK [Show var] ***************************************************************************************************************************************
ok: [localhost] =>
DELAY: '*/5 * * * * root stat --printf="\%Y\t\%n\n" /pool/slow/backup/daily/* | sed -ne ''s/^\([0-9]\+\)\t\(.*\)$/node_backup_delay{directory="\2"} \1/p'' > /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ && mv /var/lib/node_exporter/textfile_collector/backup_delay.prom.$$ /var/lib/node_exporter/textfile_collector/backup_delay.prom'
TASK [Show msg] ***************************************************************************************************************************************
ok: [localhost] =>
msg: '*/5 * * * * root du -sb /pool/slow/backup/daily/* | sed -ne ''s/^\([0-9]\+\)\t\(.*\)$/node_directory_size_bytes{directory="\2"} \1/p'' > /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ && mv /var/lib/node_exporter/textfile_collector/directory_size.prom.$$ /var/lib/node_exporter/textfile_collector/directory_size.prom'
Please take note that using delay
would result into a请注意,使用delay
会导致
[WARNING]: Found variable using reserved name: delay
Maybe another approach for you could be Templating or concatenate variable strings .也许您的另一种方法可能是模板化或连接变量字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.