main.yml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ---
  2. - name: Create trojan service user
  3. ansible.builtin.user:
  4. name: "{{ trojan_user }}"
  5. system: yes
  6. shell: /usr/sbin/nologin
  7. create_home: no
  8. - name: Create trojan config directory
  9. ansible.builtin.file:
  10. path: "{{ trojan_config_path | dirname }}"
  11. state: directory
  12. owner: "{{ trojan_user }}"
  13. group: "{{ trojan_user }}"
  14. mode: "0750"
  15. - name: Download trojan-go binary
  16. ansible.builtin.get_url:
  17. url: "https://github.com/p4gefau1t/trojan-go/releases/download/v{{ trojan_version }}/trojan-go-linux-amd64.zip"
  18. dest: /tmp/trojan-go.zip
  19. mode: "0644"
  20. - name: Create extraction directory
  21. ansible.builtin.file:
  22. path: /tmp/trojan-go-extract/
  23. state: directory
  24. mode: "0755"
  25. - name: Extract trojan-go binary
  26. ansible.builtin.unarchive:
  27. src: /tmp/trojan-go.zip
  28. dest: /tmp/trojan-go-extract/
  29. remote_src: yes
  30. creates: /tmp/trojan-go-extract/trojan-go
  31. - name: Install trojan-go binary
  32. ansible.builtin.copy:
  33. src: /tmp/trojan-go-extract/trojan-go
  34. dest: "{{ trojan_bin_path }}"
  35. remote_src: yes
  36. owner: root
  37. group: root
  38. mode: "0755"
  39. notify: restart trojan
  40. - name: Grant CAP_NET_BIND_SERVICE to trojan-go
  41. community.general.capabilities:
  42. path: "{{ trojan_bin_path }}"
  43. capability: cap_net_bind_service=+ep
  44. state: present
  45. - name: Clean up downloaded archive
  46. ansible.builtin.file:
  47. path: "{{ item }}"
  48. state: absent
  49. loop:
  50. - /tmp/trojan-go.zip
  51. - /tmp/trojan-go-extract
  52. - name: Install certbot
  53. ansible.builtin.apt:
  54. name:
  55. - certbot
  56. state: present
  57. - name: Obtain Let's Encrypt certificate
  58. ansible.builtin.command:
  59. cmd: >
  60. certbot certonly --standalone
  61. --non-interactive --agree-tos
  62. --email {{ certbot_email }}
  63. -d {{ trojan_domain }}
  64. creates: "/etc/letsencrypt/live/{{ trojan_domain }}/fullchain.pem"
  65. - name: Grant trojan user read access to TLS certificates
  66. ansible.builtin.file:
  67. path: /etc/letsencrypt
  68. state: directory
  69. mode: "0755"
  70. - name: Ensure live directory is accessible
  71. ansible.builtin.file:
  72. path: "/etc/letsencrypt/live/{{ trojan_domain }}"
  73. state: directory
  74. mode: "0755"
  75. - name: Ensure archive directory is accessible
  76. ansible.builtin.file:
  77. path: "/etc/letsencrypt/archive/{{ trojan_domain }}"
  78. state: directory
  79. mode: "0755"
  80. - name: Deploy certbot renewal hook for trojan
  81. ansible.builtin.copy:
  82. dest: /etc/letsencrypt/renewal-hooks/post/restart-trojan.sh
  83. content: |
  84. #!/bin/bash
  85. systemctl reload trojan-go
  86. owner: root
  87. group: root
  88. mode: "0755"
  89. - name: Deploy trojan-go configuration
  90. ansible.builtin.template:
  91. src: trojan-config.json.j2
  92. dest: "{{ trojan_config_path }}"
  93. owner: "{{ trojan_user }}"
  94. group: "{{ trojan_user }}"
  95. mode: "0640"
  96. notify: restart trojan
  97. - name: Deploy trojan-go systemd unit
  98. ansible.builtin.template:
  99. src: trojan.service.j2
  100. dest: /etc/systemd/system/trojan-go.service
  101. owner: root
  102. group: root
  103. mode: "0644"
  104. notify: restart trojan
  105. - name: Enable and start trojan-go service
  106. ansible.builtin.systemd:
  107. name: trojan-go
  108. daemon_reload: yes
  109. enabled: yes
  110. state: started
  111. - name: Enable certbot auto-renewal timer
  112. ansible.builtin.systemd:
  113. name: certbot.timer
  114. enabled: yes
  115. state: started