Skip to content

Commit c390971

Browse files
authored
Add Molecule tests covering common NGINX configurations (#420)
1 parent 8fadd27 commit c390971

38 files changed

+1659
-933
lines changed

.github/workflows/molecule.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ jobs:
4444
fail-fast: false
4545
matrix:
4646
scenario:
47-
- cleanup_module
47+
- api
48+
- cleanup_config
49+
- complete
50+
- complete_plus
4851
- default
49-
- plus
50-
- push
52+
- push_config
53+
- reverse_proxy
54+
- stub_status
55+
- web_server
5156
needs: ansible-lint
5257
steps:
5358
- name: Check out the codebase

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ BUG FIXES:
88

99
CI/CD:
1010

11+
- Add Molecule tests covering common NGINX use cases (web server, reverse proxy), enabling the NGINX stub status metrics, and NGINX Plus API and live metrics dashboard.
1112
- Update the platforms used in the various Molecule scenarios.
1213
- Use the local role name (`ansible-role-nginx-config`) instead of the fully qualified role name (`nginxinc.nginx_config`) in Molecule to ensure tests always work as intended in environments where the role has been already installed beforehand.
1314

README.md

+33-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This role configures NGINX Open Source and NGINX Plus on your target host.
4242

4343
- Molecule is used to test the various functionalities of the role. The recommended version of Molecule to test this role is `4.x`.
4444
- Instructions on how to install Molecule can be found in the [Molecule website](https://molecule.readthedocs.io/en/latest/installation.html). *You will also need to install the Molecule Docker driver.*
45-
- To run the NGINX Plus/App Protect config Molecule tests, you must copy your NGINX Plus/App Protect license to the role's [`files/license`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/files/license/) folder.
45+
- To run the NGINX Plus/App Protect config Molecule tests, you must copy your NGINX Plus/App Protect license to the role's [`files/license`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/files/license/) directory.
4646

4747
You can alternatively add your NGINX Plus/App Protect repository certificate and key to the local environment. Run the following commands to export these files as base64-encoded variables and execute the Molecule tests:
4848

@@ -54,6 +54,8 @@ This role configures NGINX Open Source and NGINX Plus on your target host.
5454

5555
## Installation
5656

57+
This role can be installed via either Ansible Galaxy (the Ansible community marketplace) or by cloning this repo. Once installed, you will need to include the role it in your Ansible playbook using [the `roles` keyword, the `import_role` module, or the `include_role` module](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html#using-roles).
58+
5759
### Ansible Galaxy
5860

5961
To install the latest stable release of the role on your system, use:
@@ -62,12 +64,20 @@ To install the latest stable release of the role on your system, use:
6264
ansible-galaxy install nginxinc.nginx_config
6365
```
6466

65-
Alternatively, if you have already installed the role, update the role to the latest release:
67+
Alternatively, if you have already installed the role, you can update the role to the latest release by using:
6668

6769
```bash
6870
ansible-galaxy install -f nginxinc.nginx_config
6971
```
7072

73+
To use the role, include the following task in your playbook:
74+
75+
```yaml
76+
- name: Configure NGINX
77+
ansible.builtin.include_role:
78+
name: nginxinc.nginx_config
79+
```
80+
7181
### Git
7282

7383
To pull the latest edge commit of the role from GitHub, use:
@@ -76,6 +86,14 @@ To pull the latest edge commit of the role from GitHub, use:
7686
git clone https://github.com/nginxinc/ansible-role-nginx-config.git
7787
```
7888

89+
To use the role, include the following task in your playbook:
90+
91+
```yaml
92+
- name: Configure NGINX
93+
ansible.builtin.include_role:
94+
name: <path/to/repo> # e.g. <roles/ansible-role-nginx-config> if you clone the repo inside your project's roles folder
95+
```
96+
7997
## Platforms
8098

8199
The NGINX config Ansible role supports all platforms supported by [NGINX Open Source](https://nginx.org/en/linux_packages.html#mainline) and [NGINX Plus](https://www.nginx.com/products/technical-specs/).
@@ -84,7 +102,7 @@ The NGINX config Ansible role supports all platforms supported by [NGINX Open So
84102

85103
## Role Variables
86104

87-
This role has multiple variables. The descriptions and defaults for all these variables can be found in the **[`defaults/main/`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/defaults/main/)** folder in the following files:
105+
This role has multiple variables. The descriptions and defaults for all these variables can be found in the **[`defaults/main/`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/defaults/main/)** directory in the following files:
88106

89107
| Name | Description |
90108
| ---- | ----------- |
@@ -95,16 +113,21 @@ This role has multiple variables. The descriptions and defaults for all these va
95113

96114
## Example Playbooks
97115

98-
Working functional playbook examples can be found in the **[`molecule/`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/)** folder in the following files:
116+
Working functional playbook examples can be found in the **[`molecule/`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/)** directory in the following files:
99117

100118
| Name | Description |
101119
| ---- | ----------- |
102-
| **[`cleanup_module/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/cleanup_module/converge.yml)** | Cleanup an NGINX config and configure NGINX supported modules |
103-
| **[`default/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml)** | Use the NGINX config templating variables to create an NGINX config |
104-
| **[`plus/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/plus/converge.yml)** | Use the NGINX config templating variables to create an NGINX Plus config |
105-
| **[`push/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/push/converge.yml)** | Push a preexisting config from your system to your NGINX instance |
106-
107-
Do note that if you install this repository via Ansible Galaxy, you will have to replace the role variable in the sample playbooks from `ansible-role-nginx-config` to `nginxinc.nginx_config`.
120+
| **[`api/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/api/converge.yml)** | Configure the NGINX Plus API and live metrics dashboard |
121+
| **[`cleanup_config/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/cleanup_config/converge.yml)** | Cleanup an NGINX config |
122+
| **[`complete/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/complete/converge.yml)** | Test all NGINX directives are correctly templated |
123+
| **[`complete_plus/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/complete_plus/converge.yml)** | Test all NGINX Plus specific directives are correctly templated |
124+
| **[`default/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml)** | Configure NGINX with a config as close as possible to the default config |
125+
| **[`push_config/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/push_config/converge.yml)** | Push a preexisting NGINX config from your system to your NGINX instance |
126+
| **[`reverse_proxy/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/reverse_proxy/converge.yml)** | Configure NGINX as a reverse proxy between two web servers |
127+
| **[`stub_status/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/stub_status/converge.yml)** | Configure the NGINX Open Source stub status metrics |
128+
| **[`web_server/converge.yml`](https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/web_server/converge.yml)** | Configure NGINX as a web server |
129+
130+
**Note:** If you install this repository via Ansible Galaxy, you will need to replace the `include_role` variable in the example playbooks from `ansible-role-nginx-config` to `nginxinc.nginx_config`.
108131

109132
## Other NGINX Ansible Collections and Roles
110133

defaults/main/template.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ nginx_config_main_template:
2121
# - modules/ngx_http_js_module.so
2222
user: # nginx # Can alternatively be set to a 'username' string
2323
username: nginx # Required
24-
group: nginx
24+
# group: nginx
2525
# worker_cpu_affinity:
2626
# auto: true # Optional boolean
2727
# cpumask: 0101 1010 # Optional if 'auto' is set to 'true'
@@ -37,7 +37,7 @@ nginx_config_main_template:
3737
# - file: /var/log/nginx/error.log # Required
3838
# level: notice
3939
pid: /var/run/nginx.pid
40-
daemon: true # Boolean
40+
# daemon: true # Boolean
4141
# debug_points: abort # Can be set to 'abort' or 'stop'
4242
# env: # MALLOC_OPTIONS # String, a list of strings, a dictionary, or a list of dictionaries. The 'variable' variable is only required when setting a 'value'.
4343
# variable: PERL5LIB # Required

molecule/api/converge.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
tasks:
5+
- name: Configure NGINX
6+
ansible.builtin.include_role:
7+
name: ansible-role-nginx-config
8+
vars:
9+
nginx_config_debug_output: true
10+
11+
nginx_config_rest_api_enable: true
12+
nginx_config_rest_api_template_file: http/api.conf.j2
13+
nginx_config_rest_api_file_location: /etc/nginx/conf.d/api.conf
14+
nginx_config_rest_api_backup: true
15+
nginx_config_rest_api_port: 8080
16+
nginx_config_rest_api_write: true
17+
nginx_config_rest_api_access_log:
18+
path: /var/log/nginx/access.log
19+
format: main
20+
nginx_config_rest_api_dashboard: true
File renamed without changes.

molecule/api/prepare.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Prepare
3+
hosts: localhost
4+
gather_facts: false
5+
tasks:
6+
- name: Create ephemeral license certificate file from b64 decoded env var
7+
ansible.builtin.copy:
8+
content: "{{ lookup('env', 'NGINX_CRT') | b64decode }}"
9+
dest: ../common/files/license/nginx-repo.crt
10+
force: false
11+
mode: "0444"
12+
13+
- name: Create ephemeral license key file from b64 decoded env var
14+
ansible.builtin.copy:
15+
content: "{{ lookup('env', 'NGINX_KEY') | b64decode }}"
16+
dest: ../common/files/license/nginx-repo.key
17+
force: false
18+
mode: "0444"
19+
20+
- name: Install NGINX Plus
21+
hosts: all
22+
tasks:
23+
- name: Install NGINX Plus
24+
ansible.builtin.include_role:
25+
name: nginxinc.nginx
26+
vars:
27+
nginx_type: plus
28+
nginx_license:
29+
certificate: ../common/files/license/nginx-repo.crt
30+
key: ../common/files/license/nginx-repo.key
31+
nginx_remove_license: false

molecule/api/verify.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: Verify
3+
hosts: all
4+
tasks:
5+
- name: Check if NGINX Plus is installed
6+
ansible.builtin.package:
7+
name: nginx-plus
8+
state: present
9+
check_mode: true
10+
register: install
11+
failed_when: (install is changed) or (install is failed)
12+
13+
- name: Check if NGINX service is running
14+
ansible.builtin.service:
15+
name: nginx
16+
state: started
17+
enabled: true
18+
check_mode: true
19+
register: service
20+
failed_when: (service is changed) or (service is failed)

molecule/cleanup_module/converge.yml renamed to molecule/cleanup_config/converge.yml

-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@
1515
recurse: false
1616
nginx_config_cleanup_files:
1717
- /etc/nginx/conf.d/default.conf
18-
19-
nginx_config_modules:
20-
- modules/ngx_http_js_module.so
File renamed without changes.

0 commit comments

Comments
 (0)