Search code examples
salt-project

Salt state not working in combination with pillardata


I have the following salt state:

freeradius:
  pkg.installed

radiusgroup:
  group.present:
    - name: radiusadm
    - gid: 666

{% for user, args in pillar['users'].items() %}
  {% if ( user not in pillar['absents'] ) and ( 'radiuspassword' in args ) %}
  shadow_hash_{{ user }}:
    user.present:
      - name: {{ user }}
      - password: {{ args['radiuspassword'] }}
  {% endif %}  
{% endfor %}

And have the following pillars

groups:
  radiusd:
    gid: 95

users:
  user1:
    radiuspassword: 'password1'
  user2:
    radiuspassword: 'password2'

absents:
  a2user1:
  a2user2:

I want to make this code work, I get the message:

Data failed to compile:

State 'radiusgroup' in SLS 'radiusd' is not formed as a list

Please advice me how to go from here, thank you in advance.


Solution

  • I think the issue is in the declaration of pillar. Also having indents in state file in for and if blocks may cause issues. The error states that radiusgroup is not formed as a list. So we should form a list with groups.

    Example pillar:

    groups:
      - name: radiusd
        gid: 95
    
    users:
      - name: user1
        radiuspassword: password1
      - name: user2
        radiuspassword: password2
    
    absents:
      - a2user1
      - a2user2
    

    Note that I have used similar structure for the list of users as well. Then we can have state file as:

    {% for group in pillar['groups'] %}
    radiusgroup_{{ group.name }}:
      group.present:
        - name: {{ group.name }}
        - gid: {{ group.gid }}
    {% endfor %}
    
    {% for user in pillar['users'] %}
    {% if (user.radiuspassword is defined) and (user.name not in pillar['absents']) %}
    shadow_hash_{{ user.name }}:
      user.present:
        - name: {{ user.name }}
        - password: {{ user.radiuspassword }}
    {% endif %}
    {% endfor %}
    

    If the pillar is in your control, it would be best to define it exactly how you want. So that you can avoid using complicated Jinja expressions in a state file.