# Generating a single line comma separated string variable from host group

**URL:** <https://forum.ansible.com/t/generating-a-single-line-comma-separated-string-variable-from-host-group/30356>\
**Category:** Ansible Project\
**Created:** [May 17, 2019, 5:15am UTC](https://forum.ansible.com/t/generating-a-single-line-comma-separated-string-variable-from-host-group/30356 "2019-05-17T05:15:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Armstrong1](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@James\_Armstrong1](https://forum.ansible.com/u/James_Armstrong1)\
**Post date:** [May 17, 2019, 5:15am UTC](https://forum.ansible.com/t/generating-a-single-line-comma-separated-string-variable-from-host-group/30356/1 "2019-05-17T05:15:54Z")

</div>

Hey,

I’ve been pulling my hair out over what seems like a relatively simple task for hours now.

I’ve working on playbook for an Elasticsearch deployment. One requirement for this is a config file item that is effectively just a comma separated list of hostnames or IP’s of nodes.

e.g.

discovery.zen.ping.unicast.hosts: [“hostname1”, “hostname2”, “hostname3”]

Functionally at this point I would want to generate an output here that would effective append the bellow registered variable:

node\_masters: [" “{{ansible\_hostname (FIRST GROUP MEMBER) }}”, “{{ansible\_hostname (SECOND GROUP MEMBER) }}”, “{{ansible\_hostname (THIRD GROUP MEMBER) }}”

The current j2 template for the config file uses a to\_nice\_yaml filter for the es\_config variable

{% if es\_config %}  
{{ es\_config | to\_nice\_yaml }}  
{% endif %}

I just want to get to a point where i can basically just feed in my theorical node\_masters var to the es\_config part of the play like bellow:

es\_config:  
network.host: “{{ ansible\_fqdn }}”  
cluster.name: “lab-cluster”  
**discovery.zen.ping.unicast.hosts: {{ node\_masters }}**  
discovery.zen.minimum\_master\_nodes: 1  
http.port: 9200  
node.data: true  
node.master: true  
bootstrap.memory\_lock: false

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 17, 2019, 8:40am UTC](https://forum.ansible.com/t/generating-a-single-line-comma-separated-string-variable-from-host-group/30356/2 "2019-05-17T08:40:55Z")

</div>

> Hey,
> 
> I've been pulling my hair out over what seems like a relatively simple task  
> for hours now.
> 
> I've working on playbook for an Elasticsearch deployment. One requirement  
> for this is a config file item that is effectively just a comma separated  
> list of hostnames or IP's of nodes.
> 
> e.g.
> 
> discovery.zen.ping.unicast.hosts: ["hostname1", "hostname2", "hostname3"]

The value here is more a json syntax that a yaml syntax, at least when it comes to to\_nice\_yaml you are using bellow.

> Functionally at this point I would want to generate an output here that  
> would effective append the bellow registered variable:
> 
> node\_masters: [" "{{ansible\_hostname (FIRST GROUP MEMBER) }}",  
> "{{ansible\_hostname (SECOND GROUP MEMBER) }}", "{{ansible\_hostname (THIRD  
> GROUP MEMBER) }}"
> 
> The current j2 template for the config file uses a to\_nice\_yaml filter for  
> the es\_config variable
> 
> {% if es\_config %}  
> {{ es\_config | to\_nice\_yaml }}  
> {% endif %}
> 
> I just want to get to a point where i can basically just feed in my  
> theorical node\_masters var to the es\_config part of the play like bellow:
> 
> es\_config:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network.host: "{{ ansible\_fqdn }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cluster.name: "lab-cluster"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*discovery.zen.ping.unicast.hosts: {{ node\_masters }}\*  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;discovery.zen.minimum\_master\_nodes: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http.port: 9200  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.data: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.master: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bootstrap.memory\_lock: false

The easiest is probably to make es\_config string with yaml multi-line(pay attention to the pipe) instead of a dict.

es\_config: |  
&nbsp;&nbsp;network.host: {{ ansible\_fqdn }}  
&nbsp;&nbsp;cluster.name: lab-cluster  
&nbsp;&nbsp;discovery.zen.ping.unicast.hosts: {{ node\_masters | to\_json }}  
&nbsp;&nbsp;discovery.zen.minimum\_master\_nodes: 1  
&nbsp;&nbsp;http.port: 9200  
&nbsp;&nbsp;node.data: true  
&nbsp;&nbsp;node.master: true  
&nbsp;&nbsp;bootstrap.memory\_lock: false

And the in the template so this

{% if es\_config %}  
{{ es\_config }}  
{% endif %}
