# how to get the controller ip address

**URL:** <https://forum.ansible.com/t/how-to-get-the-controller-ip-address/25446>\
**Category:** Ansible Project\
**Created:** [September 10, 2017, 6:50pm UTC](https://forum.ansible.com/t/how-to-get-the-controller-ip-address/25446 "2017-09-10T18:50:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ehab\_Aboudaya](https://avatars.discourse-cdn.com/v4/letter/e/90ced4/32.png) [@Ehab\_Aboudaya](https://forum.ansible.com/u/Ehab_Aboudaya)\
**Post date:** [September 10, 2017, 6:50pm UTC](https://forum.ansible.com/t/how-to-get-the-controller-ip-address/25446/1 "2017-09-10T18:50:28Z")

</div>

hello all

i am trying to get the controller ip address that is used to run the playbook.  
The ip is the one when the ssh connection establishes to remote.

debug: msg=“{{ (ansible\_env|default({})).SSH\_CLIENT|default(‘’)}}”

the SSH\_CLIENT in my case is always empty. yes, i have gather facts and still empty.

i have to workaround this by

- name: Get the IP of the controller  
shell: “ss -4 -ntp 2\> /dev/null | head -2 | grep sshd | awk ‘{print $4}’ | awk -F: ‘{print $1}’”  
ignore\_errors: yes  
no\_log: yes  
register: ssh\_ip

but this is not reliable because there can be other users connected and the above only gets the first item

any help is appreciated, thanks in advance

ehab

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 10, 2017, 8:52pm UTC](https://forum.ansible.com/t/how-to-get-the-controller-ip-address/25446/2 "2017-09-10T20:52:56Z")

</div>

> i am trying to get the controller ip address that is used to run the  
> playbook.  
> The ip is the one when the ssh connection establishes to remote.
> 
> &nbsp;&nbsp;debug: msg="{{ (ansible\_env|default({})).SSH\_CLIENT|default('')}}"
> 
> the SSH\_CLIENT in my case is always empty. yes, i have gather facts and  
> still empty.

Thats because SSH\_CLIENT is not a fact so your default filter is kicking in.

> i have to workaround this by
> 
> &nbsp;&nbsp;&nbsp;- name: Get the IP of the controller  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: "ss -4 -ntp 2\> /dev/null | head -2 | grep sshd | awk '{print  
> $4}' | awk -F: '{print $1}'"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;no\_log: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ssh\_ip
> 
> but this is not reliable because there can be other users connected and the  
> above only gets the first item
> 
> any help is appreciated, thanks in advance

You could use delegated\_facts

[https://docs.ansible.com/ansible/latest/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts)

---

<div class="post-metadata">

**Author:** ![Ehab\_Aboudaya](https://avatars.discourse-cdn.com/v4/letter/e/90ced4/32.png) [@Ehab\_Aboudaya](https://forum.ansible.com/u/Ehab_Aboudaya)\
**Post date:** [September 11, 2017, 9:52am UTC](https://forum.ansible.com/t/how-to-get-the-controller-ip-address/25446/3 "2017-09-11T09:52:51Z")

</div>

Thanks Kai

i solved my problem this way

- name: get ansible env  
setup: filter=ansible\_env  
register: a\_env  
delegate\_to: “{{ target\_hosts }}”  
become: false  
delegate\_facts: true

thanks for pointing the delegate part.

have a nice week.  
my regards  
ehab
