0

I have a roster with some hosts/users:

myhos1:
  host: 192.168.1.1
  user: foouser

myhost2:
  host: 192.168.1.2
  user: bazuser

Now in the state file I would like to use the username from the roster to do things, e.g.

Install VNC script:
  file.managed:
    - name: /home/<user>/test.sh
    - source: salt://files/test.sh
    - user: <user>
    - group: <user>

How do I use the template language to replace <user> with the actual user-id of the host that I apply the state to?

Would be nice if I can somehow directly access the information from the roster, but I did not find anything that would work in the relevant docu-pages.

centic
  • 15,565
  • 9
  • 68
  • 125

1 Answers1

1

You've got two options that I can think of.

If you've not many users the easiest option would be to just loop over a list using jinja

{% for user in ['foouser', 'bazuser'] %}
/home/{{ user }}/test.sh:
  file.managed:
    - source: salt://files/test.sh
    - user: {{ user }}
    - group: {{ user }}
{% endfor %}

or you should be able to do something like use cmd.run to get a list of users:

 {% for user in salt['cmd.run']('ls /home/').split('\n') %}
    ... snipped
 {% endfor %}

I have tested the above and it works for me.

cmd.run in jinja - google salt-users forum

Useful reading if you're unsure about using Jinja with Salt

grinferno
  • 524
  • 8
  • 23
  • Thanks for the suggestion! Ideally I would like to get the one single user that is used to connect to the machine in the roster-file, but your second approach will likely be a useful workaround in the meantime. – centic Oct 18 '17 at 13:04
  • @centic ah, sorry I should really put my glasses on. Have you tried using a pillar file and looping over the contents? See answer to: https://stackoverflow.com/questions/27169509/saltstack-load-pillar-in-a-for-loop. I should probably edit my question to include this – grinferno Oct 18 '17 at 14:10