Ben Mathews
Ben Mathews

Reputation: 2998

take username as a parameter to a salt state file (sls)

I'd like to use saltstack to standardize developers workspaces. As such, I need to take the username as a parameter. This could come from $USER or be typed in. Either would be fine. I see that I could write a grain to expose this, but that seems overly complicated. I'm thinking I'm missing something simpler.

I want to enable something like this

https://...../reponame.git:
  git.latest:
    - target: '/home/{{ username }}/src/reponame'
    - user: {{ username }}

What is the best/easiest way to do this?

Upvotes: 0

Views: 674

Answers (3)

user12272532
user12272532

Reputation: 1

{% set username = salt['cmd.run']('logname') | trim %}

Upvotes: 0

Travv15
Travv15

Reputation: 243

You can temporarily set/overwrite a pillar at state execution.

https://docs.saltstack.com/en/latest/topics/tutorials/pillar.html#setting-pillar-data-on-the-command-line

So, with a sls (eg. workstation/gitrepo.sls) like

{% set username = salt['pillar.get']('username') %}

https://...../reponame.git:
  git.latest:
    - target: '/home/{{ username }}/src/reponame'
    - user: {{ username }}

You could call

salt 'phils-pc' state.sls workstation.gitrepo pillar='{"username": "phil"}'

Upvotes: 0

helmbert
helmbert

Reputation: 37984

Maybe not elegant, but effective: use the cmd.run module to get the user that the Salt minion is currently running as (assuming that the Salt minion is running as user, and not as root):

{% set username = salt['cmd.run']('id -u -n') | trim %}

https://...../reponame.git:
  git.latest:
    - target: '/home/{{ username }}/src/reponame'
    - user: {{ username }}

Upvotes: 1

Related Questions