How do I get the hosts from a specific section of an Ansible `hosts` file in a Jinja2 Template
The magic you need is magic variables, in particular the groups variable, which is


  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 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(',') }}