Can any one please help me how can I write a script to fetch its own IP Address.
Why do you want to write a script ?
There’s sure a fact for this…
Look at :
ansible_default_ipv4
ansible_default_ipv6
Regards,
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 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.
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,
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 !
ansible all -m setup -a 'filter=ansibl_default_ipv4'
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.)
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!