Search code examples
variablesansiblequotesdouble-quotes

How to quote difficult and long vars in ansible?


I want to create some vars such as delay and size in ansible

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


Solution

  • 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

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

    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

    [WARNING]: Found variable using reserved name: delay
    

    Maybe another approach for you could be Templating or concatenate variable strings.