I want to create a group and user using salt state files, but I do not know the group, gid, user, uid, sshkey until I need to execute the salt state file which I would like to pass in as parameters.
I have read about Pillar to create the variable. How do I create pillars before execution?
/srv/salt/group.sls:
{{ name }}:
  group.present:
    - gid: {{ gid }}
    - system: True
Command line:
salt 'SaltStack-01' state.sls group name=awesome gid=123456
Advertisement
Answer
You use Pillars to create “dictionaries” that you can reference into State files. I’m not sure if I’m understanding you correctly, but here’s an example of what you can do:
- mkdir /srv/pillar/
- Create - /srv/pillar/groups.slsand paste something like this into it:- groups: first: 1234 second: 5678 - These are names and GIDs of the groups you want to create. 
- Create - /srv/pillar/top.slsso you can apply this pillar to your minions. This is very similar to a salt top file, so you can either apply it to all minions (- '*') or just the one (- 'SaltStack-01'):- base: 'hc01*': - groups- To test that that has worked, you can run - salt '*' pillar.itemsand you should find the- groupspillar somewhere in the output.
- Now, your - /srv/salt/group.slsfile should look like this:- {% for group,gid in pillar.get('groups',{}).items() %} {{ group }}: group.present: - gid: {{ gid }} {% endfor %}- This is a for loop: - forevery- groupand- gidin the pillar- groups, do the rest. So basically, you can look at it as if the state file is running twice:- first: group.present: - gid: 1234- And then: - second: group.present: - gid: 5678
This was incorporated from this guide.
