title: Ansible - Access hosts (list) in groups variable (dictionary) via regex or json_query (+JSME functions) on group_name (key)
body: <p>we are having dynamic group names in our inventory file, that are based on a naming convention. For these I would like to access the hosts of these to iterate through all of them in order to create a string out of it (eg. comma seperated).</p>
  
  <p>As an example:</p>
  
  <pre><code>    [misbe_test_a]
      host1
      host2
  
      [misbe_test_b]
      host2
      host3
      host4
  </code></pre>
  
  <p>The variable would be a dictionary, which contains per item a list:</p>
  
  <pre><code>    "misbe_test_a": [
          "host1",
          "host2"
      ],
      "misbe_test_b": [
          "host2",
          "host3",
          "host4"
      ]
  </code></pre>
  
  <p>What of course works out for one specific group:</p>
  
  <pre><code>    {% for host in (groups["misbe_test_a"] | unique) %}{{ host }}{% endfor %}
  </code></pre>
  
  <p>However I need to access all of the groups without knowing what is followed by misbe_. This solution will of course not work out:</p>
  
  <pre><code>    {% for host in (groups["misbe_*"] | unique) %}{{ host }}{% endfor %}
  </code></pre>
  
  <p>Any ideas how I could solve that with a one liner? What I would like to omit is the following (in addition it would add host2 twice to my string > as in different groups):</p>
  
  <pre><code>    {% for group_name in (groups | select('match','misbe.*') | list | unique) %}
        {% for host in groups[group_name] %}
          {{ host }}
        {% endfor %}
      {% endfor %}" 
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>What about using json_query for this, is there any possibility to query the key of the dictionary and then access the items of the list?</p>
  
  <p>This one would work for one group:</p>
  
  <pre><code>    {{ groups | json_query('misbe_test_a') | list | join(',') }}        
  </code></pre>
  
  <p>Unfortunately I do not know how to select they key in the dictionary with JSME functions (eg. starts_with or contains <a href="http://jmespath.org/specification.html#functions" rel="nofollow noreferrer">http://jmespath.org/specification.html#functions</a>).</p>