title: Ansible - how to use selectattr with yaml of different keys
body: <p>im pulling my hairs trying to do a simple thing (i thought it should be easy) with parsing a yaml and filtering for some key in Ansible.</p>
  
  <p>My yaml file looks like this:</p>
  
  <pre><code>---
  
  - vm: "vm1"
    ip: 10.10.10.1
  - vm: "vm2"
    ip: 10.10.10.2
  - test_vm: something
  - another_vm: something_other
  </code></pre>
  
  <p>so i thought than an expression like </p>
  
  <pre><code>lookup('file','my_file.yaml') | from_yaml | selectattr('vm','search','vm1')|list
  </code></pre>
  
  <p>would work but it gives an error like</p>
  
  <pre><code>fatal: [localhost]: FAILED! =&gt; {"msg": "Unexpected templating type error occurred on ({{ lookup('file','{{sysfile}}') | from_yaml | selectattr('vm','search','vm1')|list}}): expected string or bytes-like object"}
  </code></pre>
  
  <p>If i remove the test_vm and another_vm keys it works fine.</p>
  
  <pre><code>ok: [localhost] =&gt; {
      "msg": [
          {
              "ip": "10.10.10.1",
              "vm": "vm1"
          }
      ]
  }
  
  </code></pre>
  
  <p>If i try to search for the test_vm key it fails with:</p>
  
  <pre><code>fatal: [localhost]: FAILED! =&gt; {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'test_vm'\n\nThe error appears to be ...
  </code></pre>
  
  <p>Is selectattr filter expecting all the dicts in the list to have the same keys?Because it does not make any sense not to be able to filter a list of custom dicts with Jinja2.</p>
  
  <p>For example if i had a more complicated yaml (not that flat) am i limited to search and filter within Ansible?</p>
  
  <p>For example if i have a yaml looks like this:</p>
  
  <pre><code>---
  
  - vm: "vm1"
    ip: 10.10.10.1
  - vm: "vm2"
    ip: 10.10.10.2
  - test_vm: something
     - process_1: X
     - process_2: Y
     - process_3: Z
  - another_vm: something_other
  </code></pre>
  
  <p>how can i quickly filter for the process_2 for example?
  Is there a way?</p>
  
  <p>Thanks a lot in advance.</p>