Skip to content

Instantly share code, notes, and snippets.

@laidback
Last active May 16, 2017 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laidback/1331532e5fb94ae7de2a21574a01e9ab to your computer and use it in GitHub Desktop.
Save laidback/1331532e5fb94ae7de2a21574a01e9ab to your computer and use it in GitHub Desktop.
# users-formula/basic.sls
basic-user:
user.present:
- name: {{ name }}
- gid: {{ gid }}
############################
# state tree: vhost-user.sls
include:
- users.basic.sls
# now i want to specify basic user further to become a vhost
extend:
basic-user:
user.present:
- home: /var/www/{{ name }}
...
/var/www/{{ name }}:
file.directory:
- user: root
- group: root
@xenophonf
Copy link

As an aside, on line 11 you just need to write users.basic. The .sls part of the filename isn't included in the SLS ID.

I really think that you're better off moving the user account settings into a dictionary in Pillar. This is how I do it:
https://github.com/irtnog/salt-pillar-example/blob/master/defaults/accounts.sls

You could define the user account in a "default" Pillar similar to the above, and then either use a templating command or another Pillar SLS to modify the defaults. Here's how Pillar flattens and merges values:
https://docs.saltstack.com/en/latest/topics/pillar/#pillar-namespace-flattening
https://docs.saltstack.com/en/latest/topics/pillar/#pillar-dictionary-merging

For example, you might have the following in pillar/defaults.sls:

users:
  basic-user:
    gid: 100

And then this in pillar/www/example/com/init.sls:

users:
  basic-user:
    home: /var/www/basic-user

And this in pillar/top.sls:

base:
  '*':
    - defaults

  'web*':
    - www.example.com

So then your states would look something like this. First, states/users.sls:

{%- for user, settings in salt['pillar.get']('users', {}) %}
user_{{ loop.index0 }}:
  user.present:
    - name: {{ user|yaml_encode }}
    - gid: {{ settings['gid']|yaml_encode }}
    {%- if settings['home'] is defined %}
    - home: {{ settings['home']|yaml_encode }}
    {%- endif %}
{%- endfor %}

Finally, you'd assign the users SLS to all minions as usual, in states/top.sls:

base:
  '*':
    - users

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment