Creating an Ansible role for CloudWatch logs

To send our logs to CloudWatch, AWS provides a daemon called awslogs. We are going to install and configure it through Ansible.

Go into your ansible roles directory:

$ cd ansible/roles  

Create a new role called awslogs:

$ ansible-galaxy init awslogs
- awslogs was created successfully  

We will first edit the task file awslogs/tasks/main.yml. Our first operation will be to install the package. For that, we will use the yum module:

--- 
# tasks file for awslogs  
- name: install awslogs 
  yum: 
    name: awslogs 
    state: present 

We will want to configure the service dynamically with Ansible. For that, we will want to create a handler to restart awslogs when the configuration changes.

Edit the file awslogs/handlers/main.yml ...

Get Effective DevOps with AWS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.