Skip to content

Commit 98e899d

Browse files
authored
feat: Template support for the NGINX Stream map module (#561)
1 parent bc3dff1 commit 98e899d

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FEATURES:
66

77
- Add validation tasks to check the Ansible version, the Jinja2 version, and whether the required Ansible collections for this role are installed.
88
- Bump the Ansible `community.general` collection to `9.2.0`, `community.crypto` collection to `2.21.1` and `community.docker` collection to `3.11.0`.
9+
- Add templating support for the `ngx_stream_map_module` NGINX module.
910

1011
BUG FIXES:
1112

defaults/main/template.yml

+11
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,17 @@ nginx_config_stream_template:
961961
inactive: 20s
962962
min_uses: 2 # Number
963963
valid: 1m
964+
map: # Configure maps -- Available only in the 'stream' context
965+
hash_bucket_size: 64
966+
hash_max_size: 2048
967+
mappings: # List of dictionaries
968+
- string: $remote_addr # Required
969+
variable: $upstream # Required
970+
hostnames: false # Boolean
971+
volatile: false # Boolean
972+
content: # Dictionary or list of dictionaries
973+
- value: default
974+
new_value: 0
964975
custom_directives: # String or a list of strings. Custom directive for specific use cases not covered by templates. Note: You need to include each directive in its full form. Make sure you add a semi-colon or closing curly bracket at the end of each directive.
965976
- server {};
966977
servers: # All previous modules are also available (when allowed) in the 'servers' context.

molecule/complete/converge.yml

+18
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,24 @@
854854
inactive: 20s
855855
min_uses: 2
856856
valid: 1m
857+
map:
858+
hash_bucket_size: 128
859+
hash_max_size: 4096
860+
mappings:
861+
- string: $hostname
862+
variable: $stream_name
863+
hostnames: true
864+
volatile: true
865+
content:
866+
- value: example.com
867+
new_value: 3
868+
- string: $host
869+
variable: $isblockaccess_ua
870+
content:
871+
- value: default
872+
new_value: 0
873+
- value: '"~jndi:ldap"'
874+
new_value: 2
857875
servers:
858876
- core:
859877
listen:

molecule/complete_plus/converge.yml

+18
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,24 @@
769769
inactive: 20s
770770
min_uses: 2
771771
valid: 1m
772+
map:
773+
hash_bucket_size: 128
774+
hash_max_size: 4096
775+
mappings:
776+
- string: $hostname
777+
variable: $stream_name
778+
hostnames: true
779+
volatile: true
780+
content:
781+
- value: example.com
782+
new_value: 3
783+
- string: $host
784+
variable: $isblockaccess_ua
785+
content:
786+
- value: default
787+
new_value: 0
788+
- value: '"~jndi:ldap"'
789+
new_value: 2
772790
servers:
773791
- core:
774792
listen:

templates/stream/modules.j2

+32
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,35 @@ open_log_file_cache {{ 'off' if not log['open_log_file_cache'] else ('max=' + lo
7979
{% endif %}
8080

8181
{% endmacro %}
82+
83+
{# NGINX Stream Map -- ngx_stream_map_module #}
84+
{% macro map(map) %}{# 'map' module is only available in the 'stream' context #}
85+
{% if map['hash_bucket_size'] is defined %}
86+
map_hash_bucket_size {{ map['hash_bucket_size'] }};
87+
{% endif %}
88+
{% if map['hash_max_size'] is defined %}
89+
map_hash_max_size {{ map['hash_max_size'] }};
90+
{% endif %}
91+
{% if map['mappings'] is defined %}
92+
{% for map_data in map['mappings'] %}
93+
{% if map_data['string'] is defined and map_data['variable'] is defined %}
94+
map {{ map_data['string'] }} {{ map_data['variable'] }} {
95+
{% if map_data['hostnames'] is defined and map_data['hostnames'] is boolean and map_data['hostnames'] | bool %}
96+
hostnames;
97+
{% endif %}
98+
{% if map_data['volatile'] is defined and map_data['volatile'] is boolean and map_data['volatile'] | bool %}
99+
volatile;
100+
{% endif %}
101+
{% if map_data['content'] is defined %}
102+
{% for content_line in map_data['content'] if map_data['content'] is not mapping %}
103+
{{ content_line['value'] }} {{ content_line['new_value'] }};
104+
{% else %}
105+
{{ map_data['content']['value'] }} {{ map_data['content']['new_value'] }};
106+
{% endfor %}
107+
{% endif %}
108+
}
109+
{% endif %}
110+
{% endfor %}
111+
{% endif %}
112+
113+
{% endmacro %}

0 commit comments

Comments
 (0)