# Getting the hostname of the control machine

**URL:** <https://forum.ansible.com/t/getting-the-hostname-of-the-control-machine/24829>\
**Category:** Ansible Project\
**Created:** [June 27, 2017, 4:25pm UTC](https://forum.ansible.com/t/getting-the-hostname-of-the-control-machine/24829 "2017-06-27T16:25:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rambius](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@rambius](https://forum.ansible.com/u/rambius)\
**Post date:** [June 27, 2017, 4:25pm UTC](https://forum.ansible.com/t/getting-the-hostname-of-the-control-machine/24829/1 "2017-06-27T16:25:06Z")

</div>

Hello,

I have a mail task that sends some log files and I would like to make it more clear what process generated them:

- name: send logs  
mail:  
host: smpthost  
port: 25  
body: “Please review the attached logs from the process. Process executed on {{ ansible\_hostname}} triggered by ansible on {{ ansible\_control\_machine }}”  
attach: log1.txt log2.txt

Of course variable {{ ansible\_control\_machine }} does not exist. How should I get the name of the control machine. One way I found is to use {{ lookup(‘env’,‘HOSTNAME’) }}. Is there another way without relying on environment variables? What if I want to access a random fact of the control machine?

Thank you very much for your response!

Regards  
Rambius

---

<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 27, 2017, 8:23pm UTC](https://forum.ansible.com/t/getting-the-hostname-of-the-control-machine/24829/2 "2017-06-27T20:23:08Z")

</div>

several ways, here are 2:

local\_action: shell hostname  
register: hostname

or

lookup('pipe', 'hostname')

---

<div class="post-metadata">

**Author:** ![rambius](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@rambius](https://forum.ansible.com/u/rambius)\
**Post date:** [June 27, 2017, 9:08pm UTC](https://forum.ansible.com/t/getting-the-hostname-of-the-control-machine/24829/3 "2017-06-27T21:08:58Z")

</div>

Hello,

вторник, 27 юни 2017 г., 16:23:22 UTC-4, Brian Coca написа:

> several ways, here are 2:
> 
> local\_action: shell hostname  
> register: hostname
> 
> or
> 
> lookup(‘pipe’, ‘hostname’)

Both ways work. Thank you for your help.

Regards  
Rambius
