users-formula/users/init.sls

178 lines
4.4 KiB
Plaintext
Raw Normal View History

2013-06-19 21:53:46 +02:00
include:
- users.sudo
{% for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
{%- if user == None -%}
{%- set user = {} -%}
{%- endif -%}
{%- set home = user.get('home', "/home/%s" % name) -%}
{%- if 'prime_group' in user and 'name' in user['prime_group'] %}
{%- set user_group = user.prime_group.name -%}
{%- else -%}
{%- set user_group = name -%}
{%- endif %}
2013-06-19 21:53:46 +02:00
{% for group in user.get('groups', []) %}
{{ name }}_{{ group }}_group:
2013-06-19 21:53:46 +02:00
group:
- name: {{ group }}
- present
{% endfor %}
{{ name }}_user:
file.directory:
- name: {{ home }}
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- mode: 0755
- require:
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
group.present:
- name: {{ user_group }}
{%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
- gid: {{ user['prime_group']['gid'] }}
{%- elif 'uid' in user %}
- gid: {{ user['uid'] }}
{%- endif %}
2013-06-19 21:53:46 +02:00
user.present:
- name: {{ name }}
- home: {{ home }}
- shell: {{ user.get('shell', '/bin/bash') }}
2013-06-19 21:53:46 +02:00
{% if 'uid' in user -%}
- uid: {{ user['uid'] }}
{% endif -%}
{% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
- gid: {{ user['prime_group']['gid'] }}
{% else -%}
2013-06-19 21:53:46 +02:00
- gid_from_name: True
{% endif -%}
2013-06-19 21:53:46 +02:00
{% if 'fullname' in user %}
- fullname: {{ user['fullname'] }}
{% endif -%}
2013-06-19 21:53:46 +02:00
- groups:
- {{ user_group }}
{% for group in user.get('groups', []) -%}
- {{ group }}
2013-06-19 21:53:46 +02:00
{% endfor %}
- require:
- group: {{ user_group }}
{% for group in user.get('groups', []) -%}
- group: {{ group }}
2013-06-19 21:53:46 +02:00
{% endfor %}
user_keydir_{{ name }}:
file.directory:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- makedirs: True
2014-02-14 14:13:17 +01:00
- mode: 700
2013-06-19 21:53:46 +02:00
- require:
- user: {{ name }}
- group: {{ user_group }}
{%- for group in user.get('groups', []) %}
2013-06-19 21:53:46 +02:00
- group: {{ group }}
{%- endfor %}
2013-06-19 21:53:46 +02:00
{% if 'ssh_keys' in user %}
{% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
2013-06-19 21:53:46 +02:00
user_{{ name }}_private_key:
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
2013-06-19 21:53:46 +02:00
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- mode: 600
- contents_pillar: users:{{ name }}:ssh_keys:privkey
2013-06-19 21:53:46 +02:00
- require:
- user: {{ name }}_user
{% for group in user.get('groups', []) %}
- group: {{ name }}_{{ group }}_group
2013-06-19 21:53:46 +02:00
{% endfor %}
user_{{ name }}_public_key:
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
2013-06-19 21:53:46 +02:00
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- mode: 644
2014-03-30 17:00:17 +02:00
- contents_pillar: users:{{ name }}:ssh_keys:pubkey
2013-06-19 21:53:46 +02:00
- require:
- user: {{ name }}_user
{% for group in user.get('groups', []) %}
- group: {{ name }}_{{ group }}_group
2013-06-19 21:53:46 +02:00
{% endfor %}
{% endif %}
{% if 'ssh_auth' in user %}
{% for auth in user['ssh_auth'] %}
2013-06-19 21:53:46 +02:00
ssh_auth_{{ name }}_{{ loop.index0 }}:
ssh_auth.present:
- user: {{ name }}
- name: {{ auth }}
- require:
- file: {{ name }}_user
- user: {{ name }}_user
{% endfor %}
{% endif %}
{% if 'sudouser' in user and user['sudouser'] %}
sudoer-{{ name }}:
file.managed:
- name: /etc/sudoers.d/{{ name }}
- user: root
- group: root
- mode: '0440'
{% if 'sudo_rules' in user %}
/etc/sudoers.d/{{ name }}:
file.append:
- text:
{% for rule in user['sudo_rules'] %}
- "{{ name }} {{ rule }}"
{% endfor %}
- require:
- file: sudoer-defaults
- file: sudoer-{{ name }}
{% endif %}
{% else %}
/etc/sudoers.d/{{ name }}:
file.absent:
- name: /etc/sudoers.d/{{ name }}
2013-06-19 21:53:46 +02:00
{% endif %}
{% endfor %}
{% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
{{ name }}:
{% if 'purge' in user or 'force' in user %}
user.absent:
{% if 'purge' in user %}
- purge: {{ user['purge'] }}
{% endif %}
{% if 'force' in user %}
- force: {{ user['force'] }}
{% endif %}
{% else %}
user.absent
{% endif -%}
/etc/sudoers.d/{{ name }}:
file.absent:
- name: /etc/sudoers.d/{{ name }}
{% endfor %}
2013-06-19 21:53:46 +02:00
{% for user in pillar.get('absent_users', []) %}
{{ user }}:
user.absent
/etc/sudoers.d/{{ user }}:
file.absent:
- name: /etc/sudoers.d/{{ user }}
2013-06-19 21:53:46 +02:00
{% endfor %}
{% for group in pillar.get('absent_groups', []) %}
{{ group }}:
group.absent
{% endfor %}