---
# Where the rubber meets the road. stuff gets done
- name: Manage ssh_known_hosts file
  sshknownhosts:
    host: "{{ item.name }}"
    aliases: "{{ item.aliases|join(',') if item.aliases is defined else omit }}"
    state: "{{ item.state|default(ssh_known_hosts_state) }}"
    dest: "{{ item.path|default(ssh_known_hosts_path) }}"
    enctype: "{{ item.enctype|default(ssh_known_hosts_enctype) }}"
    port: "{{ item.port|default(ssh_known_hosts_port) }}"
    keyscan: "{{ item.keyscan|default(ssh_known_hosts_keyscan) }}"
  with_items: "{{ ssh_known_hosts }}"
  when: "{{ item.fingerprint is undefined }}"

- name: Add lines for specified fingerprints
  lineinfile: 
    dest: "{{ item.path|default(ssh_known_hosts_path) }}"
    regexp: "^{{ item.name }}"
    line: "{{ ([ item.name ] + item.aliases)|join(',') if item.aliases is defined else item.name }} {{ ssh_known_hosts_enctype_lookup[item.enctype|default(ssh_known_hosts_enctype)] }} {{ item.fingerprint }}"
  with_items: "{{ ssh_known_hosts }}"
  when: "{{ item.fingerprint is defined }}"

---
# Where the rubber meets the road. stuff gets done
- name: Manage ssh_known_hosts file
  sshknownhosts:
    host: "{{ item.name }}"
    aliases: "{{ item.aliases|join(',') if item.aliases is defined else omit }}"
    state: "{{ item.state|default(ssh_known_hosts_state) }}"
    dest: "{{ item.path|default(ssh_known_hosts_path) }}"
    enctype: "{{ item.enctype|default(ssh_known_hosts_enctype) }}"
    port: "{{ item.port|default(ssh_known_hosts_port) }}"
    keyscan: "{{ item.keyscan|default(ssh_known_hosts_keyscan) }}"
  with_items: "{{ ssh_known_hosts }}"
  when: item.fingerprint is undefined

- name: Add lines for specified fingerprints
  lineinfile: 
    dest: "{{ item.path|default(ssh_known_hosts_path) }}"
    create: True
    regexp: "^{{ item.name }}"
    line: "{{ ([ item.name ] + item.aliases)|join(',') if item.aliases is defined else item.name }} {{ ssh_known_hosts_enctype_lookup[item.enctype|default(ssh_known_hosts_enctype)] }} {{ item.fingerprint }}"
  with_items: "{{ ssh_known_hosts }}"
  when: item.fingerprint is defined

Create file when using lineinfile & don't do jinja in when clause
Create file when using lineinfile & don't do jinja in when clause
