├── LICENSE ├── README.md ├── tasks ├── cleanup.yml ├── install.yml └── main.yml └── templates └── awslogs.conf.j2 /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Christian E Willman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible CloudWatch Logs Role 2 | An Ansible role for forwarding log files to [CloudWatch](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html). 3 | 4 | You can opt to forward one or many log files to CloudWatch; each file may use a different datetime format and belong to a different log group. 5 | 6 | ## Prerequisites 7 | `aws-cloudwatch-logs` assumes you are using an official Ubuntu AMI, and it obviously only makes sense to run within EC2. Sorry if this doesn't fit your use case. 8 | 9 | ## Usage 10 | Include the `aws-cloudwatch-logs` role in your playbook yml like this: 11 | 12 | - role: aws-cloudwatch-logs 13 | logs: 14 | - file: /var/log/syslog 15 | format: "%b %d %H:%M:%S" 16 | group_name: syslog 17 | - file: /var/log/my_cool_log 18 | format: "%b %d %H:%M:%S" 19 | group_name: my-cool-log 20 | 21 | ## Author 22 | Christian Willman 23 | 24 | ## License 25 | MIT 26 | -------------------------------------------------------------------------------- /tasks/cleanup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Remove /tmp/awslogs.conf 3 | file: 4 | path: /tmp/awslogs.conf 5 | state: absent 6 | 7 | - name: Remove /tmp/awslogs-agent-setup.py 8 | file: 9 | path: /tmp/awslogs-agent-setup.py 10 | state: absent 11 | -------------------------------------------------------------------------------- /tasks/install.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Discover the EC2 instance region 3 | # Reversing the string is hacky but readable and efficient enough... 4 | shell: "ec2metadata --availability-zone | rev | cut -c 2- | rev" 5 | register: ec2_region 6 | 7 | - name: Configure /tmp/awslogs.conf 8 | template: 9 | dest: /tmp/awslogs.conf 10 | group: root 11 | mode: 0600 12 | owner: root 13 | src: awslogs.conf.j2 14 | 15 | - name: Download the awslogs-agent-setup.py script 16 | get_url: 17 | dest: /tmp/awslogs-agent-setup.py 18 | group: root 19 | owner: root 20 | mode: 0600 21 | url: https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py 22 | 23 | - name: Install the AWS CloudWatch Logs daemon 24 | shell: python /tmp/awslogs-agent-setup.py -n -r {{ ec2_region.stdout }} -c /tmp/awslogs.conf 25 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - include: install.yml 3 | - include: cleanup.yml 4 | -------------------------------------------------------------------------------- /templates/awslogs.conf.j2: -------------------------------------------------------------------------------- 1 | # 2 | # ------------------------------------------ 3 | # CLOUDWATCH LOGS AGENT CONFIGURATION FILE 4 | # ------------------------------------------ 5 | # 6 | # --- DESCRIPTION --- 7 | # This file is used by the CloudWatch Logs Agent to specify what log data to send to the service and how. 8 | # You can modify this file at any time to add, remove or change configuration. 9 | # 10 | # NOTE: A running agent must be stopped and restarted for configuration changes to take effect. 11 | # 12 | # --- CLOUDWATCH LOGS DOCUMENTATION --- 13 | # https://aws.amazon.com/documentation/cloudwatch/ 14 | # 15 | # --- CLOUDWATCH LOGS CONSOLE --- 16 | # https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs: 17 | # 18 | # --- AGENT COMMANDS --- 19 | # To check or change the running status of the CloudWatch Logs Agent, use the following: 20 | # 21 | # To check running status: /etc/init.d/awslogs status 22 | # To stop the agent: /etc/init.d/awslogs stop 23 | # To start the agent: /etc/init.d/awslogs start 24 | # 25 | # --- AGENT LOG OUTPUT --- 26 | # You can find logs for the agent in /var/log/awslogs.log 27 | # You can find logs for the agent script in /var/log/awslogs-agent-setup.log 28 | # 29 | 30 | # ------------------------------------------ 31 | # CONFIGURATION DETAILS 32 | # ------------------------------------------ 33 | 34 | [general] 35 | # Path to the CloudWatch Logs agent's state file. The agent uses this file to maintain 36 | # client side state across its executions. 37 | state_file = /var/awslogs/state/agent-state 38 | 39 | ## Each log file is defined in its own section. The section name doesn't 40 | ## matter as long as its unique within this file. 41 | #[kern.log] 42 | # 43 | ## Path of log file for the agent to monitor and upload. 44 | #file = /var/log/kern.log 45 | # 46 | ## Name of the destination log group. 47 | #log_group_name = kern.log 48 | # 49 | ## Name of the destination log stream. You may use {hostname} to use target machine's hostname. 50 | #log_stream_name = {instance_id} # Defaults to ec2 instance id 51 | # 52 | ## Format specifier for timestamp parsing. Here are some sample formats: 53 | ## Use '%b %d %H:%M:%S' for syslog (Apr 24 08:38:42) 54 | ## Use '%d/%b/%Y:%H:%M:%S' for apache log (10/Oct/2000:13:55:36) 55 | ## Use '%Y-%m-%d %H:%M:%S' for rails log (2008-09-08 11:52:54) 56 | #datetime_format = %b %d %H:%M:%S # Specification details in the table below. 57 | # 58 | ## A batch is buffered for buffer-duration amount of time or 32KB of log events. 59 | ## Defaults to 5000 ms and its minimum value is 5000 ms. 60 | #buffer_duration = 5000 61 | # 62 | # Use 'end_of_file' to start reading from the end of the file. 63 | # Use 'start_of_file' to start reading from the beginning of the file. 64 | #initial_position = start_of_file 65 | # 66 | ## Encoding of file 67 | #encoding = utf-8 # Other supported encodings include: ascii, latin-1 68 | # 69 | # 70 | # 71 | # Following table documents the detailed datetime format specification: 72 | # ---------------------------------------------------------------------------------------------------------------------- 73 | # Directive Meaning Example 74 | # ---------------------------------------------------------------------------------------------------------------------- 75 | # %a Weekday as locale's abbreviated name. Sun, Mon, ..., Sat (en_US) 76 | # ---------------------------------------------------------------------------------------------------------------------- 77 | # %A Weekday as locale's full name. Sunday, Monday, ..., Saturday (en_US) 78 | # ---------------------------------------------------------------------------------------------------------------------- 79 | # %w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, ..., 6 80 | # ---------------------------------------------------------------------------------------------------------------------- 81 | # %d Day of the month as a zero-padded decimal numbers. 01, 02, ..., 31 82 | # ---------------------------------------------------------------------------------------------------------------------- 83 | # %b Month as locale's abbreviated name. Jan, Feb, ..., Dec (en_US) 84 | # ---------------------------------------------------------------------------------------------------------------------- 85 | # %B Month as locale's full name. January, February, ..., December (en_US) 86 | # ---------------------------------------------------------------------------------------------------------------------- 87 | # %m Month as a zero-padded decimal number. 01, 02, ..., 12 88 | # ---------------------------------------------------------------------------------------------------------------------- 89 | # %y Year without century as a zero-padded decimal number. 00, 01, ..., 99 90 | # ---------------------------------------------------------------------------------------------------------------------- 91 | # %Y Year with century as a decimal number. 1970, 1988, 2001, 2013 92 | # ---------------------------------------------------------------------------------------------------------------------- 93 | # %H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23 94 | # ---------------------------------------------------------------------------------------------------------------------- 95 | # %I Hour (12-hour clock) as a zero-padded decimal numbers. 01, 02, ..., 12 96 | # ---------------------------------------------------------------------------------------------------------------------- 97 | # %p Locale's equivalent of either AM or PM. AM, PM (en_US) 98 | # ---------------------------------------------------------------------------------------------------------------------- 99 | # %M Minute as a zero-padded decimal number. 00, 01, ..., 59 100 | # ---------------------------------------------------------------------------------------------------------------------- 101 | # %S Second as a zero-padded decimal numbers. 00, 01, ..., 59 102 | # ---------------------------------------------------------------------------------------------------------------------- 103 | # %f Microsecond as a decimal number, zero-padded on the left. 000000, 000001, ..., 999999 104 | # ---------------------------------------------------------------------------------------------------------------------- 105 | # %z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030 106 | # ---------------------------------------------------------------------------------------------------------------------- 107 | # %j Day of the year as a zero-padded decimal number. 001, 002, ..., 365 108 | # ---------------------------------------------------------------------------------------------------------------------- 109 | # %U Week number of the year (Sunday as the first day of the week) as a zero padded 00, 01, ..., 53 110 | # decimal number. All days in a new year preceding the first Sunday are considered 111 | # to be in week 0. 112 | # ---------------------------------------------------------------------------------------------------------------------- 113 | # %W Week number of the year (Monday as the first day of the week) as a decimal number. 00, 01, ..., 53 114 | # All days in a new year preceding the first Monday are considered to be in week 0. 115 | # ---------------------------------------------------------------------------------------------------------------------- 116 | # %c Locale's appropriate date and time representation. Tue Aug 16 21:30:00 1988 (en_US) 117 | # ---------------------------------------------------------------------------------------------------------------------- 118 | 119 | {% for log in logs %} 120 | [{{ log.file }}] 121 | datetime_format = {{ log.format }} 122 | file = {{ log.file }} 123 | log_group_name = {{ log.group_name }} 124 | log_stream_name = {instance_id} 125 | {% endfor %} 126 | --------------------------------------------------------------------------------