title: How to check if inventory group exists in Ansible?
body: <p>Is there a way to check if an inventory group exists (and if it doesn't, the task should be skipped)?</p>
  
  <p>I know you can check if a host exists in a group by way of "'Cool-Server' in groups['WebServers']" but I am having a hard time figuring out how to make ansible ignore a task if the group itself is not defined.</p>
  
  <p>I basically have a task like such:</p>
  
  <pre><code>- name: Some Task
    command: ls -ltr
    when:
      - "'Cool-Server' in groups['WebServers]"
  </code></pre>
  
  <p>and I'd like for ansible to be able to only run this task if the group WebServers itself is defined in the inventory, otherwise it should skip the task. Right now, if the group WebServers does not exist in the inventory, ansible/jinja freaks out and errors because its trying to look for something that does not exist.</p>
  
  <p>Any ideas?</p>