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.
 
 

141 lines
5.5 KiB

  1. ---
  2. - name: Ensure Matrix Synapse paths exists
  3. file:
  4. path: "{{ item }}"
  5. state: directory
  6. mode: 0750
  7. owner: "{{ matrix_user_username }}"
  8. group: "{{ matrix_user_username }}"
  9. with_items:
  10. - "{{ matrix_synapse_base_path }}"
  11. - "{{ matrix_synapse_config_dir_path }}"
  12. - "{{ matrix_synapse_run_path }}"
  13. - "{{ matrix_synapse_media_store_path }}"
  14. - name: Ensure Matrix Docker image is pulled
  15. docker_image:
  16. name: "{{ docker_matrix_image }}"
  17. - name: Check if a Matrix Synapse configuration exists
  18. stat:
  19. path: "{{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  20. register: matrix_synapse_config_stat
  21. - name: Generate initial Matrix config
  22. docker_container:
  23. name: matrix-config
  24. image: "{{ docker_matrix_image }}"
  25. detach: no
  26. cleanup: yes
  27. command: generate
  28. env:
  29. SERVER_NAME: "{{ hostname_matrix }}"
  30. REPORT_STATS: "no"
  31. user: "{{ matrix_user_uid }}:{{ matrix_user_gid }}"
  32. volumes:
  33. - "{{ matrix_synapse_config_dir_path }}:/data"
  34. when: "not matrix_synapse_config_stat.stat.exists"
  35. - name: Ensure self-signed certificates are removed
  36. file:
  37. path: "{{ item }}"
  38. state: absent
  39. with_items:
  40. - "{{ matrix_synapse_config_dir_path }}/{{ hostname_matrix }}.tls.crt"
  41. - "{{ matrix_synapse_config_dir_path }}/{{ hostname_matrix }}.tls.key"
  42. - name: Augment Matrix log config
  43. lineinfile: "dest={{ matrix_synapse_config_dir_path }}/{{ hostname_matrix }}.log.config"
  44. args:
  45. regexp: "{{ item.regexp }}"
  46. line: '{{ item.line }}'
  47. with_items:
  48. - {"regexp": "^ filename:", "line": ' filename: /matrix-run/homeserver.log'}
  49. - {"regexp": "^ maxBytes:", "line": ' maxBytes: {{ matrix_max_log_file_size_mb * 1024 * 1024 }}'}
  50. - {"regexp": "^ backupCount:", "line": ' backupCount: {{ matrix_max_log_files_count }}'}
  51. - name: Augment Matrix config
  52. lineinfile: "dest={{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  53. args:
  54. regexp: "{{ item.regexp }}"
  55. line: '{{ item.line }}'
  56. with_items:
  57. - {"regexp": "^log_file:", "line": 'log_file: "/matrix-run/homeserver.log"'}
  58. - {"regexp": "^tls_certificate_path:", "line": 'tls_certificate_path: "/acmetool-certs/live/{{ hostname_matrix }}/fullchain"'}
  59. - {"regexp": "^tls_private_key_path:", "line": 'tls_private_key_path: "/acmetool-certs/live/{{ hostname_matrix }}/privkey"'}
  60. - {"regexp": "^server_name:", "line": 'server_name: "{{ hostname_identity }}"'}
  61. - {"regexp": "^turn_allow_guests:", "line": 'turn_allow_guests: False'}
  62. - {"regexp": "^url_preview_enabled:", "line": 'url_preview_enabled: True'}
  63. - {"regexp": "^max_upload_size:", "line": 'max_upload_size: "{{ matrix_max_upload_size_mb }}M"'}
  64. - {"regexp": "^media_store_path:", "line": 'media_store_path: "/matrix-media-store"'}
  65. - name: Augment Matrix config (specify URL previews blacklist)
  66. lineinfile: "dest={{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  67. args:
  68. regexp: "^url_preview_ip_range_blacklist:"
  69. line: 'url_preview_ip_range_blacklist: ["127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "169.254.0.0/16"]'
  70. insertafter: '^# url_preview_ip_range_blacklist:$'
  71. # We only wish to do this for the 8008 port and not for the 8448 port
  72. # (2nd instance of `x_forwarded` found in the config)
  73. - name: Augment Matrix config (mark 8008 plain traffic as forwarded)
  74. replace: "dest={{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  75. args:
  76. regexp: "8008((?:.|\n)*)x_forwarded(.*)"
  77. replace: '8008\g<1>x_forwarded: true'
  78. - name: Augment Matrix config (change database from SQLite to Postgres)
  79. lineinfile:
  80. dest: "{{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  81. regexp: '(.*)name: "sqlite3"'
  82. line: '\1name: "psycopg2"'
  83. backrefs: yes
  84. - name: Augment Matrix config (add the Postgres connection parameters)
  85. lineinfile:
  86. dest: "{{ matrix_synapse_config_dir_path }}/homeserver.yaml"
  87. regexp: '(.*)database: "(.*)homeserver.db"'
  88. line: '\1user: "{{ matrix_postgres_connection_username }}"\n\1password: "{{ matrix_postgres_connection_password }}"\n\1database: "homeserver"\n\1host: "postgres"\n\1cp_min: 5\n\1cp_max: 10'
  89. backrefs: yes
  90. - name: Augment Matrix config (configure Coturn)
  91. lineinfile: "dest={{ matrix_synapse_config_dir_path }}/turnserver.conf"
  92. args:
  93. regexp: "^{{ item.variable }}="
  94. line: '{{ item.variable }}={{ item.value }}'
  95. with_items:
  96. - {'variable': 'min-port', 'value': "{{ matrix_coturn_turn_udp_min_port }}"}
  97. - {'variable': 'max-port', 'value': "{{ matrix_coturn_turn_udp_max_port }}"}
  98. - {'variable': 'external-ip', 'value': "{{ matrix_coturn_turn_external_ip_address }}"}
  99. - name: Allow access to Matrix ports in firewalld
  100. firewalld:
  101. port: "{{ item }}"
  102. state: enabled
  103. immediate: yes
  104. permanent: yes
  105. with_items:
  106. - '8448/tcp' # Matrix federation
  107. - '3478/tcp' # STUN
  108. - '3478/udp' # STUN
  109. - "{{ matrix_coturn_turn_udp_min_port }}-{{ matrix_coturn_turn_udp_max_port }}/udp" # TURN
  110. - name: Ensure matrix-synapse.service installed
  111. template:
  112. src: "{{ role_path }}/templates/systemd/matrix-synapse.service.j2"
  113. dest: "/etc/systemd/system/matrix-synapse.service"
  114. mode: 0644
  115. - name: Ensure matrix-synapse-register-user script created
  116. template:
  117. src: "{{ role_path }}/templates/usr-local-bin/matrix-synapse-register-user.j2"
  118. dest: "/usr/local/bin/matrix-synapse-register-user"
  119. mode: 0750
  120. - name: Ensure periodic restarting of Matrix is configured (for SSL renewal)
  121. template:
  122. src: "{{ role_path }}/templates/cron.d/matrix-periodic-restarter.j2"
  123. dest: "/etc/cron.d/matrix-periodic-restarter"
  124. mode: 0600