Authored by chunhua.zhang

add log rotate roles

  1 +# logrotate
  2 +
  3 +[![Build Status](https://travis-ci.org/nickhammond/ansible-logrotate.svg?branch=master)](https://travis-ci.org/nickhammond/ansible-logrotate)
  4 +
  5 +Installs logrotate and provides an easy way to setup additional logrotate scripts by
  6 +specifying a list of directives.
  7 +
  8 +## Requirements
  9 +
  10 +None
  11 +
  12 +## Role Variables
  13 +
  14 +**logrotate_scripts**: A list of logrotate scripts and the directives to use for the rotation.
  15 +
  16 +* name - The name of the script that goes into /etc/logrotate.d/
  17 +* path - Path to point logrotate to for the log rotation
  18 +* paths - A list of paths to point logrotate to for the log rotation.
  19 +* options - List of directives for logrotate, view the logrotate man page for specifics
  20 +* scripts - Dict of scripts for logrotate (see Example below)
  21 +
  22 +```
  23 +logrotate_scripts:
  24 + - name: rails
  25 + path: "/srv/current/log/*.log"
  26 + options:
  27 + - weekly
  28 + - size 25M
  29 + - missingok
  30 + - compress
  31 + - delaycompress
  32 + - copytruncate
  33 +```
  34 +
  35 +```
  36 +logrotate_scripts:
  37 + - name: rails
  38 + paths:
  39 + - "/srv/current/scare.log"
  40 + - "/srv/current/hide.log"
  41 + options:
  42 + - weekly
  43 + - size 25M
  44 + - missingok
  45 + - compress
  46 + - delaycompress
  47 + - copytruncate
  48 +```
  49 +
  50 +## Dependencies
  51 +
  52 +None
  53 +
  54 +## Example Playbook
  55 +
  56 +Setting up logrotate for additional Nginx logs, with postrotate script.
  57 +
  58 +```
  59 +- hosts: all
  60 + vars:
  61 + logrotate_scripts:
  62 + - name: nginx-options
  63 + path: /var/log/nginx/options.log
  64 + options:
  65 + - daily
  66 + - weekly
  67 + - size 25M
  68 + - rotate 7
  69 + - missingok
  70 + - compress
  71 + - delaycompress
  72 + - copytruncate
  73 +
  74 + - name: nginx-scripts
  75 + path: /var/log/nginx/scripts.log
  76 + options:
  77 + - daily
  78 + - weekly
  79 + - size 25M
  80 + scripts:
  81 + postrotate: "echo test"
  82 +
  83 + roles:
  84 + - ansible-logrotate
  85 +```
  86 +
  87 +## Testing locally
  88 +
  89 +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.
  90 +
  91 +To run the test playbook locally within a Vagrant virtual machine:
  92 +
  93 +```
  94 +cd tests
  95 +vagrant up --provision
  96 +```
  97 +
  98 +## License
  99 +
  100 +[BSD](https://raw.githubusercontent.com/nickhammond/logrotate/master/LICENSE)
  101 +
  102 +## Author Information
  103 +
  104 +* [nickhammond](https://github.com/nickhammond) | [www](http://www.nickhammond.com) | [twitter](http://twitter.com/nickhammond)
  105 +* [bigjust](https://github.com/bigjust)
  106 +* [steenzout](https://github.com/steenzout)
  107 +* [jeancornic](https://github.com/jeancornic)
  108 +* [duhast](https://github.com/duhast)
  109 +* [kagux](https://github.com/kagux)
  1 +logrotate_conf_dir: "/etc/logrotate.d/"
  2 +logrotate_scripts: []
  1 +---
  2 +galaxy_info:
  3 + author: Nick Hammond
  4 + description: Role to configure logrotate scripts
  5 + license: BSD
  6 + min_ansible_version: 1.9
  7 + platforms:
  8 + - name: Ubuntu
  9 + versions:
  10 + - lucid
  11 + - precise
  12 + - trusty
  13 + - name: EL
  14 + versions:
  15 + - 7
  16 + categories:
  17 + - system
  18 +dependencies: []
  1 +---
  2 +- name: nickhammond.logrotate | Install logrotate
  3 + package:
  4 + name: logrotate
  5 + state: present
  6 + when: logrotate_scripts is defined and logrotate_scripts|length > 0
  7 +
  8 +- name: nickhammond.logrotate | Setup logrotate.d scripts
  9 + template:
  10 + src: logrotate.d.j2
  11 + dest: "{{ logrotate_conf_dir }}{{ item.name }}"
  12 + with_items: "{{ logrotate_scripts }}"
  13 + when: logrotate_scripts is defined
  1 +# {{ ansible_managed }}
  2 +
  3 +{% if 'path' in item %}
  4 +"{{ item.path }}"
  5 +{% elif 'paths' in item %}
  6 +{% for path in item.paths %}
  7 +"{{ path }}"
  8 +{% endfor %}
  9 +{% endif %}
  10 +{
  11 + {% if item.options is defined -%}
  12 + {% for option in item.options -%}
  13 + {{ option }}
  14 + {% endfor -%}
  15 + {% endif %}
  16 + {%- if item.scripts is defined -%}
  17 + {%- for name, script in item.scripts.items() -%}
  18 + {{ name }}
  19 + {{ script }}
  20 + endscript
  21 + {% endfor -%}
  22 + {% endif -%}
  23 +}
  1 +# -*- mode: ruby -*-
  2 +# vi: set ft=ruby :
  3 +@ansible_home = "/home/vagrant/.ansible"
  4 +
  5 +Vagrant.configure("2") do |config|
  6 + config.vm.box = "ubuntu/trusty64"
  7 +
  8 + # Copy the Ansible playbook over to the guest machine, run rsync-auto to automatically
  9 + # pull in the latest changes while a VM is running.
  10 + config.vm.synced_folder "../", "#{@ansible_home}/roles/ansible-logrotate", type: 'rsync'
  11 +
  12 + # The working ansible directory created by ansible_local is owned by root
  13 + config.vm.provision "shell", inline: "chown vagrant:vagrant #{@ansible_home}"
  14 +
  15 + config.vm.provision "ansible_local" do |ansible|
  16 + ansible.playbook = "test.yml"
  17 + end
  18 +end
  1 +---
  2 +- hosts: all
  3 + become: True
  4 + vars:
  5 + logrotate_scripts:
  6 + - name: nginx-options
  7 + path: /var/log/nginx/options.log
  8 + options:
  9 + - daily
  10 +
  11 + - name: nginx-scripts
  12 + path: /var/log/nginx/scripts.log
  13 + scripts:
  14 + postrotate: "echo test"
  15 +
  16 + - name: multiple-paths
  17 + paths:
  18 + - /var/log/nginx/options.log
  19 + - /var/log/nginx/scripts.log
  20 +
  21 + roles:
  22 + - ansible-logrotate
  23 +
  24 + tasks:
  25 + - name: Verify logrotate config check passes
  26 + shell: logrotate -d "{{ logrotate_conf_dir }}{{ item.name }}"
  27 + with_items: "{{ logrotate_scripts }}"
  28 + register: logrotate_tests
  29 + failed_when: "'error' in logrotate_tests.stderr"