title: Ansible filter dict based on key presence and list values
body: <p>I have this variable :</p>
  <pre><code>components:
    comp_one:
      name: first_component
      remote_server_path: &quot;/tmp/path/one&quot;
      binaries:
        - test
        - test_two
  
  
    comp_two:
      name: second_component
      remote_server_path: &quot;/tmp/path/two&quot;
      binaries:
        - ed_test
        - ed_test_2
  
    comp_three:
      name: third_component
      remote_server_path: &quot;/tmp/path/three&quot;
  </code></pre>
  <p>What I'm trying to achieve, is to only keep sub-dictionaries of <code>components</code> that has key <code>binaries</code>, and the filter those binaries using my <code>group_names</code>. Also, D'd like to only keep dict keys <code>binaries</code> and <code>remote_server_path</code>.</p>
  <p>For example, if i have this hosts.yaml :</p>
  <pre><code>---
  all:
    children:
      kubernetes_dev:
        children:
          ed_test:
            hosts:
              host_one:
                ansible_host: XXX.XXX.XXX.XXX
                ansible_ssh_user: ansible
          test_two:
            hosts:
              host_one:
                ansible_host: XXX.XXX.XXX.XXX
                ansible_ssh_user: ansible
          test:
            hosts:
              host_one:
                ansible_host: XXX.XXX.XXX.XXX
                ansible_ssh_user: ansible
  
  </code></pre>
  <p>The expected output would be :</p>
  <pre><code>comp_one:
    remote_server_path: &quot;/tmp/path/one&quot;
    binaries:
      - test
      - test_two
  
  comp_two:
    remote_server_path: &quot;/tmp/path/two&quot;
    binaries:
      - ed_test
  </code></pre>
  <p>I've successfully filtered the <code>comp_three</code> dict using this :</p>
  <pre><code>- name: Debug
    ansible.builtin.debug:
      msg: &quot;{{ item }}&quot;
    with_items: &quot;{{ components | dict2items | selectattr('value.binaries', 'defined') }}&quot;
  </code></pre>
  <p>But, I can't figure out a way to only select <code>binaries</code> and <code>remote_server_path</code> keys while filtering with my `group_names.</p>
  <p>Does anyone can't help me find a clean way to do this ?</p>
  <p>Regards,</p>