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

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 }}

you probably want hostnameoutput.stdout

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