Saltstack Official Users Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

194 行
4.9KB

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