title: Ansible CSV lookup in Jinja2 template, how to handle missing key
body: <p>j2 template and here is a snippet:</p>
  
  <pre><code>{% set denyGroups %}
  {{lookup('csvfile', '{{ ansible_hostname }} file={{ csvFile }} delimiter=, col=5')}}
  {% endset %}
  {%- if denyGroups |trim() -%}
  simple_deny_groups = {{ denyGroups |replace(";", ",") }}
  {%- endif -%}
  </code></pre>
  
  <p>I am injecting values into the template based on csv values. However, if the key (ansible_hostname ) is not found in the csv, get this error: <code>AnsibleError: csvfile: list index out of range</code></p>
  
  <p>How can I do this error handling? Check if its in the csv first. Now I could inject the value in tasks, but that will be a bit more cumbersome, and I prefer this template way. Thanks</p>