Wednesday, February 24, 2016

Extending Salt - Quick Start

Create a SLS file with Jinja code ( the one which got enclosed in flower {} brackets.

# cat /srv/salt/file/base/jinja/simple_var.sls
{% set simple_var = 'a simple variable' %}
jinja_var:
  cmd.run:
    - name: echo "Simple var is {{ simple_var }}"

Test the jinja based SLS file
# salt vag* state.show_sls jinja.simple_var

Jinja list example

# cat /srv/salt/file/base/jinja/list.sls
{% set list1 = ['one', 'two', 'three'] %}
jinja_list:
  cmd.run:
    - name: echo "List is {{ list1 }}"


# salt vag* state.show_sls jinja.list

Jinja example to access a particular list item

# cat /srv/salt/file/base/jinja/list_item.sls
{% set list1 = ['one', 'two', 'three'] %}
jinja_list_item:
  cmd.run:
    - name: echo "List item 2 is {{ list1[2] }}"

# salt vag* state.show_sls jinja.list_item



Jinja dictionary example

# cat /srv/salt/file/base/jinja/dict.sls
{% set my_dict = {'first': 'value 1', 'second': 'value 2'} %}
jinja_dict_first:
  cmd.run:
    - name: echo "First item is {{ my_dict['first'] }}"


# salt vag* state.show_sls jinja.dict

Jinja listing dictionary keys example