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.

221 line
5.9KB

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