Saltstack Official Users Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.1KB

  1. # vim: sts=2 ts=2 sw=2 et ai
  2. {% from "users/map.jinja" import users with context %}
  3. include:
  4. - users.sudo
  5. {% for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
  6. {%- if user == None -%}
  7. {%- set user = {} -%}
  8. {%- endif -%}
  9. {%- set home = user.get('home', "/home/%s" % name) -%}
  10. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  11. {%- set user_group = user.prime_group.name -%}
  12. {%- else -%}
  13. {%- set user_group = name -%}
  14. {%- endif %}
  15. {% for group in user.get('groups', []) %}
  16. {{ name }}_{{ group }}_group:
  17. group:
  18. - name: {{ group }}
  19. - present
  20. {% endfor %}
  21. {{ name }}_user:
  22. file.directory:
  23. - name: {{ home }}
  24. - user: {{ name }}
  25. - group: {{ user_group }}
  26. - mode: 0755
  27. - require:
  28. - user: {{ name }}
  29. - group: {{ user_group }}
  30. group.present:
  31. - name: {{ user_group }}
  32. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  33. - gid: {{ user['prime_group']['gid'] }}
  34. {%- elif 'uid' in user %}
  35. - gid: {{ user['uid'] }}
  36. {%- endif %}
  37. user.present:
  38. - name: {{ name }}
  39. - home: {{ home }}
  40. - shell: {{ user.get('shell', '/bin/bash') }}
  41. {% if 'uid' in user -%}
  42. - uid: {{ user['uid'] }}
  43. {% endif -%}
  44. {% if 'password' in user -%}
  45. - password: {{ user['password'] }}
  46. {% endif -%}
  47. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  48. - gid: {{ user['prime_group']['gid'] }}
  49. {% else -%}
  50. - gid_from_name: True
  51. {% endif -%}
  52. {% if 'fullname' in user %}
  53. - fullname: {{ user['fullname'] }}
  54. {% endif -%}
  55. - groups:
  56. - {{ user_group }}
  57. {% for group in user.get('groups', []) -%}
  58. - {{ group }}
  59. {% endfor %}
  60. - require:
  61. - group: {{ user_group }}
  62. {% for group in user.get('groups', []) -%}
  63. - group: {{ group }}
  64. {% endfor %}
  65. user_keydir_{{ name }}:
  66. file.directory:
  67. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  68. - user: {{ name }}
  69. - group: {{ user_group }}
  70. - makedirs: True
  71. - mode: 700
  72. - require:
  73. - user: {{ name }}
  74. - group: {{ user_group }}
  75. {%- for group in user.get('groups', []) %}
  76. - group: {{ group }}
  77. {%- endfor %}
  78. {% if 'ssh_keys' in user %}
  79. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  80. user_{{ name }}_private_key:
  81. file.managed:
  82. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  83. - user: {{ name }}
  84. - group: {{ user_group }}
  85. - mode: 600
  86. - show_diff: False
  87. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  88. - require:
  89. - user: {{ name }}_user
  90. {% for group in user.get('groups', []) %}
  91. - group: {{ name }}_{{ group }}_group
  92. {% endfor %}
  93. user_{{ name }}_public_key:
  94. file.managed:
  95. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  96. - user: {{ name }}
  97. - group: {{ user_group }}
  98. - mode: 644
  99. - show_diff: False
  100. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  101. - require:
  102. - user: {{ name }}_user
  103. {% for group in user.get('groups', []) %}
  104. - group: {{ name }}_{{ group }}_group
  105. {% endfor %}
  106. {% endif %}
  107. {% if 'ssh_auth' in user %}
  108. {% for auth in user['ssh_auth'] %}
  109. ssh_auth_{{ name }}_{{ loop.index0 }}:
  110. ssh_auth.present:
  111. - user: {{ name }}
  112. - name: {{ auth }}
  113. - require:
  114. - file: {{ name }}_user
  115. - user: {{ name }}_user
  116. {% endfor %}
  117. {% endif %}
  118. {% if 'sudouser' in user and user['sudouser'] %}
  119. sudoer-{{ name }}:
  120. file.managed:
  121. - name: {{ users.sudoers_dir }}{{ name }}
  122. - user: root
  123. - group: {{ users.root_group }}
  124. - mode: '0440'
  125. {% if 'sudo_rules' in user %}
  126. {% for rule in user['sudo_rules'] %}
  127. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  128. cmd.run:
  129. - name: 'visudo -cf - <<<"$rule"'
  130. - shell: {{ users.visudo_shell }}
  131. - env:
  132. # Specify the rule via an env var to avoid shell quoting issues.
  133. - rule: "{{ name }} {{ rule }}"
  134. - require_in:
  135. - file: {{ users.sudoers_dir }}{{ name }}
  136. {% endfor %}
  137. {{ users.sudoers_dir }}{{ name }}:
  138. file.managed:
  139. - contents: |
  140. {%- for rule in user['sudo_rules'] %}
  141. {{ name }} {{ rule }}
  142. {%- endfor %}
  143. - require:
  144. - file: sudoer-defaults
  145. - file: sudoer-{{ name }}
  146. {% endif %}
  147. {% else %}
  148. {{ users.sudoers_dir }}{{ name }}:
  149. file.absent:
  150. - name: {{ users.sudoers_dir }}{{ name }}
  151. {% endif %}
  152. {% endfor %}
  153. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  154. {{ name }}:
  155. {% if 'purge' in user or 'force' in user %}
  156. user.absent:
  157. {% if 'purge' in user %}
  158. - purge: {{ user['purge'] }}
  159. {% endif %}
  160. {% if 'force' in user %}
  161. - force: {{ user['force'] }}
  162. {% endif %}
  163. {% else %}
  164. user.absent
  165. {% endif -%}
  166. {{ users.sudoers_dir }}{{ name }}:
  167. file.absent:
  168. - name: {{ users.sudoers_dir }}{{ name }}
  169. {% endfor %}
  170. {% for user in pillar.get('absent_users', []) %}
  171. {{ user }}:
  172. user.absent
  173. {{ users.sudoers_dir }}{{ user }}:
  174. file.absent:
  175. - name: {{ users.sudoers_dir }}{{ user }}
  176. {% endfor %}
  177. {% for group in pillar.get('absent_groups', []) %}
  178. {{ group }}:
  179. group.absent
  180. {% endfor %}