Skip to content

Commit 0b3300c

Browse files
author
Ubuntu
committed
initial implementation of install and config roles
1 parent c2466c9 commit 0b3300c

File tree

10 files changed

+327
-57
lines changed

10 files changed

+327
-57
lines changed

README.md

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,57 @@ The NGINX Ansible role supports all platforms supported by [NGINX Plus](https://
4141
**NGINX Plus**
4242

4343
```yaml
44-
Alpine:
45-
versions:
46-
- 3.8
47-
- 3.9
48-
- 3.10
49-
Amazon Linux:
50-
versions:
51-
- 2018.03
52-
Amazon Linux 2:
53-
versions:
54-
- LTS
5544
CentOS:
5645
versions:
57-
- 6.5+
58-
- 7.4+
59-
- 8
60-
Debian:
61-
versions:
62-
- stretch
63-
- buster
64-
FreeBSD:
65-
versions:
66-
- 11.2+
67-
- 12
68-
Oracle Linux:
69-
versions:
70-
- 6.5+
71-
- 7.4+
72-
RedHat:
73-
versions:
74-
- 6.5+
75-
- 7.4+
76-
- 8
77-
SUSE/SLES:
78-
versions:
79-
- 12
80-
- 15
81-
Ubuntu:
82-
versions:
83-
- xenial
84-
- bionic
46+
- 7.4
8547
```
8648
8749
Role Variables
8850
--------------
8951
90-
This role has multiple variables. The descriptions and defaults for all these variables can be found in the directory **`defaults/main`** in the following files:
52+
This role has multiple variables. The descriptions and defaults for all these variables can be found in the **[defaults/main.yml](./defaults/main.yml)`**.
9153

92-
- **[defaults/main/main.yml](./defaults/main/main.yml):** NGINX installation variables
93-
- **[defaults/main/upload.yml](./defaults/main/upload.yml):** NGINX configuration/HTML/SSL upload variables
94-
- **[defaults/main/linux.yml](./defaults/main/linux.yml):** Linux installation variables
95-
- **[defaults/main/bsd.yml](./defaults/main/bsd.yml):** BSD installation variables
9654

9755
Dependencies
9856
------------
9957

100-
None
58+
Since this role uses the [package_facts](https://docs.ansible.com/ansible/latest/modules/package_facts_module.html) module, on debian-based systems the `python-apt` package must be installed on targeted hosts.
10159

10260
Example Playbook
10361
----------------
10462

63+
64+
This is a sample playbook file for using the role to install NGINX App Protect on NGINX Plus and configure it using basic settings to all `wafs` inventory hosts.
65+
66+
```yaml
67+
---
68+
- hosts: wafs
69+
become: true
70+
vars:
71+
# Installs NGINX App Protect and all dependencies to the target host
72+
app_protect_install: true
73+
74+
# Creates basic configuration files and enables NGINX App Protect on the target host
75+
app_protect_configure: true
76+
77+
# For use with the app_protect_configure option to determine if the default security policy will be written to the target host
78+
app_protect_security_policy_template_enable: true
79+
80+
# For use with the app_protect_configure option to determine if the default log policy will be written to the target host
81+
app_protect_log_policy_template_enable: true
82+
83+
# For use with the app_protect_configure option to determine if the sample nginx.conf will be written to the target host.
84+
# Since this can be dangerous, this value is default to false in the role defaults
85+
nginx_conf_template_enable: true
86+
87+
# For use with the app_protect_configure option to determine the syslog target to be injected
88+
# into the default log policy that will be written to the target host
89+
log_policy_syslog_target: 10.1.1.8:5144
90+
91+
roles:
92+
- role: ansible-role-nginx-app-protect
93+
```
94+
10595
This is a sample playbook file for deploying the Ansible Galaxy NGINX App Protect role in a localhost and installing NGINX App Protect on NGINX Plus.
10696

10797
```yaml

defaults/main.yml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,43 @@
11
---
2-
# defaults file for ansible-role-nginx-app-protect
2+
# defaults file for ansible-role-nginx-app-protect
3+
4+
app_protect_install: true
5+
app_protect_configure: true
6+
7+
tmp_dir: /tmp/app-protect
8+
cleanup_when_done: true
9+
10+
# Start NGINX service.
11+
# Default is true.
12+
nginx_start: true
13+
14+
# populate this dictionary of lists with appropriate values from the ansible_os_family and ansible_distribution_version facts
15+
app_protect_linux_families:
16+
RedHat:
17+
- 7.4
18+
19+
app_protect_security_policy_template_enable: true
20+
app_protect_security_policy_template:
21+
template_file: app-protect-security-policy.j2
22+
out_file_name: app-protect-security-policy.json
23+
out_file_location: /etc/nginx/
24+
25+
app_protect_log_policy_template_enable: true
26+
app_protect_log_policy_template:
27+
template_file: app-protect-log-policy.j2
28+
out_file_name: app-protect-log-policy.json
29+
out_file_location: /etc/nginx/
30+
31+
nginx_conf_template_enable: false
32+
nginx_conf_template:
33+
template_file: nginx.conf.j2
34+
out_file_name: nginx.conf
35+
out_file_location: /etc/nginx/
36+
37+
# possible values: transparent, blocking
38+
security_policy_enforcement_mode: transparent
39+
40+
# possible values: TBD
41+
log_policy_filter_request_type: all
42+
43+
log_policy_syslog_target: 127.0.0.1:514

handlers/main.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
---
2-
# handlers file for ansible-role-nginx-app-protect
2+
# handlers file for ansible-role-nginx-app-protect
3+
- name: "(Handler: All OSs) Run NGINX"
4+
block:
5+
6+
- name: "(Handler: All OSs) Start NGINX"
7+
service:
8+
name: nginx
9+
state: started
10+
enabled: yes
11+
12+
- name: "(Handler: All OSs) Reload NGINX"
13+
service:
14+
name: nginx
15+
state: reloaded
16+
17+
when:
18+
- nginx_start | bool
19+
- not ansible_check_mode

meta/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ galaxy_info:
1111
platforms:
1212
- name: EL
1313
versions:
14-
- 7
14+
- 7.4
1515

1616
galaxy_tags:
1717
- waf
18+
- security
1819
- nginx
1920
- oss
2021
- plus

tasks/configure-app-protect.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
- name: "Ensure NGINX Main Directory Exists"
3+
file:
4+
path: "{{ nginx_conf_template.out_file_location}}"
5+
state: directory
6+
when: app_protect_security_policy_template_enable or app_protect_log_policy_template_enable or nginx_conf_template_enable
7+
8+
- name: Backup existing nginx.conf
9+
copy:
10+
src: "{{ nginx_conf_template.out_file_location }}{{ nginx_conf_template.out_file_name }}"
11+
dest: "{{ nginx_conf_template.out_file_location }}{{ nginx_conf_template.out_file_name }}.orig"
12+
remote_src: yes
13+
when: nginx_conf_template_enable
14+
15+
- name: "Dynamically Generate NGINX App Protect security policy file"
16+
template:
17+
src: "{{ app_protect_security_policy_template.template_file }}"
18+
dest: "{{ app_protect_security_policy_template.out_file_location }}{{ app_protect_security_policy_template.out_file_name }}"
19+
backup: yes
20+
when: app_protect_security_policy_template_enable
21+
22+
- name: "Dynamically Generate NGINX App Protect log policy file"
23+
template:
24+
src: "{{ app_protect_log_policy_template.template_file }}"
25+
dest: "{{ app_protect_log_policy_template.out_file_location }}{{ app_protect_log_policy_template.out_file_name }}"
26+
backup: yes
27+
when: app_protect_log_policy_template_enable
28+
29+
- name: "Dynamically Generate NGINX conf file"
30+
template:
31+
src: "{{ nginx_conf_template.template_file }}"
32+
dest: "{{ nginx_conf_template.out_file_location }}{{ nginx_conf_template.out_file_name }}"
33+
backup: yes
34+
when: nginx_conf_template_enable
35+
36+
- name: "Reload NGINX"
37+
debug:
38+
msg: "trigger nginx reloaded if needed"
39+
notify: "(Handler: All OSs) Reload NGINX"
40+
changed_when: app_protect_security_policy_template_enable or app_protect_log_policy_template_enable or nginx_conf_template_enable

tasks/install-app-protect-linux.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
- name: Check that the NGINX App Protect install zip exists locally
3+
stat:
4+
path: "{{ install_zip}}"
5+
register: local_install_zip_stat
6+
connection: local
7+
8+
- name: Check preconditions
9+
assert:
10+
that:
11+
- "local_install_zip_stat.stat.exists == true"
12+
quiet: true
13+
14+
- name: Copy NGINX App Protect install zip to host
15+
copy:
16+
src: "{{ install_zip }}"
17+
dest: "{{ install_zip }}"
18+
19+
- name: Get package facts
20+
package_facts:
21+
manager: "auto"
22+
23+
- name: Set zip version number
24+
set_fact:
25+
key_value: "" # appeasing the linter
26+
app_protect_version: "{{ install_zip | regex_search('(\\d+)') }}"
27+
28+
- name: Set NGINX Plus version
29+
set_fact:
30+
key_value: "" # appeasing the linter
31+
nginx_plus_version: "{{ ansible_facts.packages['nginx-plus'] | map(attribute='version') | list | first | regex_search('^(\\d{1,3})') }}"
32+
when: "'nginx-plus' in ansible_facts.packages"
33+
34+
- name: Fail if NGINX+ version preconditions fail
35+
assert:
36+
that:
37+
- nginx_plus_version is defined
38+
- nginx_plus_version | int >= 18
39+
fail_msg: "'nginx_plus_version' release version must be a minimum of 18 for App Protect. Actual: {{ nginx_plus_version }}"
40+
success_msg: "'nginx_plus_version' is {{ nginx_plus_version }}"
41+
quiet: true
42+
43+
- name: Fail if app protect zip doesn't not match detected NGINX+ version
44+
assert:
45+
that:
46+
- app_protect_version is defined
47+
- nginx_plus_version | int == app_protect_version | int
48+
fail_msg: "'nginx_plus_version' {{ nginx_plus_version }} must match the NGINX App Protect version {{ app_protect_version }}"
49+
success_msg: "'nginx_plus_version' is {{ nginx_plus_version }} and 'app_protect_version' is {{ app_protect_version }}"
50+
quiet: true
51+
52+
- name: Install epel-release, unzip and openssl packages
53+
package:
54+
name: epel-release, unzip, openssl
55+
state: present
56+
57+
- name: Create a directory if it does not exist
58+
file:
59+
path: "{{ tmp_dir }}"
60+
state: directory
61+
62+
- name: Unarchive the App Protect package file that is already on the remote machine
63+
unarchive:
64+
src: "{{ install_zip }}"
65+
dest: "{{ tmp_dir }}"
66+
remote_src: yes
67+
68+
- name: Display paths of all .rpm files in dir; exclude NGINX+ installer
69+
find:
70+
paths:
71+
- "{{ tmp_dir }}"
72+
file_type: file
73+
use_regex: true
74+
patterns:
75+
- "^(?!.*nginx-plus-{{ app_protect_version }}).*\\.rpm$"
76+
register: rpm_files
77+
78+
- name: Install f5 packages
79+
package:
80+
name: "{{ rpm_files | select('match', '^f5.*.rpm$') | list }}"
81+
state: present
82+
83+
- name: Install app-protect dependency packages
84+
package:
85+
name: "{{ rpm_files | select('match', '^app-protect-[^\\d].*\\.rpm$') | list }}"
86+
state: present
87+
88+
- name: Install app-protect NGINX+ module package
89+
package:
90+
name: "{{ rpm_files | select('match', '^nginx-plus-module-appprotect-.*\\.rpm$') | list }}"
91+
state: present
92+
93+
- name: Install app-protect core package
94+
package:
95+
name: "{{ rpm_files | select('match', '^app-protect-\\d{2,3}.*\\.rpm$') | list }}"
96+
state: present
97+
98+
- name: Disable SELinux
99+
selinux:
100+
state: disabled
101+
102+
- name: Recursively remove extracted directory
103+
file:
104+
path: "{{ tmp_dir }}"
105+
state: absent
106+
when: cleanup_when_done
107+
108+
- name: Remove source zip
109+
file:
110+
path: "{{ install_zip }}"
111+
state: absent
112+
when: cleanup_when_done

tasks/main.yml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
---
2-
# tasks file for ansible-role-nginx-app-protect
2+
- name: set supported_os when platform and version are in supported platforms dictionary
3+
set_fact:
4+
key_value: "" # appeasing the linter
5+
supported_os: true
6+
loop: "{{ query('dict', app_protect_linux_families) }}"
7+
when: ansible_os_family in item.key and ansible_distribution_version | float in item.value
38

4-
- name: Get package facts
5-
package_facts:
6-
manager: "auto"
7-
- name: Set NGINX Plus version
9+
- name: set supported_os to false if fact not defined
810
set_fact:
9-
nginx_plus_version: "{{ ansible_facts.packages['nginx-plus']|
10-
map(attribute='version')|
11-
list }}"
12-
when: "'nginx-plus' in ansible_facts.packages"
11+
key_value: "" # appeasing the linter
12+
supported_os: false
13+
when: supported_os is not defined
14+
15+
- name: debug supported os
16+
debug:
17+
msg: "supported_os {{ supported_os }}"
18+
verbosity: 2
19+
20+
- name: Abort if the OS/version combination is not supported
21+
fail:
22+
msg: "NGINX App Protect is not supported on os family {{ ansible_os_family }} version {{ ansible_distribution_version }}"
23+
when: not supported_os
24+
25+
- name: "(Install: Linux) Install NGINX Plus App Protect"
26+
include_tasks: install-app-protect-linux.yml
27+
when: supported_os and app_protect_install
28+
29+
- name: "Configure NGINX Plus App Protect"
30+
include_tasks: configure-app-protect.yml
31+
when: supported_os and app_protect_configure

templates/app-protect-log-policy.j2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"filter": {
3+
"request_type": "{{ log_policy_filter_request_type }}"
4+
},
5+
"content": {
6+
"format": "default",
7+
"max_request_size": "any",
8+
"max_message_size": "5k"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "/Common/policy1",
3+
"template": { "name": "POLICY_TEMPLATE_NGINX_BASE" },
4+
"applicationLanguage": "utf-8",
5+
"enforcementMode": "{{ security_policy_enforcement_mode }}"
6+
}

0 commit comments

Comments
 (0)