Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a variable value indented in a YAML file using ansible

I'm generating a Behat config file using Ansible. This configuration file is a YAML file. I'm using a Jinja2 template like this:

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

And the following defined vars:

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'

As you can see the variable values are indented, but when pasted into the Jinja2 template the lost indentation:

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'

This is not valid YAML. How can I print a variable with indentation in Jinja2?

like image 937
sanzante Avatar asked Nov 30 '15 17:11

sanzante


People also ask

How do you indent YAML?

The suggested syntax for YAML files is to use 2 spaces for indentation, but YAML will follow whatever indentation system that the individual file uses. Indentation of two spaces works very well for SLS files given the fact that the data is uniform and not deeply nested.

Do indents matter in YAML files?

Indentation is meaningful in YAML. Make sure that you use spaces, rather than tab characters, to indent sections. In the default configuration files and in all the examples in the documentation, we use 2 spaces per indentation level. We recommend you do the same.

What does indent mean in YAML?

In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line. To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently.

Does YAML support variables?

In YAML, you can only define variables with pipeline or action scope. Workspace and project variables need to be defined via the GUI.


1 Answers

It turns out that the problem can be solved using the indent Jinja2 filter.

indent(s, width=4, indentfirst=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.

So, in my case is:

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, indentfirst=True) }}

      selectors:
{{ project_behat_selectors | indent( width=8, indentfirst=True) }}
like image 164
sanzante Avatar answered Oct 16 '22 19:10

sanzante