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.2KB

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