Score:0

How to access a roles hostlist in a statefile?

pk flag

I am using salt by defining roles which map a list of states to a list of hosts on which to apply the states:

#/srv/pillar/base/top.sls:
{% set h = 'host1, host2,' %}
'L@{{ h }}':
    - roles.Servers

{% set h = 'host3, host4,' %}
'L@{{ h }}':
    - roles.newServers    
    
#/srv/salt/base/top.sls: 
base:
  'role:Server': # Mapping a host to a role can be found in: /srv/pillar/top.sls
    - match: pillar
    - mystate1         # "shared" statefile
    - mystate2

  'role:newServer':
    - match: pillar
    - mystate1         # "shared" statefile
    - mystate3

Now in a state like mystate1.sls below I want to make a difference what the state does that should depend on the host list from the role by which the minion was selected.

# /srv/salt/base/mystate1.sls
newPkg:
  pkg.installed:
    - pkgs:
{% if  grains['id'] in roles[ 'Server' ] %}  # pseude code: check if 'id' is in hostlist for role 'Server'
    - mypackage1    # do this if the current host is in the hostlist of role 'Servers'
{% else %}
    - myNewPackage  # do that if the host is part of role 'newServers'
{% endif %}    

So what I would like to check in the state on the minion for which the state was called is if the minion belongs to the host list of role 'Server' or 'newServer'.

Any idea?

Score:0
il flag

Two main options. First:

{% if salt["match.pillar"]("role:Server") %}

Second, if the role is defined as a single value:

{% if pillar["role"] == 'Server' %}

or if it's a list:

{% if 'Server' in pillar["role"] %}
rainer042 avatar
pk flag
Works, great, thanks a lot.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.