main.yml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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: Obtain Let's Encrypt certificate
  64. ansible.builtin.command:
  65. cmd: >
  66. certbot certonly --standalone
  67. --non-interactive --agree-tos
  68. --email {{ certbot_email }}
  69. -d {{ trojan_domain }}
  70. creates: "/etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem"
  71. - name: Grant trojan user read access to TLS certificates
  72. ansible.builtin.file:
  73. path: /etc/letsencrypt
  74. state: directory
  75. mode: "0755"
  76. - name: Ensure live directory is accessible
  77. ansible.builtin.file:
  78. path: "/etc/letsencrypt/live/{{ trojan_domain }}"
  79. state: directory
  80. mode: "0755"
  81. - name: Ensure archive directory is accessible
  82. ansible.builtin.file:
  83. path: "/etc/letsencrypt/archive/{{ trojan_domain }}"
  84. state: directory
  85. mode: "0755"
  86. - name: Deploy certbot renewal hook for trojan
  87. ansible.builtin.copy:
  88. dest: /etc/letsencrypt/renewal-hooks/post/trojan-go.sh
  89. content: |
  90. #!/bin/bash
  91. mkdir -p /etc/trojan-go/tls
  92. cp /etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem /etc/trojan-go/tls/fullchain.pem
  93. cp /etc/letsencrypt/live/{{ trojan_domain }}/privkey.pem /etc/trojan-go/tls/privkey.pem
  94. chown -R {{ trojan_user }}:{{ trojan_user }} /etc/trojan-go/tls
  95. systemctl reload trojan-go
  96. owner: root
  97. group: root
  98. mode: "0755"
  99. - name: Copy initial TLS certificates to trojan-owned directory
  100. ansible.builtin.shell: |
  101. mkdir -p /etc/trojan-go/tls
  102. cp /etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem /etc/trojan-go/tls/fullchain.pem
  103. cp /etc/letsencrypt/live/{{ trojan_domain }}/privkey.pem /etc/trojan-go/tls/privkey.pem
  104. chown -R {{ trojan_user }}:{{ trojan_user }} /etc/trojan-go/tls
  105. args:
  106. creates: /etc/trojan-go/tls/privkey.pem
  107. notify: restart trojan
  108. - name: Deploy trojan-go configuration
  109. ansible.builtin.template:
  110. src: config.json.j2
  111. dest: "{{ trojan_config_path }}"
  112. owner: "{{ trojan_user }}"
  113. group: "{{ trojan_user }}"
  114. mode: "0640"
  115. notify: restart trojan
  116. - name: Deploy trojan-go systemd unit
  117. ansible.builtin.template:
  118. src: trojan-go.service.j2
  119. dest: /etc/systemd/system/trojan-go.service
  120. owner: root
  121. group: root
  122. mode: "0644"
  123. notify: restart trojan
  124. - name: Install nginx for Trojan fallback
  125. ansible.builtin.apt:
  126. name:
  127. - nginx
  128. state: present
  129. - name: Deploy nginx fallback config
  130. ansible.builtin.template:
  131. src: nginx-fallback.conf.j2
  132. dest: /etc/nginx/conf.d/trojan-fallback.conf
  133. owner: root
  134. group: root
  135. mode: "0644"
  136. notify: restart nginx
  137. - name: Create fallback web root
  138. ansible.builtin.file:
  139. path: /var/www/trojan-fallback
  140. state: directory
  141. owner: www-data
  142. group: www-data
  143. mode: "0755"
  144. - name: Deploy fallback index page
  145. ansible.builtin.copy:
  146. content: |
  147. <!DOCTYPE html>
  148. <html>
  149. <head><title>Welcome</title></head>
  150. <body><h1>Welcome</h1></body>
  151. </html>
  152. dest: /var/www/trojan-fallback/index.html
  153. owner: www-data
  154. group: www-data
  155. mode: "0644"
  156. notify: restart nginx
  157. - name: Remove default nginx site
  158. ansible.builtin.file:
  159. path: /etc/nginx/sites-enabled/default
  160. state: absent
  161. notify: restart nginx
  162. - name: Enable and start nginx
  163. ansible.builtin.systemd:
  164. name: nginx
  165. enabled: yes
  166. state: started
  167. - name: Enable and start trojan-go service
  168. ansible.builtin.systemd:
  169. name: trojan-go
  170. daemon_reload: yes
  171. enabled: yes
  172. state: started
  173. - name: Enable certbot auto-renewal timer
  174. ansible.builtin.systemd:
  175. name: certbot.timer
  176. enabled: yes
  177. state: started
  178. - name: Allow Trojan port through UFW
  179. community.general.ufw:
  180. rule: allow
  181. port: "{{ trojan_port }}"
  182. proto: tcp