# Ansible + jinja2 macro

**URL:** <https://forum.ansible.com/t/ansible-jinja2-macro/28905>\
**Category:** Ansible Project\
**Created:** [October 31, 2018, 10:39am UTC](https://forum.ansible.com/t/ansible-jinja2-macro/28905 "2018-10-31T10:39:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [October 31, 2018, 10:39am UTC](https://forum.ansible.com/t/ansible-jinja2-macro/28905/1 "2018-10-31T10:39:21Z")

</div>

Hi @all,

I am trying to use a macro in an ansible template:

` `{% macro logging(ltype) %}`  
``

```auto
{% if ltype == 'fluentd' %}
logging:
  driver: 'fluentd'
  options:
    tag: "{{ app_name }}.{{ '{{' }}.Name{{ '}}' }}"
    labels: "mylabel"
{% endif %}
{% endmacro %}

...                                                                                                                   
   {{ logging('fluentd') }}                                                                                           
...

```

`

When I render it (template module), I am always getting

`  
…  
logging:  
driver: ‘fluentd’  
options:  
tag: “test.{{.Name}}”  
labels: “fhg.iam.appname,fhg.iam.hostname,fhg.iam.publicip,fhg.iam.privateip”

…

`

So apparently 'logging is indented right, but everything else just loses its indentation. Has anyone else got any similar problem?

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [November 1, 2018, 5:16pm UTC](https://forum.ansible.com/t/ansible-jinja2-macro/28905/2 "2018-11-01T17:16:03Z")

</div>

Have you tried this to see if it removes the extra ident?

```auto
...                                                                                                                   
{{ logging('fluentd') }}                                                                                           
...

```

---

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [November 6, 2018, 12:29pm UTC](https://forum.ansible.com/t/ansible-jinja2-macro/28905/3 "2018-11-06T12:29:50Z")

</div>

yes, thanks, funnily there is some logics behind that. The macro is like you would paste it at the spot. so, the first line is indented, and the others are not…

---

<div class="post-metadata">

**Author:** ![15911133389](https://avatars.discourse-cdn.com/v4/letter/1/5f9b8f/32.png) [@15911133389](https://forum.ansible.com/u/15911133389)\
**Post date:** [November 6, 2018, 12:37pm UTC](https://forum.ansible.com/t/ansible-jinja2-macro/28905/4 "2018-11-06T12:37:37Z")

</div>

I don’t think so, at least in the examples I’ve seen so far, you can change user information in tables at will, but in fact you won’t be allowed to do so in version 3.6 + of mongo.

[

| 

![](http://mail-online.nosdn.127.net/sm27d9c4ca3f07df9ae8142c81e13e9c41.jpg)

 | 

于宗洋

 |
| 

[15911133389@163.com](mailto:15911133389@163.com)

 |

](https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=%E4%BA%8E%E5%AE%97%E6%B4%8B&uid=15911133389%40163.com&iconUrl=http%3A%2F%2Fmail-online.nosdn.127.net%2Fsm27d9c4ca3f07df9ae8142c81e13e9c41.jpg&items=%5B%2215911133389%40163.com%22%5D)

签名由 [网易邮箱大师](https://mail.163.com/dashi/dlpro.html?from=mail81) 定制

On 11/6/2018 20:29，[ProfHase](mailto:profhase85@gmail.com)[profhase85@gmail.com](mailto:profhase85@gmail.com) wrote：
