Search code examples
jinja2salt-project

Get value using mine.get from a ordered dictionary in state.sls


I have a mine defined in pillar to get the ip of a salt minion. When I use the mine in a salt state I get a dictionary. Is there a way to filter out and get the first item from it.

Here is my mine which I have defined in a pillar:

    ip_add:
      - mine_function: grains.get
      - ipv4

Here is my state file:

{% set id = 'aws-vm1' %}
{% set addrs =  salt['mine.get'](id, 'ip_add') %}

{% for key in salt['mine.get'](id, 'ip_add')  %}
Test:
  file.append:
    - name: C:\test
    - text: {{addrs}}

The output i get is : OrderedDict([('aws-vm1', ['10.93.143.235', '127.0.0.1'])])

I want to get the first ip so that I can share it between minions


Solution

  • I was able to find a work around. I get the list of IPs from the mine

    aws-vm1:
        ----------
        aws-vm1:
            - 10.93.143.235
            - 127.0.0.1
    

    and grab the first value by {{ip[0]}} which is the private IP I want.

    {% set id = 'aws-vm1' %}
     
    # Returns a dict OrderedDict
    {% set ip_list  =  salt['mine.get'](id, 'ip_add') %} 
    
    
    {% set ip=  ip_list.values() | first %}
    
    Test:
      file.append:
        - name: C:\test
        - text: {{ip[0]}}