如何在ansible中引用困难和长的变量?

How to quote difficult and long vars in ansible?

我想在 ansible

中创建一些变量,例如 delaysize
delay: */5 * * * * root stat --printf="\%Y\t\%n\n" /pool/slow/backup/daily/* | sed -ne 's/^\([0-9]\+\)\t\(.*\)$/node_backup_delay{directory=""} /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=""} /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=""} /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"'

谁能告诉我应该使用哪些引语?

我使用 '''...'''、'"..."' 但没有任何效果

根据你对语法问题的描述和关于 YAML multiline strings 的评论,我用 Block Scalar Style 创建了一个小测试: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=""} /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=""} /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=""} /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=""} /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

也许另一种适合您的方法是 Templating or