yaml 中的文本块和转义字符

Text block in yaml and escaping characters

我收到以下 saltstack YAML 错误:

Rendering SLS 'openstack:openstack.horizon.CentOS' failed: could not found expected ':'; line 63

horizon_https:
  file.prepend:
    - text: |-
      <VirtualHost *:80>
      ServerName openstack.example.com    <======================
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
      </IfModule>

对于 YAML 的以下位:

horizon_https:
  file.prepend:
    - text: |-
      <VirtualHost *:80>
      ServerName openstack.example.com
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
      </IfModule>
      <IfModule !mod_rewrite.c>
      RedirectPermanent / https://openstack.example.com
      </IfModule>
      </VirtualHost>
      <VirtualHost *:443>
      ServerName openstack.example.com

      SSLEngine On
      # Remember to replace certificates and keys with valid paths in your environment
      SSLCertificateFile /etc/apache2/SSL/openstack.example.com.crt
      SSLCACertificateFile /etc/apache2/SSL/openstack.example.com.crt
      SSLCertificateKeyFile /etc/apache2/SSL/openstack.example.com.key
      SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

      # HTTP Strict Transport Security (HSTS) enforces that all communications
      # with a server go over SSL. This mitigates the threat from attacks such
      # as SSL-Strip which replaces links on the wire, stripping away https prefixes
      # and potentially allowing an attacker to view confidential information on the
      # wire
      Header add Strict-Transport-Security "max-age=15768000"

知道问题出在哪里吗?

text 是一个映射键,字符串 <VirtualHost *:80>\nServername ... 是它的值。该值不能缩进与键相同的级别。

所以你必须做:

horizon_https:
  file.prepend:
    - text: |-
        <VirtualHost *:80>
        ServerName openstack.example.com
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
        </IfModule>

错误消息抱怨无法找到的冒号是在指示的行中缺少的“:”。由于它在 text 映射键的同一级别缩进,因此它希望它包含一个键,后跟冒号 +space。

(这并不能解决 找不到 错误消息的不合语法性,但至少应该摆脱它)