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.

149 lines
3.6KB

  1. include:
  2. - users.sudo
  3. {% for name, user in pillar.get('users', {}).items() %}
  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. {{ 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 'prime_group' in user and 'gid' in user['prime_group'] -%}
  43. - gid: {{ user['prime_group']['gid'] }}
  44. {% else -%}
  45. - gid_from_name: True
  46. {% endif -%}
  47. {% if 'fullname' in user %}
  48. - fullname: {{ user['fullname'] }}
  49. {% endif -%}
  50. - groups:
  51. - {{ user_group }}
  52. {% for group in user.get('groups', []) -%}
  53. - {{ group }}
  54. {% endfor %}
  55. - require:
  56. - group: {{ user_group }}
  57. {% for group in user.get('groups', []) -%}
  58. - group: {{ group }}
  59. {% endfor %}
  60. user_keydir_{{ name }}:
  61. file.directory:
  62. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  63. - user: {{ name }}
  64. - group: {{ user_group }}
  65. - makedirs: True
  66. - mode: 744
  67. - require:
  68. - user: {{ name }}
  69. - group: {{ user_group }}
  70. {%- for group in user.get('groups', []) %}
  71. - group: {{ group }}
  72. {%- endfor %}
  73. {% if 'privkey' in user %}
  74. user_{{ name }}_private_key:
  75. file.managed:
  76. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/id_rsa
  77. - user: {{ name }}
  78. - group: {{ user_group }}
  79. - mode: 600
  80. - source: salt://keys/{{ user['privkey'] }}
  81. - require:
  82. - user: {{ name }}_user
  83. {% for group in user.get('groups', []) %}
  84. - group: {{ group }}_group
  85. {% endfor %}
  86. user_{{ name }}_public_key:
  87. file.managed:
  88. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/id_rsa.pub
  89. - user: {{ name }}
  90. - group: {{ name }}
  91. - mode: 644
  92. - source: salt://keys/{{ user['privkey'] }}.pub
  93. - require:
  94. - user: {{ name }}_user
  95. {% for group in user.get('groups', []) %}
  96. - group: {{ group }}_group
  97. {% endfor %}
  98. {% endif %}
  99. {% if 'ssh_auth' in user %}
  100. {% for auth in user['ssh_auth'] %}
  101. ssh_auth_{{ name }}_{{ loop.index0 }}:
  102. ssh_auth.present:
  103. - user: {{ name }}
  104. - name: {{ auth }}
  105. - require:
  106. - file: {{ name }}_user
  107. - user: {{ name }}_user
  108. {% endfor %}
  109. {% endif %}
  110. {% if 'sudouser' in user and user['sudouser'] %}
  111. sudoer-{{ name }}:
  112. file.managed:
  113. - name: /etc/sudoers.d/{{ name }}
  114. - user: root
  115. - group: root
  116. - mode: '0440'
  117. /etc/sudoers.d/{{ name }}:
  118. file.append:
  119. - text:
  120. - "{{ name }} ALL=(ALL) NOPASSWD: ALL"
  121. - require:
  122. - file: sudoer-defaults
  123. - file: sudoer-{{ name }}
  124. {% else %}
  125. /etc/sudoers.d/{{ name }}:
  126. file.absent:
  127. - name: /etc/sudoers.d/{{ name }}
  128. {% endif %}
  129. {% endfor %}
  130. {% for user in pillar.get('absent_users', []) %}
  131. {{ user }}:
  132. user.absent
  133. /etc/sudoers.d/{{ user }}:
  134. file.absent:
  135. - name: /etc/sudoers.d/{{ user }}
  136. {% endfor %}