Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass parameters to a salt state file?

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
like image 491
Ryan Currah Avatar asked Sep 21 '14 18:09

Ryan Currah


1 Answers

Another nice way to pass (incase you don't want to use pillars Nor create a file as other answers shows) - you can pass a local environment variable to salt and read it from within the sls file, like this:

Command:

MYVAR=world salt 'SaltStack-01' state.sls somesalt   # Note the env variable passed at the beginning

sls file:

# /srv/salt/somesalt.sls

foo:
  cmd.run:
    - name: |
        echo "hello {{ salt['environ.get']('MYVAR') }}"

Will print to stdout:

hello world

Another good thing to know is that the env variable also gets passed on to any included salt states as well.

like image 147
Mercury Avatar answered Oct 19 '22 04:10

Mercury