main.yml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ---
  2. - name: Assert trojan_domain is configured for this host
  3. ansible.builtin.assert:
  4. that:
  5. - trojan_domain is defined and trojan_domain | length > 0
  6. - certbot_email is defined and certbot_email | length > 0
  7. fail_msg: "trojan_domain and certbot_email must be defined for each host in the trojan group. Set them in inventory/hosts.yml."
  8. - name: Create trojan service user
  9. ansible.builtin.user:
  10. name: "{{ trojan_user }}"
  11. system: yes
  12. shell: /usr/sbin/nologin
  13. create_home: no
  14. - name: Create trojan config directory
  15. ansible.builtin.file:
  16. path: "{{ trojan_config_path | dirname }}"
  17. state: directory
  18. owner: "{{ trojan_user }}"
  19. group: "{{ trojan_user }}"
  20. mode: "0750"
  21. - name: Download trojan-go binary
  22. ansible.builtin.get_url:
  23. url: "https://github.com/p4gefau1t/trojan-go/releases/download/v{{ trojan_version }}/trojan-go-linux-amd64.zip"
  24. dest: /tmp/trojan-go.zip
  25. mode: "0644"
  26. - name: Create extraction directory
  27. ansible.builtin.file:
  28. path: /tmp/trojan-go-extract/
  29. state: directory
  30. mode: "0755"
  31. - name: Extract trojan-go binary
  32. ansible.builtin.unarchive:
  33. src: /tmp/trojan-go.zip
  34. dest: /tmp/trojan-go-extract/
  35. remote_src: yes
  36. creates: /tmp/trojan-go-extract/trojan-go
  37. - name: Install trojan-go binary
  38. ansible.builtin.copy:
  39. src: /tmp/trojan-go-extract/trojan-go
  40. dest: "{{ trojan_bin_path }}"
  41. remote_src: yes
  42. owner: root
  43. group: root
  44. mode: "0755"
  45. notify: restart trojan
  46. - name: Grant CAP_NET_BIND_SERVICE to trojan-go
  47. community.general.capabilities:
  48. path: "{{ trojan_bin_path }}"
  49. capability: cap_net_bind_service=+ep
  50. state: present
  51. - name: Clean up downloaded archive
  52. ansible.builtin.file:
  53. path: "{{ item }}"
  54. state: absent
  55. loop:
  56. - /tmp/trojan-go.zip
  57. - /tmp/trojan-go-extract
  58. - name: Install certbot
  59. ansible.builtin.apt:
  60. name:
  61. - certbot
  62. state: present
  63. - name: Allow HTTP port through UFW for certbot ACME validation
  64. community.general.ufw:
  65. rule: allow
  66. port: 80
  67. proto: tcp
  68. - name: Obtain Let's Encrypt certificate
  69. ansible.builtin.command:
  70. cmd: >
  71. certbot certonly --standalone
  72. --non-interactive --agree-tos
  73. --email {{ certbot_email }}
  74. -d {{ trojan_domain }}
  75. creates: "/etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem"
  76. - name: Grant trojan user read access to TLS certificates
  77. ansible.builtin.file:
  78. path: /etc/letsencrypt
  79. state: directory
  80. mode: "0755"
  81. - name: Ensure live directory is accessible
  82. ansible.builtin.file:
  83. path: "/etc/letsencrypt/live/{{ trojan_domain }}"
  84. state: directory
  85. mode: "0755"
  86. - name: Ensure archive directory is accessible
  87. ansible.builtin.file:
  88. path: "/etc/letsencrypt/archive/{{ trojan_domain }}"
  89. state: directory
  90. mode: "0755"
  91. - name: Deploy certbot renewal hook for trojan
  92. ansible.builtin.copy:
  93. dest: /etc/letsencrypt/renewal-hooks/post/trojan-go.sh
  94. content: |
  95. #!/bin/bash
  96. mkdir -p /etc/trojan-go/tls
  97. cp /etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem /etc/trojan-go/tls/fullchain.pem
  98. cp /etc/letsencrypt/live/{{ trojan_domain }}/privkey.pem /etc/trojan-go/tls/privkey.pem
  99. chown -R {{ trojan_user }}:{{ trojan_user }} /etc/trojan-go/tls
  100. systemctl reload trojan-go
  101. owner: root
  102. group: root
  103. mode: "0755"
  104. - name: Copy initial TLS certificates to trojan-owned directory
  105. ansible.builtin.shell: |
  106. mkdir -p /etc/trojan-go/tls
  107. cp /etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem /etc/trojan-go/tls/fullchain.pem
  108. cp /etc/letsencrypt/live/{{ trojan_domain }}/privkey.pem /etc/trojan-go/tls/privkey.pem
  109. chown -R {{ trojan_user }}:{{ trojan_user }} /etc/trojan-go/tls
  110. args:
  111. creates: /etc/trojan-go/tls/privkey.pem
  112. notify: restart trojan
  113. - name: Deploy trojan-go configuration
  114. ansible.builtin.template:
  115. src: config.json.j2
  116. dest: "{{ trojan_config_path }}"
  117. owner: "{{ trojan_user }}"
  118. group: "{{ trojan_user }}"
  119. mode: "0640"
  120. notify: restart trojan
  121. - name: Deploy trojan-go systemd unit
  122. ansible.builtin.template:
  123. src: trojan-go.service.j2
  124. dest: /etc/systemd/system/trojan-go.service
  125. owner: root
  126. group: root
  127. mode: "0644"
  128. notify: restart trojan
  129. - name: Install nginx for Trojan fallback
  130. ansible.builtin.apt:
  131. name:
  132. - nginx
  133. state: present
  134. - name: Deploy nginx fallback config
  135. ansible.builtin.template:
  136. src: nginx-fallback.conf.j2
  137. dest: /etc/nginx/conf.d/trojan-fallback.conf
  138. owner: root
  139. group: root
  140. mode: "0644"
  141. notify: restart nginx
  142. - name: Create fallback web root
  143. ansible.builtin.file:
  144. path: /var/www/trojan-fallback
  145. state: directory
  146. owner: www-data
  147. group: www-data
  148. mode: "0755"
  149. - name: Deploy fallback index page
  150. ansible.builtin.copy:
  151. content: |
  152. <!DOCTYPE html>
  153. <html>
  154. <head><title>Welcome</title></head>
  155. <body><h1>Welcome</h1></body>
  156. </html>
  157. dest: /var/www/trojan-fallback/index.html
  158. owner: www-data
  159. group: www-data
  160. mode: "0644"
  161. notify: restart nginx
  162. - name: Remove default nginx site
  163. ansible.builtin.file:
  164. path: /etc/nginx/sites-enabled/default
  165. state: absent
  166. notify: restart nginx
  167. - name: Enable and start nginx
  168. ansible.builtin.systemd:
  169. name: nginx
  170. enabled: yes
  171. state: started
  172. - name: Enable and start trojan-go service
  173. ansible.builtin.systemd:
  174. name: trojan-go
  175. daemon_reload: yes
  176. enabled: yes
  177. state: started
  178. - name: Enable certbot auto-renewal timer
  179. ansible.builtin.systemd:
  180. name: certbot.timer
  181. enabled: yes
  182. state: started
  183. - name: Allow Trojan port through UFW
  184. community.general.ufw:
  185. rule: allow
  186. port: "{{ trojan_port }}"
  187. proto: tcp