使用ansible打印在YAML文件中缩进的变量值

Printing a variable value indented in a YAML file using ansible

我正在使用 Ansible 生成 Behat 配置文件。这个配置文件是一个 YAML 文件。我正在使用这样的 Jinja2 模板:

default:
  paths:
    features: '../all/tests/features'
  filters:
    tags: "~@api&&~@drush"
  extensions:
    Behat\MinkExtension\Extension:
    files_path: '{{ project_docroot }}/sites/all/tests/files'
      files_path: '{{ project_docroot }}'
      goutte: ~
      selenium2: ~
      base_url: '{{ base_url }}'
    Drupal\DrupalExtension\Extension:
      blackbox: ~
      drush_driver: "drush"
      drush:
        root: "{{ project_docroot }}"
      api_driver: "drupal"
      drupal:
        drupal_root: "{{ project_docroot }}"
      region_map:
{{ project_behat_region_map }}
      selectors:
{{ project_behat_selectors }}

以及以下定义的变量:

project_behat_region_map: |
        content: "#content"
        footer: "#footer"
        header: "#header"
        header bottom: "#header-bottom"
        navigation: "#navigation"
        highlighted: "#highlighted"
        help: "#help"
        bottom: "#bottom"

project_behat_selectors: |
        message_selector: '.messages'
        error_message_selector: '.messages.error'
        success_message_selector: '.messages.status'
        warning_message_selector: '.messages.warning'

如您所见,变量值是缩进的,但是当粘贴到 Jinja2 模板中时,丢失的缩进:

default:
  paths:
    features: '../all/tests/features'
  filters:
    tags: "~@api&&~@drush"
  extensions:
    Behat\MinkExtension\Extension:
    files_path: '/var/www//bacteriemias/docroot/sites/all/tests/files'
      files_path: '/var/www//bacteriemias/docroot'
      goutte: ~
      selenium2: ~
      base_url: 'http://bacteriemias.me'
    Drupal\DrupalExtension\Extension:
      blackbox: ~
      drush_driver: "drush"
      drush:
        root: "/var/www//bacteriemias/docroot"
      api_driver: "drupal"
      drupal:
        drupal_root: "/var/www//bacteriemias/docroot"
      region_map:
content: "#content"
footer: "#footer"
header: "#header"
header bottom: "#header-bottom"
navigation: "#navigation"
highlighted: "#highlighted"
help: "#help"
bottom: "#bottom"


      selectors:
message_selector: '.messages'
error_message_selector: '.messages.error'
success_message_selector: '.messages.status'
warning_message_selector: '.messages.warning'

这不是有效的 YAML。如何在 Jinja2 中打印带缩进的变量?

原来可以使用indent Jinja2 filter解决问题。

indent(s, width=4, indent=False)

Return a copy of the passed string, each line indented by 4 spaces. The first line is not indented. If you want to change the number of spaces or indent the first line too you can pass additional parameters to the filter:

{{ mytext|indent(2, true) }} indent by two spaces and indent the first line too.

所以,在我的例子中是:

default:
  paths:
    features: '../all/tests/features'
  filters:
    tags: "~@api&&~@drush"
  extensions:
    Behat\MinkExtension\Extension:
    files_path: '{{ project_docroot }}/sites/all/tests/files'
      files_path: '{{ project_docroot }}'
      goutte: ~
      selenium2: ~
      base_url: '{{ base_url }}'
    Drupal\DrupalExtension\Extension:
      blackbox: ~
      drush_driver: "drush"
      drush:
        root: "{{ project_docroot }}"
      api_driver: "drupal"
      drupal:
        drupal_root: "{{ project_docroot }}"
      region_map:
{{ project_behat_region_map | indent( width=8, indent=True) }}

      selectors:
{{ project_behat_selectors | indent( width=8, indent=True) }}

indent(s, width=4, indent=False)

2022 年 5 月更新

Jinja 2.10 之前 indent parameter was called indentfirst.

我已将代码更新为新名称。