Skip to content

Commit e29c69a

Browse files
committed
Second commit
1 parent 19b8e7e commit e29c69a

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

ansible.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[defaults]
2+
host_key_checking = false
3+
roles_path = /home/ec2-user/nginx-project/roles

inventory

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[amazon]
2+
3+
devops ansible_ssh_host=172.31.38.196 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem" ansible_python_interpreter="auto_silent"
4+
5+
[test]
6+
flipkart ansible_ssh_host=172.31.44.149 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem" ansible_python_interpreter="auto_silent"

main.yml

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
3+
- name: "Uploading the git PHP website using nginx role"
4+
hosts: test
5+
become: true
6+
roles:
7+
- nginx
8+
vars:
9+
git_url: "https://github.com/Chris-luiz-16/aws-elb-site.git"
10+
ssl_dir: /etc/nginx/ssl
11+
acme_directory: "https://acme-v02.api.letsencrypt.org/directory"
12+
tasks:
13+
- name: "Uploading the git contents to the remote server"
14+
git:
15+
repo: "{{ git_url }}"
16+
dest: /tmp/website
17+
register: git_status
18+
- name: "Copying the contents to the default document root /var/www/html/{{ domain_name }}"
19+
when: git_status.changed == true
20+
copy:
21+
src: /tmp/website/
22+
dest: "/var/www/html/{{ domain_name }}/"
23+
owner: "{{ nginx_owner }}"
24+
group: "{{ nginx_owner }}"
25+
remote_src: true
26+
notify:
27+
- restart-php
28+
- name: "Nginx restart required for Letsencrypt validation"
29+
service:
30+
name: nginx
31+
state: restarted
32+
- name: "Create a letsencrypt directory for nginx"
33+
file:
34+
path: "{{ ssl_dir }}/{{ item }}"
35+
state: directory
36+
with_items:
37+
- account
38+
- certs
39+
- csrs
40+
- keys
41+
- name: "Generate a account key for acme_module"
42+
community.crypto.openssl_privatekey:
43+
path: "{{ ssl_dir }}/account/account.key"
44+
type: RSA
45+
size: 4096
46+
- name: "Generate a private key for {{ domain_name }}"
47+
community.crypto.openssl_privatekey:
48+
path: "{{ ssl_dir }}/keys/{{ domain_name }}.key"
49+
type: RSA
50+
size: 4096
51+
- name: "Generate an csr with the common_name as {{ domain_name }}"
52+
community.crypto.openssl_csr:
53+
path: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
54+
privatekey_path: "{{ ssl_dir }}/keys/{{ domain_name }}.key"
55+
common_name: "{{ domain_name }}"
56+
- name: "First challenge for {{ domain_name }} using the csr and account key"
57+
community.crypto.acme_certificate:
58+
acme_directory: "{{ acme_directory }}"
59+
acme_version: "2"
60+
account_key_src: "{{ ssl_dir }}/account/account.key"
61+
account_email: "admin@{{ domain_name }}"
62+
terms_agreed: true
63+
challenge: http-01
64+
csr: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
65+
dest: "{{ ssl_dir }}/certs/{{ domain_name }}.crt"
66+
fullchain_dest: "{{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt"
67+
register: sample_com_challenge
68+
- name: "Create .well-known/acme-challenge directory"
69+
file:
70+
path: "/var/www/html/{{ domain_name }}/.well-known/acme-challenge"
71+
state: directory
72+
owner: "{{ nginx_owner }}"
73+
group: "{{ nginx_owner }}"
74+
- name: "Copy http-01 challenges to well-known/acme-challenge directory"
75+
copy:
76+
dest: "/var/www/html/{{ item.key }}/{{ item.value['http-01']['resource'] }}"
77+
content: "{{ item.value['http-01']['resource_value'] }}"
78+
owner: "{{ nginx_owner }}"
79+
group: "{{ nginx_owner }}"
80+
loop: "{{ sample_com_challenge.challenge_data | dict2items }}"
81+
when: sample_com_challenge is changed
82+
- name: "Final letsencrypt verification and saving the full chain file "
83+
community.crypto.acme_certificate:
84+
acme_directory: "{{ acme_directory }}"
85+
acme_version: "2"
86+
account_key_src: "{{ ssl_dir }}/account/account.key"
87+
account_email: "admin@{{ domain_name }}"
88+
terms_agreed: true
89+
challenge: http-01
90+
csr: "{{ ssl_dir }}/csrs/{{ domain_name }}.csr"
91+
dest: "{{ ssl_dir }}/certs/{{ domain_name }}.crt"
92+
fullchain_dest: "{{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt"
93+
data: "{{ sample_com_challenge }}"
94+
notify:
95+
- restart-nginx
96+
- name: "Activate SSL in nginx conf"
97+
blockinfile:
98+
path: "/etc/nginx/conf.d/{{ domain_name }}.conf"
99+
marker: "##<!-- {mark} ANSIBLE MANAGED BLOCK -->"
100+
insertafter: "root /var/www/html/{{ domain_name }};"
101+
block: |
102+
listen 443 ssl;
103+
ssl_certificate {{ ssl_dir }}/certs/fullchain_{{ domain_name }}.crt;
104+
ssl_certificate_key {{ ssl_dir }}/keys/{{ domain_name }}.key;
105+
if ($scheme = http) {
106+
return 301 https://$server_name$request_uri;
107+
}
108+
notify:
109+
- restart-nginx
110+
111+
112+
113+

0 commit comments

Comments
 (0)