# Generating YAML config files via Jinja2 & Ansible vars

**URL:** <https://forum.ansible.com/t/generating-yaml-config-files-via-jinja2-ansible-vars/18289>\
**Category:** Ansible Project\
**Created:** [May 22, 2015, 6:07pm UTC](https://forum.ansible.com/t/generating-yaml-config-files-via-jinja2-ansible-vars/18289 "2015-05-22T18:07:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Tehranian](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Dan\_Tehranian](https://forum.ansible.com/u/Dan_Tehranian)\
**Post date:** [May 22, 2015, 6:07pm UTC](https://forum.ansible.com/t/generating-yaml-config-files-via-jinja2-ansible-vars/18289/1 "2015-05-22T18:07:11Z")

</div>

Hello there,

I’m trying to make a role that can dynamically generate the YAML config files for my app (The app is Data Dog’s agent). Ex: I want my Jinja2 template to render valid YAML, the contents of which are coming from Ansible vars, which themselves are defined via YAML. This is equivalent to the pattern that [https://github.com/jdauphant/ansible-role-nginx](https://github.com/jdauphant/ansible-role-nginx) uses for translating Ansible vars into nginx config files.

Is there an easy way to do this? Am I thinking about this all wrong?

Sample YAML config file for Datadog that I would like to have dynamically generated from equivalent Ansible vars:

> * * *
> 
> init\_config:

> instances:
> 
> # For every instance, you have an `nginx_status_url` and (optionally)
> 
> # a list of tags.
> 
> - nginx\_status\_url: [http://example.com/nginx\_status/](http://example.com/nginx_status/)  
> tags:
> - instance:foo
> - nginx\_status\_url: [http://example2.com:1234/nginx\_status/](http://example2.com:1234/nginx_status/)  
> tags:
> - instance:bar

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 24, 2015, 12:32pm UTC](https://forum.ansible.com/t/generating-yaml-config-files-via-jinja2-ansible-vars/18289/2 "2015-05-24T12:32:59Z")

</div>

I just did something like that recently for data-dog. My role adds data-dog agent config entries.

Example usage looks like:

…

roles:

- role: datadog

- role: datadog\_monitor  
monitors:  
mongodb:  
instances:

- server: mongodb://localhost:27017/admin  
tags:

- mongo\_tag\_1

- mongo\_tag\_2

- role: datadog\_monitor  
monitors:  
docker:  
init\_config:  
docker\_root: /  
socket\_timeout: 5  
instances:

- url: unix://var/run/docker.sock  
include:

- “docker\_image:ubuntu”

- “docker\_image:ngnx”  
exclude:

- “.\*”  
new\_tag\_names: true  
tag\_by\_command: false  
collect\_event: true  
collect\_container\_size: false  
collect\_images\_stats: false  
collect\_all\_metrics: false  
tags:

- docker\_tag\_1

- docker\_tag\_2

The roles tasks/main.yml selects a template based on the ‘monitors’ keys:

- fail: msg=“Datadog must be configured. Are you missing role datadog?”  
when: datadog\_etc\_confd\_dir is not defined

- name: Configure datadog {{ item.monitor }} monitor  
template:  
src: “conf.d/{{item.key}}.yml.j2”  
dest: “{{datadog\_etc\_confd\_dir}}/{{item.key}}.yaml”  
owner: “{{datadog\_user}}”  
notify: restart datadog  
with\_dict: monitors

These two templates:

# mongodb.yml.j2

# {{ ansible\_managed }}

init\_config:  
{{ item.value | to\_nice\_yaml }}

# docker.yml.j2

# {{ ansible\_managed }}

{% if item.value.init\_config is not defined %}  
init\_config:  
{% endif %}  
{{ item.value | to\_nice\_yaml }}

My main goal was to not create complex templates that require the definition of every data-dog variable. As I have only used this a very little so far, ymmv. At some point I will create a galaxy submission.
