# replace a word in a file with o/p of command

**URL:** <https://forum.ansible.com/t/replace-a-word-in-a-file-with-o-p-of-command/21733>\
**Category:** Ansible Developer\
**Created:** [June 10, 2016, 12:41pm UTC](https://forum.ansible.com/t/replace-a-word-in-a-file-with-o-p-of-command/21733 "2016-06-10T12:41:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallaveet](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@pallaveet](https://forum.ansible.com/u/pallaveet)\
**Post date:** [June 10, 2016, 12:41pm UTC](https://forum.ansible.com/t/replace-a-word-in-a-file-with-o-p-of-command/21733/1 "2016-06-10T12:41:58Z")

</div>

Hi,

I have a file in which i need to replace worrd with hostname of my machine.  
I am trying below code and getting error.  
Pls let me know ho can i do this … i am new to ansible

- name: hostame  
shell: hostname  
register: hostnameoutput

- debug: mesg=‘{{ hostnameoutput }}’

- name: replace hostname  
replace: dest=/root/test1 regexp=hostname replace={{ hostnameoutput }}

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 10, 2016, 1:52pm UTC](https://forum.ansible.com/t/replace-a-word-in-a-file-with-o-p-of-command/21733/2 "2016-06-10T13:52:44Z")

</div>

you probably want hostnameoutput.stdout

---

<div class="post-metadata">

**Author:** ![jothibasu\_k](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jothibasu\_k](https://forum.ansible.com/u/jothibasu_k)\
**Post date:** [July 19, 2016, 5:58pm UTC](https://forum.ansible.com/t/replace-a-word-in-a-file-with-o-p-of-command/21733/3 "2016-07-19T17:58:35Z")

</div>

if you enable gathering fatcs is true, you can call the hostname like below.

- hosts: all  
gather\_facts: yes  
tasks:
- debug: msg=‘{{ inventory\_hostname }}’

Note; Inventory\_hostname will give you the hostname of the remote server where your task is exeucting, it will work for localhost also
