Answers: <p>The magic you need is <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables" rel="nofollow noreferrer">magic variables</a>, in particular the <code>groups</code> variable, <a href="https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic" rel="nofollow noreferrer">which is</a></p>
  
  <blockquote>
    <p>a dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it.</p>
  </blockquote>
  
  <p>Then you can use template <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html" rel="nofollow noreferrer">filters</a> to format the list: use <code>map</code> with <code>regex_replace</code> to add the quotes to all list elements and then <code>join</code> them into the final result.</p>
  
  <pre><code>{{ groups['elasticsearch_nodes']|map('regex_replace', '(.*)', '"\\1"')|join(',') }}
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-02T19:34:00.563Z
Answers: 2019-05-02T19:34:00.563Z
Answers: Michał Politowski
Body: <p>I have a <code>hosts</code> file including the following contents</p>
  
  <pre><code>[elasticsearch_nodes]
  host1 os=linux
  host2 os=linux
  host3 os=linux
  </code></pre>
  
  <p>In a <code>.j2</code> template I want to get the the list of <code>[elasticsearch_nodes]</code></p>
  
  <pre><code>{{ some_ansible_magic_here }}
  
  # should output "host1","host2","host3"
  </code></pre>
  
  <p>How do I do this?</p>
  
  <p>I do have this as well to help with the quotes and the commas when I have the list.</p>
  
  <pre><code>{% for host in some_sort_of_ansible_magic_to_get_me_that_list_i_need %}
   "{{ host }}"{% if not loop.last %},{% endif %}
  {% endfor %}
  </code></pre>
  
Tags: <ansible><jinja>
Title: How do I get the hosts from a specific section of an Ansible `hosts` file in a Jinja2 Template
UserDisplayName: David West
ThreadText: # How do I get the hosts from a specific section of an Ansible `hosts` file in a Jinja2 Template
  
  (Asked by: username_0 on 2019-05-02)
  
  I have a `hosts` file including the following contents
  
  ```
  [elasticsearch_nodes]
  host1 os=linux
  host2 os=linux
  host3 os=linux
  
  ```
  
  In a `.j2` template I want to get the the list of `[elasticsearch_nodes]`
  
  ```
  {{ some_ansible_magic_here }}
  
  # should output "host1","host2","host3"
  
  ```
  
  How do I do this?
  
  I do have this as well to help with the quotes and the commas when I have the list.
  
  ```
  {% for host in some_sort_of_ansible_magic_to_get_me_that_list_i_need %}
   "{{ host }}"{% if not loop.last %},{% endif %}
  {% endfor %}
  
  ```
  
  **Tags:** ansible, jinja
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-05-02. Score: 2
  
  The magic you need is [magic variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables), in particular the `groups` variable, [which is](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic)
  
  > a dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it.
  
  Then you can use template [filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html) to format the list: use `map` with `regex_replace` to add the quotes to all list elements and then `join` them into the final result.
  
  ```
  {{ groups['elasticsearch_nodes']|map('regex_replace', '(.*)', '"\\1"')|join(',') }}
  
  ```
  
  
  ---
  
  