main.yml 3.1 KB

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