How to get IP address of hostname inside jinja template
You could use a custom grain. Create file _grains/fqdn_ip.py in the state tree directory:
import socket
def fqdn_ip():
return {
'fqdn_ip': socket.gethostbyname(socket.getfqdn())
}
In template:
{{ grains.fqdn_ip }}
Another way is use dnsutil module (requires dig command on minion):
{{ salt['dnsutil.A']('host.name.tld')[0] }}
I've see this: http://cnygaard.blogspot.com.es/2012/11/how-to-template-eth0-address-with.html
This is the easy way that I've found.
#init.sls:
...
...
/etc/swift/proxy-server.conf:
file:
- managed
- source: salt://swift/proxy-server.conf
- template: jinja
- context:
proxy_ip: {{ salt['network.interfaces']()['eth0']['inet'][0]['address'] }}
And then:
#In proxy-server.conf
...
[filter:cache]
use = egg:swift#memcache
memcache_servers = {{ proxy_ip }}:11211
This is a very old post, but it is highly ranked in Google for getting the ipv4 address. As of salt 2015.5.8, the best way to get the primary ipv4 address is {{ grains['ipv4'][0] }}
.