Wednesday, November 21, 2012

How to template eth0 ip address with Saltstack Jinja


You can test the output of salt network interfaces with
# salt 'some-machine' network.interfaces

Here is the Salt state file, we will write /etc/haproxy/haproxy.cfg with the context of the eth0 ip address
Below is the content of haproxy.sls
#
# Write /etc/haproxy/haproxy.cfg
#
/etc/haproxy/haproxy.cfg:
  file.managed:
    - source: salt://files/etc/haproxy/haproxy.cfg
    - user: root
    - group: root
    - mode: 640
    - template: jinja
    - context:
        ip: {{ salt['network.interfaces']()['eth0']['inet'][0]['address'] }}

file referenced in source: salt:// above content of files/etc/haproxy/haproxy.cfg
the important part is to write the variable in thise case ip as {{ variable }}

listen stats {{ ip }}:10000

3 comments:

  1. Thanks for this post, helped me out

    ReplyDelete
  2. Thanks,

    Is this best way ?
    http://intothesaltmine.org/index5.html

    {{ salt['network.ip_addrs']('eth0')[0] }}





    ReplyDelete
  3. salt 2014.1.0:

    {{ grains['ip_interfaces']['eth0'][0] }}

    ReplyDelete