Add ansible_host= to /etc/hosts

Hi, I am having issues getting proper /etc/hosts entries when I specify ansible_host= in my inventory file. I have two task; first behaves as expected; second chokes with no variable ansible_host defined error. Any help or pointers would be much appreciated.

tasks:

- name: Build hosts file
  lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[item].ansible_default_ipv4.address }} {{ item }}" state=present
  when: hostvars[item].ansible_default_ipv4 is defined
  with_items: "{{ groups['all'] }}"

- name: Add aliases for hosts excluded in playbook to hosts file
  lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[hostvars[item].ansible_host].ansible_default_ipv4.address }} {{ item }}" state=present
  when: hostvars[item].ansible_default_ipv4 is not defined
  with_items: "{{ groups['all'] }}"

inventory:

[jenkins]
jenkins
[logstash]
logstash1 ansible_host=web1
logstash2 ansible_host=web2
logstash3 ansible_host=web3
[web]
web1
web2
web3