I was configuring GlusterFS on few servers using Ansibleexternal link and have a need to update /etc/hosts with hostnames for easier configuration. I found this1 one working:

- name: Update /etc/hosts
  lineinfile: dest=/etc/hosts regexp='.*{{item}}$' line='{{hostvars.{{item}}.ansible_default_ipv4.address}} {{item}}' state=present
  with_items: '{{groups.somegroup}}'