From 1d296c7087d0ae03142c179d73c83331febdf555 Mon Sep 17 00:00:00 2001 From: Marc Ransome Date: Thu, 6 May 2021 10:14:07 +0100 Subject: [PATCH] Ensure app-specific CloudWatch configs are appended after agent restart --- roles/deploy/defaults/main.yml | 2 +- roles/deploy/tasks/deploy.yml | 10 +++------- roles/deploy/tasks/main.yml | 17 ++++++++++++++--- .../templates/cloudwatch-config-service.json.j2 | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/roles/deploy/defaults/main.yml b/roles/deploy/defaults/main.yml index c4ad9c3..1e69ade 100644 --- a/roles/deploy/defaults/main.yml +++ b/roles/deploy/defaults/main.yml @@ -7,7 +7,7 @@ cloudwatch_agent: metrics_collection_interval: 60 user: cwagent -tuxedo_log_default_parent: /var/log/tuxedo +tuxedo_logs_path: /var/log/tuxedo tuxedo_log_files: ewf: diff --git a/roles/deploy/tasks/deploy.yml b/roles/deploy/tasks/deploy.yml index 764420b..b0786ca 100644 --- a/roles/deploy/tasks/deploy.yml +++ b/roles/deploy/tasks/deploy.yml @@ -41,7 +41,7 @@ - name: "{{ tuxedo_user }} : Create service logs directory" file: - path: "/var/log/tuxedo/{{ tuxedo_user }}" + path: "{{ tuxedo_logs_path }}/{{ tuxedo_user }}" owner: "{{ tuxedo_user }}" group: "{{ tuxedo_user }}" mode: 0755 @@ -50,7 +50,7 @@ - name: "{{ tuxedo_user }} : Populate template config files" template: src: "{{ item }}" - dest: "{{ new_deployment_files.path }}/config//{{ item | basename | replace('.j2', '') }}" + dest: "{{ new_deployment_files.path }}/config/{{ item | basename | replace('.j2', '') }}" owner: "{{ tuxedo_user }}" group: "{{ tuxedo_user }}" mode: 0644 @@ -171,13 +171,9 @@ - name: "{{ tuxedo_user }} : Create CloudWatch agent configuration file for Tuxedo service group" template: src: templates/cloudwatch-config-service.json.j2 - dest: "{{ cloudwatch_agent.config_dir }}//cloudwatch-config-{{ tuxedo_user }}.json" + dest: "{{ cloudwatch_agent.config_dir }}/cloudwatch-config-{{ tuxedo_user }}.json" trim_blocks: False -- name: "{{ tuxedo_user }} : Add configuration for Tuxedo service group to CloudWatch agent" - command: - cmd: "{{ cloudwatch_agent.path }} -a append-config -m ec2 -s -c file:{{ cloudwatch_agent.config_dir }}//cloudwatch-config-{{ tuxedo_user }}.json" - - name: "{{ tuxedo_user }} : Start Tuxedo services" become_user: "{{ tuxedo_user }}" shell: "source $HOME/deployment/config/envfile && tmboot -y" diff --git a/roles/deploy/tasks/main.yml b/roles/deploy/tasks/main.yml index ff7c8ab..9941bbe 100644 --- a/roles/deploy/tasks/main.yml +++ b/roles/deploy/tasks/main.yml @@ -28,7 +28,7 @@ cloudwatch_log_stream_name: "{{ ansible_ec2_instance_id }}_{{ ansible_ec2_hostname }}" region: "{{ ansible_ec2_instance_identity_document_region }}" -- name: Create CloudWatch agent configuration file for instance +- name: Create CloudWatch agent primary configuration file template: src: templates/cloudwatch-config.json.j2 dest: "{{ cloudwatch_agent.config_dir }}/cloudwatch-config.json" @@ -67,9 +67,9 @@ group: "{{ tuxedo_service_group }}" mode: 0755 -- name: "Create logs directory" +- name: "Create Tuxedo logs directory" file: - path: "/var/log/tuxedo" + path: "{{ tuxedo_logs_path }}" owner: root group: "{{ tuxedo_service_group }}" mode: 0755 @@ -80,6 +80,17 @@ loop_control: loop_var: tuxedo_user +- name: Find application-specific CloudWatch configuration files + find: + paths: "{{ cloudwatch_agent.config_dir }}" + patterns: 'cloudwatch-config-*.json' + register: cloudwatch_configs + +- name: "{{ tuxedo_user }} : Add configuration for Tuxedo service group to CloudWatch agent" + command: + cmd: "{{ cloudwatch_agent.path }} -a append-config -m ec2 -s -c file:{{ item.path }}" + loop: "{{ cloudwatch_configs.files }}" + - name: Remove temporary directories file: path: "{{ application_artefact_files.path }}" diff --git a/roles/deploy/templates/cloudwatch-config-service.json.j2 b/roles/deploy/templates/cloudwatch-config-service.json.j2 index 645d1d9..1a5bacb 100644 --- a/roles/deploy/templates/cloudwatch-config-service.json.j2 +++ b/roles/deploy/templates/cloudwatch-config-service.json.j2 @@ -6,7 +6,7 @@ {% if tuxedo_user in tuxedo_log_files -%} {%+ for log in tuxedo_log_files[tuxedo_user] -%} { - "file_path": "{{ log.file_path_parent | default(tuxedo_log_default_parent) }}/{{ tuxedo_user }}/{{ log.file_pattern }}", + "file_path": "{{ tuxedo_logs_path }}/{{ tuxedo_user }}/{{ log.file_pattern }}", "log_group_name": "{{ log.cloudwatch_log_group_name }}", "log_stream_name": "{{ cloudwatch_log_stream_name }}" }{%- if not loop.last -%},{%- endif -%}