提问人:a1dude 提问时间:1/21/2022 更新时间:1/21/2022 访问量:178
如何在 ansible 中引用困难和长的变量?
How to quote difficult and long vars 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
但是当我启动 playbook 时,我收到错误
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"'
谁能告诉我我应该使用哪些引号?
我使用 '''...''', ' “...”',但没有任何工作
答:
1赞
U880D
1/21/2022
#1
根据您对语法问题的描述和对 YAML 多行字符串的评论,我创建了一个带有块标量样式的小型测试: 和 块咀嚼:folded
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 }}"
导致输出
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'
请注意,使用将导致delay
[WARNING]: Found variable using reserved name: delay
评论