chrisluiz16

Letsencrypt ansible module

Jul 5th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ####---------------------------------- Tasks --------------------####
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ##### Nginx #####
  12.  
  13.  
  14. [ec2-user@ip-172-31-10-148 nginx-project]$ tree
  15. .
  16. ├── ansible.cfg
  17. ├── aws.pem
  18. ├── inventory
  19. ├── lineinfile.yml
  20. ├── main.yml
  21. └── roles
  22. └── nginx
  23. ├── defaults
  24. │   └── main.yml
  25. ├── files
  26. ├── handlers
  27. │   └── main.yml
  28. ├── meta
  29. │   └── main.yml
  30. ├── README.md
  31. ├── tasks
  32. │   └── main.yml
  33. ├── templates
  34. │   └── nginx.conf.tmpl
  35. ├── tests
  36. │   ├── inventory
  37. │   └── test.yml
  38. └── vars
  39. └── main.yml
  40.  
  41.  
  42.  
  43.  
  44. #### Role task ###
  45.  
  46. ---
  47. # tasks file for nginx
  48.  
  49. - name: "Installing nginx on remote server"
  50. yum:
  51. name: "{{ packages }}"
  52. state: present
  53. - name: "Checking the status of the file"
  54. stat:
  55. path: "/etc/nginx/conf.d/{{ domain_name }}.conf"
  56. register: nginx_conf
  57.  
  58. - name: "Creating an nginx conf for the domain {{ domain_name }}"
  59. when: nginx_conf.stat.exists == false
  60. template:
  61. src: nginx.conf.tmpl
  62. dest: "/etc/nginx/conf.d/{{ domain_name }}.conf"
  63. notify:
  64. - restart-nginx
  65. - restart-php
  66.  
  67.  
  68. #### nginx.conf.tmpl ####
  69.  
  70. [ec2-user@ip-172-31-10-148 nginx-project]$ cat roles/nginx/templates/nginx.conf.tmpl
  71. server{
  72. server_name {{ domain_name }};
  73. listen 80;
  74. listen [::]:80;
  75.  
  76. root /var/www/html/{{ domain_name }};
  77. index index.php;
  78. error_page 404 /404.html;
  79.  
  80. add_header X-FRAME-Options "SAMEORIGIN";
  81. add_header X-Content-Type-Options "nosniff";
  82.  
  83.  
  84. location / {
  85. try_files $uri $uri/ /index.php?query_string;
  86. }
  87.  
  88.  
  89. location ~ \.php$ {
  90. include fastcgi_params;
  91. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  92. fastcgi_pass unix:/var/run/php-fpm/www.sock;
  93. }
  94.  
  95. location ~ /\.ht {
  96. deny all;
  97. }
  98.  
  99. location ~ /\.(?!well-known).* {
  100. deny all;
  101. }
  102.  
  103. location ~* \.(?:jpg|jpeg|gif|css|png|ico|svg|html)$ {
  104. access_log off;
  105. expires max;
  106. }
  107.  
  108.  
  109. gzip on;
  110. gzip_proxied no-cache no-store private expired auth;
  111. }
  112.  
  113.  
  114.  
  115. #### Main playbook ####
  116.  
  117.  
  118. ---
  119.  
  120. - name: "Uploading the git PHP website using nginx role"
  121. hosts: test
  122. become: true
  123. roles:
  124. - nginx
  125. vars:
  126. git_url: "https://github.com/Chris-luiz-16/aws-elb-site.git"
  127. ssl_dir: /etc/nginx/ssl
  128. acme_directory: "https://acme-v02.api.letsencrypt.org/directory"
  129. tasks:
  130. - name: "Uploading the git contents to the remote server"
  131. git:
  132. repo: "{{ git_url }}"
  133. dest: /tmp/website
  134. register: git_status
  135. - name: "Copying the contents to the default document root /var/www/html/{{ domain_name }}"
  136. when: git_status.changed == true
  137. copy:
  138. src: /tmp/website/
  139. dest: "/var/www/html/{{ domain_name }}/"
  140. owner: "{{ nginx_owner }}"
  141. group: "{{ nginx_owner }}"
  142. remote_src: true
  143. notify:
  144. - restart-php
  145. - name: "Nginx restart required for Letsencrypt validation"
  146. service:
  147. name: nginx
  148. state: restarted
  149. - name: "Create a letsencrypt directory for nginx"
  150. file:
  151. path: "{{ ssl_dir }}/{{ item }}"
  152. state: directory
  153. with_items:
  154. - account
  155. - certs
  156. - csrs
  157. - keys
  158. - name: "Generate a account key for acme_module"
  159. community.crypto.openssl_privatekey:
  160. path: "{{ ssl_dir }}/account/account.key"
  161. type: RSA
  162. size: 4096
  163. - name: "Generate a private key for {{ domain_name }}"
  164. community.crypto.openssl_privatekey:
  165. path: "{{ ssl_dir }}/keys/{{ domain_name }}.key"
  166. type: RSA
  167. size: 4096
  168. - name: "Generate an csr with the common_name as {{ domain_name }}"
  169. community.crypto.openssl_csr:
  170. path: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
  171. privatekey_path: "{{ ssl_dir }}/keys/{{ domain_name }}.key"
  172. common_name: "{{ domain_name }}"
  173. - name: "First challenge for {{ domain_name }} using the csr and account key"
  174. community.crypto.acme_certificate:
  175. acme_directory: "{{ acme_directory }}"
  176. acme_version: "2"
  177. account_key_src: "{{ ssl_dir }}/account/account.key"
  178. account_email: "admin@{{ domain_name }}"
  179. terms_agreed: true
  180. challenge: http-01
  181. csr: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
  182. dest: "{{ ssl_dir }}/certs/{{ domain_name }}.crt"
  183. fullchain_dest: "{{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt"
  184. register: sample_com_challenge
  185. - name: "Create .well-known/acme-challenge directory"
  186. file:
  187. path: "/var/www/html/{{ domain_name }}/.well-known/acme-challenge"
  188. state: directory
  189. owner: "{{ nginx_owner }}"
  190. group: "{{ nginx_owner }}"
  191. - name: "Copy http-01 challenges to well-known/acme-challenge directory"
  192. copy:
  193. dest: "/var/www/html/{{ item.key }}/{{ item.value['http-01']['resource'] }}"
  194. content: "{{ item.value['http-01']['resource_value'] }}"
  195. owner: "{{ nginx_owner }}"
  196. group: "{{ nginx_owner }}"
  197. loop: "{{ sample_com_challenge.challenge_data | dict2items }}"
  198. when: sample_com_challenge is changed
  199. - name: "Final letsencrypt verification and saving the full chain file "
  200. community.crypto.acme_certificate:
  201. acme_directory: "{{ acme_directory }}"
  202. acme_version: "2"
  203. account_key_src: "{{ ssl_dir }}/account/account.key"
  204. account_email: "admin@{{ domain_name }}"
  205. terms_agreed: true
  206. challenge: http-01
  207. csr: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
  208. dest: "{{ ssl_dir }}/certs/{{ domain_name }}.crt"
  209. fullchain_dest: "{{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt"
  210. data: "{{ sample_com_challenge }}"
  211. notify:
  212. - restart-nginx
  213. - name: "Activate SSL in nginx conf"
  214. blockinfile:
  215. path: "/etc/nginx/conf.d/{{ domain_name }}.conf"
  216. marker: "##<!-- {mark} ANSIBLE MANAGED BLOCK -->"
  217. insertafter: "root /var/www/html/{{ domain_name }};"
  218. block: |
  219. listen 443 ssl;
  220. ssl_certificate {{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt;
  221. ssl_certificate_key {{ ssl_dir }}/keys/{{ domain_name }}.key;
  222. if ($scheme = http) {
  223. return 301 https://$server_name$request_uri;
  224. }
  225. notify:
  226. - restart-nginx
  227.  
  228.  
  229.  
  230.  
  231.  
  232. #### output ####
  233.  
  234. [ec2-user@ip-172-31-10-148 nginx-project]$ ansible-playbook -i inventory main.yml -e domain_name=amazon.chrisich.fun
  235.  
  236. PLAY [Uploading the git PHP website using nginx role] ******************************************************************
  237.  
  238. TASK [Gathering Facts] *************************************************************************************************
  239. ok: [flipkart]
  240.  
  241. TASK [nginx : Installing nginx on remote server] ***********************************************************************
  242. changed: [flipkart]
  243.  
  244. TASK [nginx : Checking the status of the file] *************************************************************************
  245. ok: [flipkart]
  246.  
  247. TASK [nginx : Creating an nginx conf for the domain amazon.chrisich.fun] ***********************************************
  248. changed: [flipkart]
  249.  
  250. TASK [Uploading the git contents to the remote server] *****************************************************************
  251. changed: [flipkart]
  252.  
  253. TASK [Copying the contents to the default document root /var/www/html/amazon.chrisich.fun] *****************************
  254. changed: [flipkart]
  255.  
  256. TASK [Nginx restart required for Letsencrypt validation] ***************************************************************
  257. changed: [flipkart]
  258.  
  259. TASK [Create a letsencrypt directory for nginx] ************************************************************************
  260. changed: [flipkart] => (item=account)
  261. changed: [flipkart] => (item=certs)
  262. changed: [flipkart] => (item=csrs)
  263. changed: [flipkart] => (item=keys)
  264.  
  265. TASK [Generate a account key for acme_module] **************************************************************************
  266. changed: [flipkart]
  267.  
  268. TASK [Generate a private key for amazon.chrisich.fun] ******************************************************************
  269. changed: [flipkart]
  270.  
  271. TASK [Generate an csr with the common_name as amazon.chrisich.fun] *****************************************************
  272. changed: [flipkart]
  273.  
  274. TASK [First challenge for amazon.chrisich.fun using the csr and account key] *******************************************
  275. changed: [flipkart]
  276.  
  277. TASK [Create .well-known/acme-challenge directory] *********************************************************************
  278. changed: [flipkart]
  279.  
  280. TASK [Copy http-01 challenges to well-known/acme-challenge directory] **************************************************
  281. changed: [flipkart] => (item={'key': 'amazon.chrisich.fun', 'value': {'http-01': {'resource': '.well-known/acme-challenge/QGJhTKgo1l24mU90Yp9AxS8YXkcrsef5hlcs_3S38PQ', 'resource_value': 'QGJhTKgo1l24mU90Yp9AxS8YXkcrsef5hlcs_3S38PQ.L7w-ZFUMVg7AGgX_5zNb9YlVJ1hpYMhnCrvqH4Zcq08'}, 'dns-01': {'resource': '_acme-challenge', 'resource_value': 'zITvzTMImB8TcYaY8pleEO-Jr1BRwjbmxHV7kqL3AO8', 'record': '_acme-challenge.amazon.chrisich.fun'}, 'tls-alpn-01': {'resource': 'amazon.chrisich.fun', 'resource_original': 'dns:amazon.chrisich.fun', 'resource_value': 'zITvzTMImB8TcYaY8pleEO+Jr1BRwjbmxHV7kqL3AO8='}}})
  282.  
  283. TASK [Final letsencrypt verification and saving the full chain file] ***************************************************
  284. changed: [flipkart]
  285.  
  286. TASK [Activate SSL in nginx conf] **************************************************************************************
  287. changed: [flipkart]
  288.  
  289. RUNNING HANDLER [nginx : restart-nginx] ********************************************************************************
  290. changed: [flipkart]
  291.  
  292. RUNNING HANDLER [nginx : restart-php] **********************************************************************************
  293. changed: [flipkart]
  294.  
  295. PLAY RECAP *************************************************************************************************************
  296. flipkart : ok=18 changed=16 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  297.  
Advertisement
Add Comment
Please, Sign In to add comment