Skip to content

Commit 3db5164

Browse files
authored
Add custom logrotate config (#251)
1 parent 5a77317 commit 3db5164

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,60 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
362362
```
363363

364364

365+
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and changes logs permission with custom logrotate config.
366+
367+
```yaml
368+
---
369+
- hosts: localhost
370+
become: true
371+
roles:
372+
- role: nginxinc.nginx
373+
vars:
374+
nginx_http_template_enable: true
375+
nginx_http_template:
376+
default:
377+
template_file: http/default.conf.j2
378+
conf_file_name: default.conf
379+
conf_file_location: /etc/nginx/conf.d/
380+
servers:
381+
server1:
382+
listen:
383+
listen_localhost:
384+
# ip: 0.0.0.0
385+
port: 80
386+
server_name: localhost
387+
error_page: /usr/share/nginx/html
388+
access_log:
389+
- name: main
390+
location: /var/log/nginx/access.log
391+
error_log:
392+
location: /var/log/nginx/error.log
393+
level: warn
394+
autoindex: false
395+
web_server:
396+
locations:
397+
default:
398+
location: /
399+
html_file_location: /usr/share/nginx/html
400+
html_file_name: index.html
401+
autoindex: false
402+
http_demo_conf: false
403+
nginx_logrotate_conf_enable: true
404+
nginx_logrotate_conf:
405+
paths:
406+
- "/var/log/nginx/*.log"
407+
options:
408+
- daily
409+
- missingok
410+
- rotate 14
411+
- compress
412+
- delaycompress
413+
- notifempty
414+
- create 0644 www-data adm # Changes nginx logs permissions
415+
- sharedscripts
416+
```
417+
418+
365419
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing NGINX Plus.
366420

367421
```yaml

defaults/main/logrotate.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
# Create custom logrotate config
3+
nginx_logrotate_conf_enable: false
4+
nginx_logrotate_conf:
5+
paths:
6+
- "/var/log/nginx/*.log"
7+
options:
8+
- daily
9+
- missingok
10+
- rotate 14
11+
- compress
12+
- delaycompress
13+
- notifempty
14+
- create 0644 www-data adm # Changes nginx logs permissions
15+
- sharedscripts

molecule/common/playbook_template.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,16 @@
351351
port: 8091
352352
weight: 1
353353
health_check: max_fails=1 fail_timeout=10s
354+
355+
nginx_logrotate_conf_enable: true
356+
nginx_logrotate_conf:
357+
paths:
358+
- "/var/log/nginx/*.log"
359+
options:
360+
- daily
361+
- missingok
362+
- rotate 14
363+
- compress
364+
- delaycompress
365+
- notifempty
366+
- sharedscripts

tasks/conf/logrotate.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
- name: "(Config: Alpine) Install Logrotate"
3+
apk:
4+
name: logrotate
5+
when: ansible_os_family == "Alpine"
6+
7+
- name: "(Config: Ubuntu/Debian) Install Logrotate"
8+
apt:
9+
name: logrotate
10+
state: present
11+
when: ansible_os_family == "Debian"
12+
13+
- name: "(Config: CentOS/RedHat) Install Logrotate"
14+
yum:
15+
name: logrotate
16+
state: present
17+
when: ansible_os_family == "RedHat"
18+
19+
- name: "(Config: SUSE) Add Logrotate Repo"
20+
zypper_repository:
21+
repo: https://download.opensuse.org/repositories/openSUSE:Leap:42.1/standard/openSUSE:Leap:42.1.repo
22+
when: ansible_os_family == "Suse"
23+
24+
- name: "(Config: SUSE) Install Logrotate"
25+
zypper:
26+
name: logrotate
27+
state: present
28+
when: ansible_os_family == "Suse"
29+
30+
- name: "(Config: All OSs) Create Logrotate Config"
31+
template:
32+
src: "logrotate/nginx.j2"
33+
dest: "/etc/logrotate.d/nginx"
34+
register: nginx_logrotate_result
35+
36+
- name: "(Config: All OSs) Ensure NGINX is Running"
37+
meta: flush_handlers
38+
39+
- name: "(Config: All OSs) Run Logrotate"
40+
command: logrotate -f /etc/logrotate.d/nginx
41+
changed_when: nginx_logrotate_result.changed

tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@
8383
- import_tasks: unit/install-unit.yml
8484
when: nginx_unit_enable | bool
8585
tags: nginx_install_unit
86+
87+
- import_tasks: conf/logrotate.yml
88+
when: nginx_logrotate_conf_enable | bool
89+
tags: nginx_logrotate_config

templates/logrotate/nginx.j2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% for path in nginx_logrotate_conf.paths %}
2+
{{ path }}
3+
{% endfor %}
4+
{
5+
{% for option in nginx_logrotate_conf.options %}
6+
{{ option }}
7+
{% endfor %}
8+
prerotate
9+
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
10+
run-parts /etc/logrotate.d/httpd-prerotate; \
11+
fi \
12+
endscript
13+
postrotate
14+
{% if ansible_os_family == "Debian" %}
15+
invoke-rc.d nginx rotate >/dev/null 2>&1
16+
{% else %}
17+
nginx -s reopen
18+
{% endif %}
19+
endscript
20+
}

0 commit comments

Comments
 (0)