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.

init.sls 5.8KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 not user.get('remove_groups', True) %}
  58. - remove_groups: False
  59. {% endif %}
  60. - groups:
  61. - {{ user_group }}
  62. {% for group in user.get('groups', []) -%}
  63. - {{ group }}
  64. {% endfor %}
  65. - require:
  66. - group: {{ user_group }}
  67. {% for group in user.get('groups', []) -%}
  68. - group: {{ group }}
  69. {% endfor %}
  70. user_keydir_{{ name }}:
  71. file.directory:
  72. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  73. - user: {{ name }}
  74. - group: {{ user_group }}
  75. - makedirs: True
  76. - mode: 700
  77. - require:
  78. - user: {{ name }}
  79. - group: {{ user_group }}
  80. {%- for group in user.get('groups', []) %}
  81. - group: {{ group }}
  82. {%- endfor %}
  83. {% if 'ssh_keys' in user %}
  84. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  85. user_{{ name }}_private_key:
  86. file.managed:
  87. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  88. - user: {{ name }}
  89. - group: {{ user_group }}
  90. - mode: 600
  91. - show_diff: False
  92. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  93. - require:
  94. - user: {{ name }}_user
  95. {% for group in user.get('groups', []) %}
  96. - group: {{ name }}_{{ group }}_group
  97. {% endfor %}
  98. user_{{ name }}_public_key:
  99. file.managed:
  100. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  101. - user: {{ name }}
  102. - group: {{ user_group }}
  103. - mode: 644
  104. - show_diff: False
  105. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  106. - require:
  107. - user: {{ name }}_user
  108. {% for group in user.get('groups', []) %}
  109. - group: {{ name }}_{{ group }}_group
  110. {% endfor %}
  111. {% endif %}
  112. {% if 'ssh_auth' in user %}
  113. {% for auth in user['ssh_auth'] %}
  114. ssh_auth_{{ name }}_{{ loop.index0 }}:
  115. ssh_auth.present:
  116. - user: {{ name }}
  117. - name: {{ auth }}
  118. - require:
  119. - file: {{ name }}_user
  120. - user: {{ name }}_user
  121. {% endfor %}
  122. {% endif %}
  123. {% if 'ssh_auth.absent' in user %}
  124. {% for auth in user['ssh_auth.absent'] %}
  125. ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  126. ssh_auth.absent:
  127. - user: {{ name }}
  128. - name: {{ auth }}
  129. - require:
  130. - file: {{ name }}_user
  131. - user: {{ name }}_user
  132. {% endfor %}
  133. {% endif %}
  134. {% if 'sudouser' in user and user['sudouser'] %}
  135. {% if not used_sudo %}
  136. {% set used_sudo = True %}
  137. include:
  138. - users.sudo
  139. {% endif %}
  140. sudoer-{{ name }}:
  141. file.managed:
  142. - name: {{ users.sudoers_dir }}{{ name }}
  143. - user: root
  144. - group: {{ users.root_group }}
  145. - mode: '0440'
  146. {% if 'sudo_rules' in user %}
  147. {% for rule in user['sudo_rules'] %}
  148. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  149. cmd.run:
  150. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  151. - stateful: True
  152. - shell: {{ users.visudo_shell }}
  153. - env:
  154. # Specify the rule via an env var to avoid shell quoting issues.
  155. - rule: "{{ name }} {{ rule }}"
  156. - require_in:
  157. - file: {{ users.sudoers_dir }}{{ name }}
  158. {% endfor %}
  159. {{ users.sudoers_dir }}{{ name }}:
  160. file.managed:
  161. - contents: |
  162. {%- for rule in user['sudo_rules'] %}
  163. {{ name }} {{ rule }}
  164. {%- endfor %}
  165. - require:
  166. - file: sudoer-defaults
  167. - file: sudoer-{{ name }}
  168. {% endif %}
  169. {% else %}
  170. {{ users.sudoers_dir }}{{ name }}:
  171. file.absent:
  172. - name: {{ users.sudoers_dir }}{{ name }}
  173. {% endif %}
  174. {% endfor %}
  175. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  176. {{ name }}:
  177. {% if 'purge' in user or 'force' in user %}
  178. user.absent:
  179. {% if 'purge' in user %}
  180. - purge: {{ user['purge'] }}
  181. {% endif %}
  182. {% if 'force' in user %}
  183. - force: {{ user['force'] }}
  184. {% endif %}
  185. {% else %}
  186. user.absent
  187. {% endif -%}
  188. {{ users.sudoers_dir }}{{ name }}:
  189. file.absent:
  190. - name: {{ users.sudoers_dir }}{{ name }}
  191. {% endfor %}
  192. {% for user in pillar.get('absent_users', []) %}
  193. {{ user }}:
  194. user.absent
  195. {{ users.sudoers_dir }}{{ user }}:
  196. file.absent:
  197. - name: {{ users.sudoers_dir }}{{ user }}
  198. {% endfor %}
  199. {% for group in pillar.get('absent_groups', []) %}
  200. {{ group }}:
  201. group.absent
  202. {% endfor %}