Update hosts file with host in group

Hi,

I want to create host file with following requirements

My inventory file

[hadoop:children]
hadoop-master
hadoop-slave
hadoop-gateway

[hadoop-master]
hnn001.test.abc.com
hnn002.test.abc.com

[hadoop-slave]
hdp001.test.abc.com
hdp002.test.abc.com
hdp003.test.abc.com
hdp004.test.abc.com
hdp005.test.abc.com
hdp006.test.abc.com

[hadoop-gateway]
hadoop-client01.test.abc.com

My task (got from https://gist.github.com/rothgar/8793800) :

  • name: Update /etc/hosts
    lineinfile: dest=/etc/hosts regexp=‘.*{{ item }}$’ line=“{{ hostvars[item].ansible_default_ipv4.address }} {{item}}” state=present
    when: hostvars[item].ansible_default_ipv4.address is defined
    with_items: groups[‘hadoop’]

but here I need to run playbook on the group (hadoop), how can I just run on group children (hadoop-master, hadoop-slave, hadoop-gateway) or just one of the host & still get host entry for all hosts in the group.

any idea how to do that in ansible ?

Thanks