├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── defaults └── main.yml ├── meta └── main.yml ├── tasks └── main.yml ├── templates └── logrotate.d.j2 └── tests ├── Vagrantfile ├── inventory └── test.yml /.gitignore: -------------------------------------------------------------------------------- 1 | tests/.vagrant 2 | test.retry 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: python 3 | python: "2.7" 4 | before_install: 5 | - sudo apt-get update -qq 6 | - sudo apt-get install -qq python-apt python-pycurl 7 | install: 8 | - pip install ansible 9 | script: 10 | - "printf '[defaults]\nroles_path = ../' > ansible.cfg" 11 | - ansible-playbook -i tests/inventory --syntax-check tests/test.yml 12 | - ansible-playbook -i tests/inventory --connection=local --become -vvvv tests/test.yml 13 | notifications: 14 | email: false 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016-14, Nick Hammond 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of ansiblebit nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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) 110 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | logrotate_conf_dir: "/etc/logrotate.d/" 2 | logrotate_scripts: [] 3 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 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: [] 19 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 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 14 | -------------------------------------------------------------------------------- /templates/logrotate.d.j2: -------------------------------------------------------------------------------- 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 | } 24 | -------------------------------------------------------------------------------- /tests/Vagrantfile: -------------------------------------------------------------------------------- 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 19 | -------------------------------------------------------------------------------- /tests/inventory: -------------------------------------------------------------------------------- 1 | localhost 2 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 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" 30 | --------------------------------------------------------------------------------