# Issue with yaml in jinja2

**URL:** <https://forum.ansible.com/t/issue-with-yaml-in-jinja2/37734>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 6, 2023, 6:40pm UTC](https://forum.ansible.com/t/issue-with-yaml-in-jinja2/37734 "2023-03-06T18:40:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://forum.ansible.com/u/Guido)\
**Post date:** [March 6, 2023, 6:40pm UTC](https://forum.ansible.com/t/issue-with-yaml-in-jinja2/37734/1 "2023-03-06T18:40:00Z")

</div>

Hi Ansible community.,

I’d like to share a problem I’m having while trying lo load a yaml formatted template and perhaps getting feedback from you on how to make it work.

I wrote the following playbook: [https://gist.github.com/gaccardo/2c12fc4aab443978fe33829129237cbc](https://gist.github.com/gaccardo/2c12fc4aab443978fe33829129237cbc)

If the content of “policy\_content.yaml” is “pure” YAML, i.e: [https://gist.github.com/gaccardo/3047c0c06d36d39a69d2d3c60a3daf4e](https://gist.github.com/gaccardo/3047c0c06d36d39a69d2d3c60a3daf4e), the task Create IAM Managed Policy works as expected, meaning the IAM policy gets created in my AWS account.

Now, instead if I change the file policy\_content.yaml to the following: [https://gist.github.com/gaccardo/fc30a3c40f8ff01d44b61ad6fec0a3b7](https://gist.github.com/gaccardo/fc30a3c40f8ff01d44b61ad6fec0a3b7), the task fails with the following error: [https://gist.github.com/gaccardo/f27accb0dac958ab83c232bb347a292b](https://gist.github.com/gaccardo/f27accb0dac958ab83c232bb347a292b).

This is how i’m calling the playbook:

$ ansible-playbook -e “selected\_env=dev” policy.yml -vvv

Is it possible that the filter “from\_yaml” is getting the template unredered from “lookup”?

The error says: “did not find expected ‘-’ indicator” but I’m starting the lines within the Actions with the required “-”

…  
7 - Effect: Allow  
8 Action:  
9 - “s3:Get\*”  
10 {% if env in [“dev”, “stg”] %}  
11 - “s3:Put\*”  
12 {% endif %}  
13 Resource:  
14 - “arn:aws:s3:::bucket/{{ env }}”  
15 - “arn:aws:s3:::bucket/{{ env }}/\*”

Check lines 9 and 11.

Thank you in advance for you time. Best!

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 6, 2023, 7:07pm UTC](https://forum.ansible.com/t/issue-with-yaml-in-jinja2/37734/2 "2023-03-06T19:07:36Z")

</div>

A few things here:

1. Your gist of that file, and what you indicate in your email are different, specifically the `if env in ["dev", "stg]` part
2. As a result of #1 the YAML renders incorrectly, causing that error

Here is the result of the template when `env: dev` is set:

---

<div class="post-metadata">

**Author:** ![Guido](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Guido](https://forum.ansible.com/u/Guido)\
**Post date:** [March 6, 2023, 7:21pm UTC](https://forum.ansible.com/t/issue-with-yaml-in-jinja2/37734/3 "2023-03-06T19:21:45Z")

</div>

> A few things here:
> 
> 1. Your gist of that file, and what you indicate in your email are different, specifically the `if env in ["dev", "stg]` part

Sorry about this.

> 1. As a result of #1 the YAML renders incorrectly, causing that error
> 
> Here is the result of the template when `env: dev` is set:
> 
> * * *
> 
> Version: 2012-10-17  
> Statement:
> 
> - Effect: Allow  
> Action:
> - “s3:List\*”  
> Resource: “arn:aws:s3:::bucket”
> - Effect: Allow  
> Action:
> - “s3:Get\*”
> - “s3:Put\*”  
> Resource:
> - “arn:aws:s3:::bucket/dev”
> - “arn:aws:s3:::bucket/dev/\*”
> 
> As such, your template needs to be adjusted with something like this, where the `{% if %}` and `{% endif %}` blocks aren’t adding to the indentation, by being completely left justified:
> 
> * * *
> 
> Version: 2012-10-17  
> Statement:
> 
> - Effect: Allow  
> Action:
> - “s3:List\*”  
> Resource: “arn:aws:s3:::bucket”
> - Effect: Allow  
> Action:
> - “s3:Get\*”
> 
> {% if env in [“dev”, “stg”] %}
> 
> - “s3:Put\*”  
> {% endif %}  
> Resource:
> 
> - “arn:aws:s3:::bucket/{{ env }}”
> 
> - “arn:aws:s3:::bucket/{{ env }}/\*”

Thank you for this clarification, what you suggested worked!
