Saltstack Official Users Formula
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

178 líneas
5.2KB

  1. # -*- coding: utf-8 -*-
  2. # vim: ft=sls
  3. {##
  4. Name: users/addusers.sls
  5. Description:
  6. This file removes users
  7. #}
  8. {% from "users/map.jinja" import users_settings with context %}
  9. {% for name, user in users_settings.items() %}
  10. {% if user.absent is not defined or not user.absent or user != None %}
  11. {% set home = user.get('home', "/home/%s" % name) %}
  12. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  13. {%- set user_group = user.prime_group.name -%}
  14. {%- else -%}
  15. {%- set user_group = name -%}
  16. {%- endif %}
  17. {% for group in user.get('groups', []) %}
  18. users-{{ name }}-{{ group }}-group:
  19. group:
  20. - name: {{ group }}
  21. - present
  22. {% endfor %}
  23. users-{{ name }}-user:
  24. {% if user.get('createhome', True) %}
  25. file.directory:
  26. - name: {{ home }}
  27. - user: {{ name }}
  28. - group: {{ user_group }}
  29. - mode: {{ user.get('user_dir_mode', '0750') }}
  30. {%- endif %}
  31. group.present:
  32. - name: {{ user_group }}
  33. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  34. - gid: {{ user['prime_group']['gid'] }}
  35. {%- elif 'uid' in user %}
  36. - gid: {{ user['uid'] }}
  37. {%- endif %}
  38. user.present:
  39. - name: {{ name }}
  40. - home: {{ home }}
  41. - shell: {{ user.get('shell', users.get('shell', '/bin/bash')) }}
  42. {% if 'uid' in user -%}
  43. - uid: {{ user['uid'] }}
  44. {% endif -%}
  45. {% if 'password' in user -%}
  46. - password: '{{ user['password'] }}'
  47. {% endif -%}
  48. {% if 'enforce_password' in user -%}
  49. - enforce_password: {{ user['enforce_password'] }}
  50. {% endif -%}
  51. {% if user.get('system', False) -%}
  52. - system: True
  53. {% endif -%}
  54. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  55. - gid: {{ user['prime_group']['gid'] }}
  56. {% else -%}
  57. - gid_from_name: True
  58. {% endif -%}
  59. {% if 'fullname' in user %}
  60. - fullname: {{ user['fullname'] }}
  61. {% endif -%}
  62. {% if not user.get('createhome', True) %}
  63. - createhome: False
  64. {% endif %}
  65. {% if 'expire' in user -%}
  66. - expire: {{ user['expire'] }}
  67. {% endif -%}
  68. - remove_groups: {{ user.get('remove_groups', 'False') }}
  69. - groups:
  70. - {{ user_group }}
  71. {% for group in user.get('groups', []) -%}
  72. - {{ group }}
  73. {% endfor %}
  74. {% if 'ssh_keys' in user %}
  75. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  76. users_user_{{ name }}_private_key:
  77. file.managed:
  78. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  79. - user: {{ name }}
  80. - group: {{ user_group }}
  81. - mode: 600
  82. - show_diff: False
  83. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  84. users_user_{{ name }}_public_key:
  85. file.managed:
  86. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  87. - user: {{ name }}
  88. - group: {{ user_group }}
  89. - mode: 644
  90. - show_diff: False
  91. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  92. {% endif %}
  93. {% if 'ssh_auth_file' in user %}
  94. users_authorized_keys_{{ name }}:
  95. file.managed:
  96. - name: {{ home }}/.ssh/authorized_keys
  97. - user: {{ name }}
  98. - group: {{ name }}
  99. - mode: 600
  100. - contents: |
  101. {% for auth in user.ssh_auth_file -%}
  102. {{ auth }}
  103. {% endfor -%}
  104. {% endif %}
  105. {% if 'ssh_auth' in user %}
  106. {% for auth in user['ssh_auth'] %}
  107. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  108. ssh_auth.present:
  109. - user: {{ name }}
  110. - name: {{ auth }}
  111. {% endfor %}
  112. {% endif %}
  113. {% if 'ssh_keys_pillar' in user %}
  114. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  115. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  116. file.managed:
  117. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}
  118. - user: {{ name }}
  119. - group: {{ user_group }}
  120. - mode: 600
  121. - show_diff: False
  122. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  123. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  124. file.managed:
  125. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}.pub
  126. - user: {{ name }}
  127. - group: {{ user_group }}
  128. - mode: 644
  129. - show_diff: False
  130. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  131. {% endfor %}
  132. {% endif %}
  133. {% if 'ssh_auth_sources' in user %}
  134. {% for pubkey_file in user['ssh_auth_sources'] %}
  135. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  136. ssh_auth.present:
  137. - user: {{ name }}
  138. - source: {{ pubkey_file }}
  139. {% endfor %}
  140. {% endif %}
  141. {% if 'ssh_auth.absent' in user %}
  142. {% for auth in user['ssh_auth.absent'] %}
  143. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  144. ssh_auth.absent:
  145. - user: {{ name }}
  146. - name: {{ auth }}
  147. {% endfor %}
  148. {% endif %}
  149. {% if 'ssh_config' in user %}
  150. users_ssh_config_{{ name }}:
  151. file.managed:
  152. - name: {{ home }}/.ssh/config
  153. - user: {{ name }}
  154. - group: {{ user_group }}
  155. - mode: 640
  156. - contents: |
  157. # Managed by Saltstack
  158. # Do Not Edit
  159. {% for label, setting in user.ssh_config.items() %}
  160. # {{ label }}
  161. Host {{ setting.get('hostname') }}
  162. {%- for opts in setting.get('options') %}
  163. {{ opts }}
  164. {%- endfor %}
  165. {% endfor -%}
  166. {% endif %}
  167. {%- endif %}
  168. {% endfor %}