From ef38655431e0d98537540da4eba769b1ba87cd20 Mon Sep 17 00:00:00 2001 From: Bator Horvath Date: Mon, 7 Oct 2019 15:31:10 +1100 Subject: [PATCH 1/2] Allow variables to be passed to amazon-cloudwatch-agent.json template --- CHANGELOG.md | 5 ++++- libraries/agent_installer.rb | 6 +++++- metadata.rb | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c97db0..bbbaeb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ This file is used to list changes made in each version of the aws_cloudwatch cookbook. -## 1.0.2 (2019-04.07) +## 1.0.3 (2019-10-07) +- Allow variables to be passed to amazon-cloudwatch-agent.json template + +## 1.0.2 (2019-04-07) - Ubuntu-18.04 support ## 1.0.1 (2019-01-14) diff --git a/libraries/agent_installer.rb b/libraries/agent_installer.rb index 1610fd3..18e8234 100644 --- a/libraries/agent_installer.rb +++ b/libraries/agent_installer.rb @@ -7,6 +7,7 @@ class AgentInstaller < Chef::Resource property :config, String property :json_config, String + property :json_variables, Hash property :config_params, Hash, default: {'param' => 'value'} default_action :install @@ -111,6 +112,9 @@ class AgentInstaller < Chef::Resource template json_path do action :create source new_resource.json_config + if new_resource.json_variables + variables new_resource.json_variables + end end script 'amazon-cloudwatch-agent-config-translator' do action :run @@ -196,4 +200,4 @@ class AgentInstaller < Chef::Resource end end end -end \ No newline at end of file +end diff --git a/metadata.rb b/metadata.rb index 2fb27e3..05ea97a 100644 --- a/metadata.rb +++ b/metadata.rb @@ -2,7 +2,7 @@ maintainer 'Gennady Potapov' license 'Apache-2.0' description 'Provides aws_cloudwatch_agent resource' -version '1.0.2' +version '1.0.3' chef_version '>= 12.14' if respond_to?(:chef_version) supports 'ubuntu', '= 14.04' From cdb00435a41475e2a5782e0193c4b640efbe9331 Mon Sep 17 00:00:00 2001 From: Bator Horvath <5924171+bhorvath@users.noreply.github.com> Date: Tue, 1 Sep 2020 15:45:18 +1000 Subject: [PATCH 2/2] Update readme with json_variables example --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index a688b5e..0c45f2f 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,39 @@ Amazon CloudWatch Agent configuration file which defines which metrics/logs are Place the `amazon-cloudwatch-agent.json.erb` file to `templates` directory. This is an agent configuration for metrics and logs collection. See AWS documentation for more information: [Manually Create or Edit the CloudWatch Agent Configuration File](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html#CloudWatch-Agent-Configuration-File-Complete-Example) +### json_variables + +A hash of variables can be passed into the template file. This is similar to the way variables are used in the [template resource](https://docs.chef.io/resources/template/). +As an example, define your resource with variables: + +``` +aws_cloudwatch_agent 'default' do + action [:install, :configure, :restart] + json_config 'amazon-cloudwatch-agent.json.erb' + json_variables ({ + :disks => '/' + }) +end +``` + +Then define your `amazon-cloudwatch-agent.json.erb` template: + +``` +{ + "metrics": { + "metrics_collected": { + "disk": { + "measurement": [ + "used_percent" + ], + "resources": [ + <%= @disks %> + ] + } + } + } +} +``` ### config @@ -105,6 +138,7 @@ The `aws_cloudwatch_agent` resource installs AWS Cloudwatch Agent. * `config` - A template name for a custom `test-config.toml` file * `config_params` - A hash with `test-config.toml` parameters * `json_config` - A template name for an `amazon-cloudwatch-agent.json` file +* `json_variables` - A hash with variables available to the `amazon-cloudwatch-agent.json` template #### Example @@ -112,6 +146,9 @@ The `aws_cloudwatch_agent` resource installs AWS Cloudwatch Agent. aws_cloudwatch_agent 'default' do action [:install, :configure, :restart] json_config 'amazon-cloudwatch-agent.json.erb' + json_variables ({ + :disks => '/' + }) config_params :shared_credential_profile => 'test_profile', :shared_credential_file => '/etc/test_credential_file', :http_proxy => 'http://192.168.0.1',