Skip to content

Commit c040e86

Browse files
committed
Refactored Nginx role (DRY).
1 parent 6804524 commit c040e86

File tree

27 files changed

+168
-495
lines changed

27 files changed

+168
-495
lines changed

Ansible/ansible_collections/jfrog/platform/roles/artifactory/tasks/install.yml

+2-11
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@
1515
- name: Install nginx
1616
ansible.builtin.include_role:
1717
name: artifactory_nginx
18-
when:
19-
- artifactory_nginx_enabled | bool
20-
- not artifactory_nginx_ssl_enabled | bool
21-
22-
- name: Install nginx with SSL
23-
ansible.builtin.include_role:
24-
name: artifactory_nginx_ssl
25-
when:
26-
- not artifactory_nginx_enabled | bool
27-
- artifactory_nginx_ssl_enabled | bool
18+
when: ( artifactory_nginx_enabled | bool ) or ( artifactory_nginx_ssl_enabled | bool )
2819

2920
- name: Ensure group artifactory exist
3021
become: true
@@ -239,4 +230,4 @@
239230
delay: 5
240231
when:
241232
- not ansible_check_mode
242-
- artifactory_start_service | bool
233+
- artifactory_start_service | bool
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
# artifactory_nginx
2-
3-
This role installs NGINX for artifactory. This role is automatically called by the artifactory role and isn't intended to be used separately.
1+
# artifactory_nginx_ssl
2+
The artifactory_nginx_ssl role installs and configures nginx for SSL.
43

54
## Role Variables
6-
7-
* _server_name_: **mandatory** This is the server name. eg. "artifactory.54.175.51.178.xip.io"
8-
* _artifactory_docker_registry_subdomain_: Whether to add a redirect directive to the nginx config for the use of docker subdomains.
5+
* _server_name_: This is the server name. eg. "artifactory.54.175.51.178.xip.io"
6+
* _ssl_certificate_install_: `true` - install the SSL certificate and private key. When `false` you need to manage certs yourself.
7+
* _ssl_certificate_: This is the filename of the SSL certificate.
8+
* _ssl_certificate_path_: This is the full directory path for the SSL certificate, excluding _ssl_certificate_.
9+
* _ssl_certificate_key_: This is the filename of the SSL private key.
10+
* _ssl_certificate_key_path_: This is the full directory path for the SSL private key, excluding _ssl_certificate_key_.
11+
* _nginx_worker_processes_: The worker_processes configuration for nginx. Defaults to 1.
12+
* _artifactory_docker_registry_subdomain_: Whether to add a redirect directive to the nginx config for the use of docker
13+
subdomains.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
---
2-
# defaults file for artifactory_nginx
1+
# defaults file for artifactory_nginx_ssl
2+
33
## For production deployments,You SHOULD change it.
4-
server_name: test.artifactory.com
4+
# server_name: test.artifactory.com
55

66
nginx_daemon: nginx
7+
nginx_upstream: false
8+
nginx_upstream_repo_key: https://nginx.org/keys/nginx_signing.key
9+
nginx_upstream_repo_baseurl: https://nginx.org/packages
10+
nginx_module: '1.22'
11+
redirect_http_to_https_enabled: true
712

813
nginx_worker_processes: 1
914
artifactory_docker_registry_subdomain: false
1015

1116
artifactory_conf_template: artifactory.conf.j2
1217
nginx_conf_template: nginx.conf.j2
18+
19+
ssl_certificate_install: true
20+
ssl_certificate_path: /etc/pki/tls/certs
21+
ssl_certificate_key_path: /etc/pki/tls/private
22+
ssl_certificate: cert.pem
23+
ssl_certificate_key: cert.key

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/handlers/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
# handlers file for artifactory_nginx
2+
# handlers file for artifactory_nginx_ssl
33
- name: Restart nginx
44
become: true
55
ansible.builtin.systemd:

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/meta/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dependencies: []
33

