# Get IP Address of server

**URL:** <https://forum.ansible.com/t/get-ip-address-of-server/19918>\
**Category:** Ansible Project\
**Created:** [December 2, 2015, 4:37pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918 "2015-12-02T16:37:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saikrishnakhanday](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@saikrishnakhanday](https://forum.ansible.com/u/saikrishnakhanday)\
**Post date:** [December 2, 2015, 4:37pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/1 "2015-12-02T16:37:13Z")

</div>

Can any one please help me how can I write a script to fetch its own IP Address.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [December 2, 2015, 7:21pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/2 "2015-12-02T19:21:08Z")

</div>

Why do you want to write a script ?  
There’s sure a fact for this…

Look at :  
ansible\_default\_ipv4  
ansible\_default\_ipv6

Regards,

---

<div class="post-metadata">

**Author:** ![saikrishnakhanday](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@saikrishnakhanday](https://forum.ansible.com/u/saikrishnakhanday)\
**Post date:** [December 2, 2015, 8:20pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/3 "2015-12-02T20:20:04Z")

</div>

Hi Jean

tasks:

- name: set hostname  
action: hostname name=devcfgl02

- name: create zabbix group  
action: group name=zabbix gid=1100

- name: create zabbix user  
action: user name=zabbix uid=1100 group=zabbix comment=“Zabbix Monitoring User” home=“/home/zabbix”

- name: add zabbix restart with yum  
yum: name=[http://repo.zabbix.com/zabbix/2.4/rhel/6/x86\_64/zabbix-release-2.4-1.el6.noarch.rpm](http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm) state=present

- name: Ensure that zabbix is installed  
yum: pkg=zabbix-agent state=installed

- name: Ensure that zabbix is running  
service: name=zabbix-agent state=started enabled=yes

- name: zabbix-server restarted  
service: name=zabbix-agent state=restarted enabled=yes

- name: change zabbix\_agent.conf server  
action: lineinfile dest=“/etc/zabbix/zabbix\_agentd.conf” backup=yes state=present regexp=“Server=127.0.0.1” line=“Server=prdmonapp1”

- name: change zabbix\_agent.conf server  
action: lineinfile dest=“/etc/zabbix/zabbix\_agentd.conf” backup=yes state=present regexp=“ServerActive=127.0.0.1” line=“ServerActive=prdmonapp1:10045”

- name: change zabbix\_agent.conf server  
action: lineinfile dest=“/etc/zabbix/zabbix\_agentd.conf” backup=yes state=present regexp=“ListenIP=0.0.0.0”

What action do i have to use here in the last step to fetch its own server ip and replace the ListenIP in the config file.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [December 2, 2015, 8:25pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/4 "2015-12-02T20:25:36Z")

</div>

Something like this ? (not tested)

action: lineinfile dest=“/etc/zabbix/zabbix\_agentd.conf” backup=yes state=present regexp=“^ListenIP=0.0.0.0” line=“ListenIP={{ ansible\_default\_ipv4 }}”

Regards,

---

<div class="post-metadata">

**Author:** ![saikrishnakhanday](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@saikrishnakhanday](https://forum.ansible.com/u/saikrishnakhanday)\
**Post date:** [December 2, 2015, 8:51pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/5 "2015-12-02T20:51:40Z")

</div>

action: lineinfile dest=“/etc/zabbix/zabbix\_agentd.conf” backup=yes state=present regexp=“^ListenIP=0.0.0.0” line=“ListenIP={{ ansible\_default\_ipv4.address }}”

added .address and it is working the way I was expecting. Appreciate your idea !

Cheers !

---

<div class="post-metadata">

**Author:** ![Andrew\_Edelstein](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@Andrew\_Edelstein](https://forum.ansible.com/u/Andrew_Edelstein)\
**Post date:** [December 2, 2015, 10:55pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/6 "2015-12-02T22:55:03Z")

</div>

```auto
ansible all -m setup -a 'filter=ansibl_default_ipv4'

```

```auto
You'll have to do a bit more work to get just the IP address itself, as this will return an object with the address, netmask, gateway, etc. I haven't figured that part out myself. If you have facter installed on the managed machine, you can also use "filter=facter_ipaddress". Keep in mind these may not be the IP addresses you're looking for if the managed machine has more than one IP address. Also, of course, my example only shows how to spit it out on the command line. It's much easier to access the fact sub-attributes in a task or play. ("ansible_default_ipv4.address" I believe is the syntax for the actual address. Possibly "ansible_default_ipv4.address.value, depending how you're using it.)

```

```auto

```

---

<div class="post-metadata">

**Author:** ![saikrishnakhanday](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@saikrishnakhanday](https://forum.ansible.com/u/saikrishnakhanday)\
**Post date:** [December 3, 2015, 2:31pm UTC](https://forum.ansible.com/t/get-ip-address-of-server/19918/7 "2015-12-03T14:31:26Z")

</div>

Andrew Edelstein,

am trying to get only IP addr and I have used " ansible\_default\_ipv4.address " and its working. Thanks for sharing additional information on this.

Cheers!
