Matrix Docker Ansible eploy
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.
 
 

231 lines
7.4 KiB

  1. - name: Enable index.html creation if user doesn't wish to customise base domain
  2. delegate_to: 127.0.0.1
  3. lineinfile:
  4. path: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
  5. regexp: "^#? *{{ item.key | regex_escape() }}:"
  6. line: "{{ item.key }}: {{ item.value }}"
  7. insertafter: '# Base Domain Settings'
  8. with_dict:
  9. 'matrix_nginx_proxy_base_domain_homepage_enabled': 'true'
  10. when: customise_base_domain_website|bool == false
  11. - name: Disable index.html creation to allow multi-file site if user does wish to customise base domain
  12. delegate_to: 127.0.0.1
  13. lineinfile:
  14. path: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
  15. regexp: "^#? *{{ item.key | regex_escape() }}:"
  16. line: "{{ item.key }}: {{ item.value }}"
  17. insertafter: '# Base Domain Settings'
  18. with_dict:
  19. 'matrix_nginx_proxy_base_domain_homepage_enabled': 'false'
  20. when: customise_base_domain_website|bool == true
  21. - name: Record custom 'Customise Website + Access Export' variables locally on AWX
  22. delegate_to: 127.0.0.1
  23. lineinfile:
  24. path: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
  25. regexp: "^#? *{{ item.key | regex_escape() }}:"
  26. line: "{{ item.key }}: {{ item.value }}"
  27. insertafter: '# Custom Settings'
  28. with_dict:
  29. 'customise_base_domain_website': '{{ customise_base_domain_website }}'
  30. 'sftp_auth_method': '"{{ sftp_auth_method }}"'
  31. 'sftp_password': '"{{ sftp_password }}"'
  32. 'sftp_public_key': '"{{ sftp_public_key }}"'
  33. - name: Copy new 'matrix_vars.yml' to target machine
  34. copy:
  35. src: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
  36. dest: '/matrix/awx/matrix_vars.yml'
  37. mode: '0660'
  38. - name: Reload vars in matrix_vars.yml
  39. include_vars:
  40. file: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
  41. no_log: True
  42. - name: Save new 'Customise Website + Access Export' survey.json to the AWX tower, template
  43. delegate_to: 127.0.0.1
  44. template:
  45. src: './roles/matrix-awx/surveys/configure_website_access_export.json.j2'
  46. dest: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/configure_website_access_export.json'
  47. - name: Copy new 'Customise Website + Access Export' survey.json to target machine
  48. copy:
  49. src: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/configure_website_access_export.json'
  50. dest: '/matrix/awx/configure_website_access_export.json'
  51. mode: '0660'
  52. - name: Collect AWX admin token the hard way!
  53. delegate_to: 127.0.0.1
  54. shell: |
  55. curl -sku {{ tower_username }}:{{ tower_password }} -H "Content-Type: application/json" -X POST -d '{"description":"Tower CLI", "application":null, "scope":"write"}' https://{{ tower_host }}/api/v2/users/1/personal_tokens/ | jq '.token' | sed -r 's/\"//g'
  56. register: tower_token
  57. no_log: True
  58. - name: Recreate 'Customise Base Domain Export' job template
  59. delegate_to: 127.0.0.1
  60. awx.awx.tower_job_template:
  61. name: "{{ matrix_domain }} - 1 - Configure Website + Access Export"
  62. description: "Configure base domain website settings and access the servers export."
  63. extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
  64. job_type: run
  65. job_tags: "start,setup-nginx-proxy"
  66. inventory: "{{ member_id }}"
  67. project: "{{ member_id }} - Matrix Docker Ansible Deploy"
  68. playbook: setup.yml
  69. credential: "{{ member_id }} - AWX SSH Key"
  70. survey_enabled: true
  71. survey_spec: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/configure_website_access_export.json') }}"
  72. become_enabled: yes
  73. state: present
  74. verbosity: 1
  75. tower_host: "https://{{ tower_host }}"
  76. tower_oauthtoken: "{{ tower_token.stdout }}"
  77. validate_certs: yes
  78. - name: Ensure group "sftp" exists
  79. group:
  80. name: sftp
  81. state: present
  82. - name: If user doesn't define a sftp_password, create a disabled 'sftp' account
  83. user:
  84. name: sftp
  85. comment: SFTP user to set custom web files and access servers export
  86. shell: /bin/false
  87. home: /home/sftp
  88. group: sftp
  89. password: '*'
  90. update_password: always
  91. when: sftp_password|length == 0
  92. - name: If user defines sftp_password, enable account and set password on 'stfp' account
  93. user:
  94. name: sftp
  95. comment: SFTP user to set custom web files and access servers export
  96. shell: /bin/false
  97. home: /home/sftp
  98. group: sftp
  99. password: "{{ sftp_password | password_hash('sha512') }}"
  100. update_password: always
  101. when: sftp_password|length > 0
  102. - name: adding existing user 'sftp' to group matrix
  103. user:
  104. name: sftp
  105. groups: matrix
  106. append: yes
  107. - name: Create the ro /chroot directory with sticky bit if it doesn't exist. (/chroot/website has matrix:matrix permissions and is mounted to nginx container)
  108. file:
  109. path: /chroot
  110. state: directory
  111. owner: root
  112. group: root
  113. mode: '1755'
  114. - name: Ensure /chroot/website location exists.
  115. file:
  116. path: /chroot/website
  117. state: directory
  118. owner: matrix
  119. group: matrix
  120. mode: '0574'
  121. - name: Ensure /chroot/export location exists
  122. file:
  123. path: /chroot/export
  124. state: directory
  125. owner: sftp
  126. group: sftp
  127. mode: '0700'
  128. - name: Ensure /home/sftp/.ssh location exists
  129. file:
  130. path: /home/sftp/.ssh
  131. state: directory
  132. owner: sftp
  133. group: sftp
  134. mode: '0700'
  135. - name: Ensure /home/sftp/authorized_keys exists
  136. file:
  137. path: /home/sftp/.ssh/authorized_keys
  138. state: touch
  139. owner: sftp
  140. group: sftp
  141. mode: '0644'
  142. - name: Clear authorized_keys file
  143. shell: echo "" > /home/sftp/.ssh/authorized_keys
  144. - name: Insert public SSH key into authorized_keys file
  145. lineinfile:
  146. path: /home/sftp/.ssh/authorized_keys
  147. line: "{{ sftp_public_key }}"
  148. owner: sftp
  149. group: sftp
  150. mode: '0644'
  151. when: (sftp_public_key | length > 0) and (sftp_auth_method == "SSH Key")
  152. - name: Alter SSH Subsystem State 1
  153. lineinfile:
  154. path: /etc/ssh/sshd_config
  155. line: "Subsystem sftp /usr/lib/openssh/sftp-server"
  156. state: absent
  157. - name: Alter SSH Subsystem State 2
  158. lineinfile:
  159. path: /etc/ssh/sshd_config
  160. insertafter: "^# override default of no subsystems"
  161. line: "Subsystem sftp internal-sftp"
  162. - name: Add SSH Match User section for disabled auth
  163. blockinfile:
  164. path: /etc/ssh/sshd_config
  165. state: absent
  166. block: |
  167. Match User sftp
  168. ChrootDirectory /chroot
  169. PermitTunnel no
  170. X11Forwarding no
  171. AllowTcpForwarding no
  172. PasswordAuthentication yes
  173. AuthorizedKeysFile /home/sftp/.ssh/authorized_keys
  174. when: sftp_auth_method == "Disabled"
  175. - name: Add SSH Match User section for password auth
  176. blockinfile:
  177. path: /etc/ssh/sshd_config
  178. state: present
  179. block: |
  180. Match User sftp
  181. ChrootDirectory /chroot
  182. PermitTunnel no
  183. X11Forwarding no
  184. AllowTcpForwarding no
  185. PasswordAuthentication yes
  186. when: sftp_auth_method == "Password"
  187. - name: Add SSH Match User section for publickey auth
  188. blockinfile:
  189. path: /etc/ssh/sshd_config
  190. state: present
  191. block: |
  192. Match User sftp
  193. ChrootDirectory /chroot
  194. PermitTunnel no
  195. X11Forwarding no
  196. AllowTcpForwarding no
  197. AuthorizedKeysFile /home/sftp/.ssh/authorized_keys
  198. when: sftp_auth_method == "SSH Key"
  199. - name: Restart service ssh.service
  200. service:
  201. name: ssh.service
  202. state: restarted