# How to flatten host vars into a string.

**URL:** <https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226>\
**Category:** Ansible Project\
**Created:** [September 15, 2014, 10:54am UTC](https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226 "2014-09-15T10:54:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain\_Kreienbuhl](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Alain\_Kreienbuhl](https://forum.ansible.com/u/Alain_Kreienbuhl)\
**Post date:** [September 15, 2014, 10:54am UTC](https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226/1 "2014-09-15T10:54:34Z")

</div>

Hello,

I’m trying to build a String based on host vars from a group.

I see how to loop thru host in a group

```auto
{% for host in groups['app_servers'] %}
   {{ hostvars[host]['my_cool_var'] }}
{% endfor %}

```

```auto
But not how to flatten this into a string to get :

```

```auto
my_cool_val1, my_cool_val2,...,my_cool_vallast

```

```auto
Thanks for you help

```

```auto

```

```auto
Alain

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 15, 2014, 11:12am UTC](https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226/2 "2014-09-15T11:12:19Z")

</div>

something like

{% for cmd in tester\_commands\_root\_extra %}{{ cmd }}{% if not loop.last %}, {% endif %}{% endfor %}

---

<div class="post-metadata">

**Author:** ![Alain\_Kreienbuhl](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Alain\_Kreienbuhl](https://forum.ansible.com/u/Alain_Kreienbuhl)\
**Post date:** [September 15, 2014, 12:09pm UTC](https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226/3 "2014-09-15T12:09:34Z")

</div>

Super. Thanks for your quick reply. Exactly what I needed

Alain

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 16, 2014, 12:58am UTC](https://forum.ansible.com/t/how-to-flatten-host-vars-into-a-string/16226/4 "2014-09-16T00:58:35Z")

</div>

several ways, with a for loop:

{% for host in groups['app\_servers'] %}{{  
hostvars[host]['my\_cool\_var'] }}{% if not loop.last %},{% endi f%}{%  
endfor %}
