# How to update host file  using ansible

**URL:** <https://forum.ansible.com/t/how-to-update-host-file-using-ansible/29515>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 24, 2019, 2:40pm UTC](https://forum.ansible.com/t/how-to-update-host-file-using-ansible/29515 "2019-01-24T14:40:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep\_Ghorpade](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pradeep\_Ghorpade](https://forum.ansible.com/u/Pradeep_Ghorpade)\
**Post date:** [January 24, 2019, 2:40pm UTC](https://forum.ansible.com/t/how-to-update-host-file-using-ansible/29515/1 "2019-01-24T14:40:47Z")

</div>

Hello Team,

I am looking ansible playbook which will update host file in below scenario.

I have 2 group server both are aws instance.  
[test]  
servername

[test1]  
servername1

I want to update host file on test group server with test1 server details like ip address and hostname.

I dont want to put ip details into inventory.

can u help me here.

---

<div class="post-metadata">

**Author:** ![Pradeep\_Ghorpade](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pradeep\_Ghorpade](https://forum.ansible.com/u/Pradeep_Ghorpade)\
**Post date:** [January 24, 2019, 2:46pm UTC](https://forum.ansible.com/t/how-to-update-host-file-using-ansible/29515/2 "2019-01-24T14:46:38Z")

</div>

I am using below playbook with set hostname on xyz server and update host file for self. I want to add abc server details into host file of xyz.

vars:

test\_hostname: “xyz”

- name: set up host name  
hostname:  
name: “{{ test\_hostname }}”  
register: set\_hostname  
tags:

- set\_hostname  
when: test\_hostname is defined

- name: Add host entry into host file  
lineinfile:  
dest: /etc/hosts  
regexp: ‘.\*{{ ansible\_nodename }}$’  
line: ‘{{ ansible\_default\_ipv4.address }} {{ ansible\_nodename }} {{ ansible\_hostname }}’  
state: present

- name: Add other host entry  
lineinfile:  
path: /etc/hosts  
regexp: ‘.\*{{ ansible\_nodename }}$’  
#insertafter: ‘^{{ ansible\_nodename }}’  
line: ‘{{ ansible\_default\_ipv4.address }} {{ ansible\_nodename }}’  
when: inventory\_hostname is groups[‘test1’]  
tags:

- add\_host
