Elijah Lynn
Elijah Lynn

Reputation: 13468

How to write a multiline Ansible Jinja2 variable?

I have a Ansible line that fails linting:

 tags: "{{ deployment_id | resource_tags('asg', base_resource_tags, deployment=deployment_id, deployment_env=deployment_env, deployment_name=deployment_name, purpose=deployment_purpose_tag, cpu_utilization=deployment_cpu_utilization_tag, disk_io_class=deployment_disk_io_tag, prom_exporters=deployment_prom_exporter_tags) | asg_tag_list }}"

How do I make this pass linting?

Upvotes: 6

Views: 8202

Answers (1)

Elijah Lynn
Elijah Lynn

Reputation: 13468

You need to use a YAML Folding Scalar, > without quotes. Then append the "Strip" Block Chomping - (docs) indicator to remove the trailing newline, in total it looks like >-. The below example will work correctly, with each newline translating to a space. Adding quotes will break it e.g.

tags: >-
    {{ deployment_id | resource_tags('asg', base_resource_tags, deployment=deployment_id,
    deployment_env=deployment_env, deployment_name=deployment_name, Purpose=deployment_purpose_tag,
    cpu_utilization=deployment_cpu_utilization_tag, disk_io_class=deployment_disk_io_tag,
    prom_exporters=deployment_prom_exporter_tags) | asg_tag_list }}

Upvotes: 6

Related Questions