Authored by chunhua.zhang

add log rotate roles

# logrotate
[![Build Status](https://travis-ci.org/nickhammond/ansible-logrotate.svg?branch=master)](https://travis-ci.org/nickhammond/ansible-logrotate)
Installs logrotate and provides an easy way to setup additional logrotate scripts by
specifying a list of directives.
## Requirements
None
## Role Variables
**logrotate_scripts**: A list of logrotate scripts and the directives to use for the rotation.
* name - The name of the script that goes into /etc/logrotate.d/
* path - Path to point logrotate to for the log rotation
* paths - A list of paths to point logrotate to for the log rotation.
* options - List of directives for logrotate, view the logrotate man page for specifics
* scripts - Dict of scripts for logrotate (see Example below)
```
logrotate_scripts:
- name: rails
path: "/srv/current/log/*.log"
options:
- weekly
- size 25M
- missingok
- compress
- delaycompress
- copytruncate
```
```
logrotate_scripts:
- name: rails
paths:
- "/srv/current/scare.log"
- "/srv/current/hide.log"
options:
- weekly
- size 25M
- missingok
- compress
- delaycompress
- copytruncate
```
## Dependencies
None
## Example Playbook
Setting up logrotate for additional Nginx logs, with postrotate script.
```
- hosts: all
vars:
logrotate_scripts:
- name: nginx-options
path: /var/log/nginx/options.log
options:
- daily
- weekly
- size 25M
- rotate 7
- missingok
- compress
- delaycompress
- copytruncate
- name: nginx-scripts
path: /var/log/nginx/scripts.log
options:
- daily
- weekly
- size 25M
scripts:
postrotate: "echo test"
roles:
- ansible-logrotate
```
## Testing locally
This role is already configured to run on travis CI within a test playbook but it's useful to be able to run and debug a role locally which can be done via Vagrant and the `ansible_local` provisioner.
To run the test playbook locally within a Vagrant virtual machine:
```
cd tests
vagrant up --provision
```
## License
[BSD](https://raw.githubusercontent.com/nickhammond/logrotate/master/LICENSE)
## Author Information
* [nickhammond](https://github.com/nickhammond) | [www](http://www.nickhammond.com) | [twitter](http://twitter.com/nickhammond)
* [bigjust](https://github.com/bigjust)
* [steenzout](https://github.com/steenzout)
* [jeancornic](https://github.com/jeancornic)
* [duhast](https://github.com/duhast)
* [kagux](https://github.com/kagux)
... ...
logrotate_conf_dir: "/etc/logrotate.d/"
logrotate_scripts: []
... ...
---
galaxy_info:
author: Nick Hammond
description: Role to configure logrotate scripts
license: BSD
min_ansible_version: 1.9
platforms:
- name: Ubuntu
versions:
- lucid
- precise
- trusty
- name: EL
versions:
- 7
categories:
- system
dependencies: []
... ...
---
- name: nickhammond.logrotate | Install logrotate
package:
name: logrotate
state: present
when: logrotate_scripts is defined and logrotate_scripts|length > 0
- name: nickhammond.logrotate | Setup logrotate.d scripts
template:
src: logrotate.d.j2
dest: "{{ logrotate_conf_dir }}{{ item.name }}"
with_items: "{{ logrotate_scripts }}"
when: logrotate_scripts is defined
... ...
# {{ ansible_managed }}
{% if 'path' in item %}
"{{ item.path }}"
{% elif 'paths' in item %}
{% for path in item.paths %}
"{{ path }}"
{% endfor %}
{% endif %}
{
{% if item.options is defined -%}
{% for option in item.options -%}
{{ option }}
{% endfor -%}
{% endif %}
{%- if item.scripts is defined -%}
{%- for name, script in item.scripts.items() -%}
{{ name }}
{{ script }}
endscript
{% endfor -%}
{% endif -%}
}
... ...
# -*- mode: ruby -*-
# vi: set ft=ruby :
@ansible_home = "/home/vagrant/.ansible"
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
# Copy the Ansible playbook over to the guest machine, run rsync-auto to automatically
# pull in the latest changes while a VM is running.
config.vm.synced_folder "../", "#{@ansible_home}/roles/ansible-logrotate", type: 'rsync'
# The working ansible directory created by ansible_local is owned by root
config.vm.provision "shell", inline: "chown vagrant:vagrant #{@ansible_home}"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "test.yml"
end
end
... ...
---
- hosts: all
become: True
vars:
logrotate_scripts:
- name: nginx-options
path: /var/log/nginx/options.log
options:
- daily
- name: nginx-scripts
path: /var/log/nginx/scripts.log
scripts:
postrotate: "echo test"
- name: multiple-paths
paths:
- /var/log/nginx/options.log
- /var/log/nginx/scripts.log
roles:
- ansible-logrotate
tasks:
- name: Verify logrotate config check passes
shell: logrotate -d "{{ logrotate_conf_dir }}{{ item.name }}"
with_items: "{{ logrotate_scripts }}"
register: logrotate_tests
failed_when: "'error' in logrotate_tests.stderr"
... ...