ADD: manage conf.d and include.d

This commit is contained in:
Marek Sirovy 2023-02-03 10:38:54 +00:00
parent bb6ee693ab
commit 7262b39e9c
5 changed files with 121 additions and 0 deletions

View File

@ -227,6 +227,59 @@ Create the child template in the path you configured above and extend `geerlingg
{% endblock %}
```
## Manage conf.d content
If you can't repeat the same options again and again, you can generate files under **conf.d** directory (those files will be applied automaticaly) or under **include.d** directory (you have to include those files manualy)
### Example: conf.d/files
```yaml
nginx_conf_path: /etc/nginx/conf.d
nginx_conf_d:
- file_name: json_logs.conf
content: |
# Managed by ansible (ansible-nginx-role/conf_d)
log_format json escape=json '{"time": $msec, '
'"resp_body_size": $body_bytes_sent, '
'"host": "$http_host", '
'"address": "$remote_addr", '
'"request_length": $request_length, '
'"method": "$request_method", '
'"uri": "$request_uri", '
'"status": $status, '
'"user_agent": "$http_user_agent", '
'"resp_time": $request_time, '
'"upstream_addr": "$upstream_addr", '
'"upstream_status": "$upstream_status", '
'"upstream_header_time": "$upstream_header_time", '
'"upstream_response_time": "$upstream_response_time", '
'"upstream_connect_time": "$upstream_connect_time", '
'"referer": "$http_referer"}';
```
### Example: include.d/files
```yaml
nginx_include_path: /etc/nginx/include.d
nginx_include_d:
- file_name: deny_htaccess.conf
content: |
location ~ /\.ht {
deny all;
}
vhost:
- filename: example_com.conf
listen: "443 ssl http2"
server_name: "example.com"
root: "/var/www/example.com"
index: "index.php index.html index.htm"
extra_parameters: |
include ../include.d/deny_htaccess.conf;
```
## Dependencies
None.

View File

@ -91,3 +91,27 @@ nginx_log_format: |-
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
###### Nginx conf.d content
nginx_conf_path: /etc/nginx/conf.d
nginx_conf_d:
- file_name: json_logs.conf
content: |
# Managed by ansible (ansible-nginx-role/conf_d)
log_format json escape=json '{"time": $msec, '
'"resp_body_size": $body_bytes_sent, '
'"host": "$http_host", '
'"address": "$remote_addr", '
'"request_length": $request_length, '
'"method": "$request_method", '
'"uri": "$request_uri", '
'"status": $status, '
'"user_agent": "$http_user_agent", '
'"resp_time": $request_time, '
'"upstream_addr": "$upstream_addr", '
'"upstream_status": "$upstream_status", '
'"upstream_header_time": "$upstream_header_time", '
'"upstream_response_time": "$upstream_response_time", '
'"upstream_connect_time": "$upstream_connect_time", '
'"referer": "$http_referer"}';

19
tasks/conf_d.yml Normal file
View File

@ -0,0 +1,19 @@
# Manage conf.d content
#
# DOESN'T DELETE EXISTING FILES
#
---
- name: "Create the {{ nginx_conf_d }} directory"
file:
path: "{{ nginx_conf_d }}"
state: directory
- name: Deploy conf.d files
copy:
content: "{{ item.content }}"
dest: "{{ nginx_conf_path }}/{{ item.file_name }}"
mode: "0640"
owner: root
with_items: "{{ nginx_conf_d }}"
when: nginx_conf_d is defined
notify: reload nginx

19
tasks/include_d.yml Normal file
View File

@ -0,0 +1,19 @@
# Manage include.d content
#
# DOESN'T DELETE EXISTING FILES
#
---
- name: "Create the {{ nginx_include_d }} directory"
file:
path: "{{ nginx_include_d }}"
state: directory
- name: Deploy conf.d files
copy:
content: "{{ item.content }}"
dest: "{{ nginx_include_path }}/{{ item.file_name }}"
mode: "0640"
owner: root
with_items: "{{ nginx_include_d }}"
when: nginx_include_d is defined
#notify: reload nginx

View File

@ -27,6 +27,12 @@
- include_tasks: setup-Archlinux.yml
when: ansible_os_family == 'Archlinux'
# Manage conf.d content
- import_tasks: conf_d.yml
# Manage conf.d content
- import_tasks: include_d.yml
# Vhost configuration.
- import_tasks: vhosts.yml