44
galaxy_info:
55
author: "JFrog Maintainers Team <[email protected]>"
6-
description: "This role installs NGINX for artifactory. This role is automatically called by the artifactory role and isn't intended to be used separately."
6+
description: "The artifactory_nginx_ssl role installs and optionally configures nginx for SSL."
77
company: JFrog
88
issue_tracker_url: "https://github.com/jfrog/JFrog-Cloud-Installers/issues"
99
license: license (Apache-2.0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
- name: ensure apt-transport-https is installed
2+
anible.builtin.napt:
3+
name: apt-transport-https
4+
state: present
5+
6+
- name: Add upstream nginx apt key
7+
become: true
8+
ansible.builtin.apt_key:
9+
url: "{{ nginx_upstream_repo_key }}"
10+
state: present
11+
12+
- name: Add nginx stable repo
13+
become: true
14+
ansible.builtin.apt_repository:
15+
repo: 'deb {{ nginx_upstream_repo_baseurl }}/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} nginx'
16+
state: present
17+
18+
- name: Update apt cache
19+
become: true
20+
ansible.builtin.apt:
21+
update_cache: true
22+
cache_valid_time: 3600
23+
register: apt_update_cache
24+
retries: 5
25+
delay: 60

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/tasks/Debian.yml renamed to Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/tasks/Debian-upstream.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
owner: root
1212
group: root
1313
mode: '0644'
14-
content: deb https://nginx.org/packages/{{ distro_family }} {{ distro_codename }} nginx
14+
content: deb https://nginx.org/packages/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} nginx
1515
vars:
1616
distro_family: "{{ ansible_distribution | lower }}"
1717
distro_codename: "{{ ansible_distribution_release }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- name: Enable nginx from AppStream
2+
become: true
3+
ansible.builtin.command: "yum module enable -y nginx:{{ nginx_module }}"
4+
5+
- name: Update yum cache
6+
become: true
7+
ansible.builtin.yum:
8+
state: present
9+
update_cache: true
10+
11+
- name: Gather selinux facts
12+
ansible.builtin.setup:
13+
gather_subset: selinux
14+
15+
- name: Set httpd_can_network_connect
16+
become: true
17+
ansible.posix.seboolean:
18+
name: httpd_can_network_connect
19+
state: true
20+
persistent: true
21+
when: ansible_facts.selinux.status == 'enabled'

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/tasks/RedHat.yml

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
- name: Install prerequisite packages
2-
ansible.builtin.include_tasks: "{{ ansible_os_family }}.yml"
1+
- name: Check required variables
2+
ansible.builtin.fail: msg="Variable '{{ item }}' is not defined"
3+
when: item not in vars
4+
loop:
5+
- certificate
6+
- certificate_key
7+
- server_name
8+
9+
- name: Install upstream packages
10+
when: nginx_upstream | bool
11+
ansible.builtin.include_tasks: "{{ ansible_os_family }}-upstream.yml"
12+
13+
- name: Install packages from distribution server
14+
when: not nginx_upstream | bool
15+
ansible.builtin.include_tasks: "{{ ansible_os_family }}-ownstream.yml"
316

417
- name: Install nginx
518
become: true
@@ -9,9 +22,9 @@
922
register: install_nginx
1023
retries: 5
1124
delay: 60
12-
until: install_nginx is succeeded
25+
until: install_nginx is success
1326

14-
- name: Copy nginx.conf file
27+
- name: Configure main nginx conf file.
1528
become: true
1629
ansible.builtin.template:
1730
src: "{{ nginx_conf_template }}"
@@ -20,7 +33,21 @@
2033
group: root
2134
mode: '0755'
2235

23-
- name: Generate artifactory.conf
36+
- name: Configure redirect nginx conf
37+
when:
38+
- artifactory_nginx_ssl_enabled is defined
39+
- artifactory_nginx_ssl_enabled | bool
40+
become: true
41+
ansible.builtin.copy:
42+
src: redirect_http_to_https.conf
43+
dest: /etc/nginx/conf.d/redirect_http_to_https.conf
44+
owner: root
45+
group: root
46+
mode: '0755'
47+
when: redirect_http_to_https_enabled | bool
48+
notify: Restart nginx
49+
50+
- name: Configure the artifactory nginx conf
2451
become: true
2552
ansible.builtin.template:
2653
src: "{{ artifactory_conf_template }}"
@@ -30,5 +57,50 @@
3057
mode: '0755'
3158
notify: Restart nginx
3259

60+
- name: Configure SSL
61+
when:
62+
- artifactory_nginx_ssl_enabled is defined
63+
- artifactory_nginx_ssl_enabled | bool
64+
- ssl_certificate_install | bool
65+
block:
66+
- name: Create directory
67+
become: true
68+
ansible.builtin.file:
69+
path: "/var/opt/jfrog/nginx/ssl"
70+
state: directory
71+
mode: '0755'
72+
73+
- name: Ensure ssl_certificate_path exists
74+
become: true
75+
ansible.builtin.file:
76+
path: "{{ ssl_certificate_path }}"
77+
state: directory
78+
mode: '0755'
79+
80+
- name: Ensure ssl_certificate_key_path exists
81+
become: true
82+
ansible.builtin.file:
83+
path: "{{ ssl_certificate_key_path }}"
84+
state: directory
85+
mode: '0700'
86+
87+
- name: Configure certificate
88+
become: true
89+
ansible.builtin.template:
90+
src: certificate.pem.j2
91+
dest: "{{ ssl_certificate_path }}/{{ ssl_certificate }}"
92+
mode: '0644'
93+
notify: Restart nginx
94+
no_log: true
95+
96+
- name: Configure key
97+
become: true
98+
ansible.builtin.template:
99+
src: certificate.key.j2
100+
dest: "{{ ssl_certificate_key_path }}/{{ ssl_certificate_key }}"
101+
mode: '0600'
102+
notify: Restart nginx
103+
no_log: true
104+
33105
- name: Restart nginx
34106
ansible.builtin.meta: flush_handlers

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/templates/artifactory.conf.j2

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###########################################################
2-
## this configuration was generated by JFrog Artifactory ##
2+
## this configuration was generated for JFrog Artifactory ##
33
###########################################################
44

55
## add HA entries when ha is configure
@@ -9,9 +9,21 @@
99
upstream artifactory-direct {
1010
server 127.0.0.1:8081;
1111
}
12+
{% if artifactory_nginx_ssl_enabled is defined and artifactory_nginx_ssl_enabled %}
13+
ssl_protocols TLSv1.2 TLSv1.3;
14+
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
15+
ssl_certificate {{ ssl_certificate_path }}/{{ ssl_certificate }};
16+
ssl_certificate_key {{ ssl_certificate_key_path }}/{{ ssl_certificate_key }};
17+
ssl_session_cache shared:SSL:1m;
18+
ssl_prefer_server_ciphers on;
19+
{% endif %}
1220
## server configuration
1321
server {
14-
listen 80 ;
22+
{% if artifactory_nginx_ssl_enabled is defined and artifactory_nginx_ssl_enabled %}
23+
listen 443 ssl http2;
24+
{% else %}
25+
listen 80;
26+
{% endif %}
1527
server_name {{ server_name }};
1628
if ($http_x_forwarded_proto = '') {
1729
set $http_x_forwarded_proto $scheme;
@@ -41,4 +53,4 @@
4153
proxy_pass http://artifactory-direct;
4254
}
4355
}
44-
}
56+
}

Ansible/ansible_collections/jfrog/platform/roles/artifactory_nginx/vars/main.yml

-1
This file was deleted.

0 commit comments

Comments
 (0)