christiangda.amazon_cloudwatch_agent
Ansible 角色:christiangda.amazon_cloudwatch_agent
这个角色用于安装 AWS CloudWatch Agent。
安装此角色的最好方法是使用命令 ansible-galaxy install christiangda.amazon_cloudwatch_agent
,Ansible Galaxy 存储库为christiangda.amazon_cloudwatch_agent。
存储库代码可在https://github.com/christiangda/ansible-role-amazon-cloudwatch-agent。
特性:
- 适用于 AWS EC2 实例 和 本地实例
- 支持不同版本的 Python、Ansible 和操作系统(更多详情请参见 持续集成矩阵)
- 从 AWS 发行包 下载并安装 CloudWatch Agent
- 当配置更改时重新加载服务
- 提供默认的 代理配置 文件(这是一个最小配置,不推荐使用)
- 旋转 CloudWatch Agent 日志文件
- 允许您加载自定义的 JSON 文件 / YAML 文件或内嵌配置,适用于代理、指标和日志部分
- 支持
amd64
和arm64
架构
注意事项:
- 版本 2.x.y 与版本 1.x.y 不兼容
- 从版本 2.x.y 开始,此角色默认不再安装 EPEL 存储库,您需要自行处理,建议使用我的
ansible 角色
christiangda.epel_repo - 从版本 2.x.y 开始,此角色默认不再创建 AWS CLI 配置文件(配置和凭证),您需要自行处理,建议使用我的
ansible 角色
christiangda.awscli 和 christiangda.awscli_configure。请参见示例(当 cwa_agent_mode: "onPremise") - 在检测到使用 以 collectd 检索自定义指标 时,自动安装
collectd OS 包
。根据OS
的不同,collectd 包
可能需要 EPEL 存储库 - 红帽用户:必须拥有有效的订阅,以避免出现依赖包安装问题。
更多详细信息请查看 VERSION.md。
要求
此角色适用于 RedHat、CentOS、Amazon Linux、Debian 和 Ubuntu 发行版。
- RedHat
- 7
- 8
- CentOS
- 7
- 8
- stream8
- Rocky Linux
- 8
- 9
- Amazon Linux
- 2
- Ubuntu
- 14.04 (*)
- 16.04 (*)
- 18.*
- 20.*
- 21.*
- 22.*
- Debian
- Stretch (9)
- buster (10)
- bullseye (11)
- Bookworm (12)
有关 Python 与 Ansible 版本的兼容性矩阵,请参见 Travis-CI 构建矩阵。
角色变量
变量 | 默认值 |
---|---|
cwa_conf_json_file_content | "" --> 空 |
cwa_agent_mode | "ec2" |
cwa_aws_region | "" |
cwa_use_credentials | false |
cwa_profile | "AmazonCloudWatchAgent" |
cwa_agent_profile_path | /root |
cwa_http_proxy | "" --> 空 |
cwa_https_proxy | "" --> 空 |
cwa_no_proxy | "169.254.169.254" |
cwa_logrotate_file_size | "10M" |
cwa_logrotate_files | 5 |
cwa_debug | false |
更多详细信息:请参见文件 defaults/main.yaml。
依赖关系
- 对于 RedHat/Centos 操作系统家族可能需要 EPEL 存储库
- 如果设置
cwa_agent_mode: "onPremise"
,则需要配置 AWS CLI 配置文件 - 如果您使用 以 collectd 检索自定义指标,则
collectd
包将被自动安装,根据OS
的不同,可能需要 EPEL 存储库。
示例剧本
当 cwa_agent_mode: "onPremise"
# 使用 ansible-vault 进行配置
# 示例:
# ansible-vault encrypt_string --ask-vault-pass --name 'cwa_access_key' 'AKIAIOSFODNN7EXAMPLE'
# ansible-vault encrypt_string --ask-vault-pass --name 'cwa_secret_key' 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
- hosts: servers
gather_facts: True
roles:
- role: christiangda.epel_repo # 如果没有安装 EPEL 存储库
when: >
ansible_os_family == 'RedHat' and (
ansible_distribution == 'CentOS' or
ansible_distribution == 'RedHat' or
ansible_distribution == 'Amazon'
)
- role: christiangda.awscli_configure # 如果没有配置 AWS CLI 配置文件
vars:
awscliconf_path: '/root'
awscliconf_files:
credentials:
- AmazonCloudWatchAgent:
aws_access_key_id: !vault |
$ANSIBLE_VAULT;1.1;AES256
30376338613338326663373366303234623665633339303338613463313564633832363237306137
3362643039616631323339383332306536333962346133310a383265376665316235653261616136
61306566623531356263346439633761633830323636646236373736353530396134636536666532
3939636433636364310a316639366139366566623337623536346661633339343766323936346336
65333035366635396138656132643262626438333961326266396466626464643766
aws_secret_access_key: !vault |
$ANSIBLE_VAULT;1.1;AES256
65643230613939303737336632346432393234616437383532386139616364316233333933643735
6633636261383163323362623562323333323533336564310a323030343431366135343035326635
33623161376634643939636464306139386662303034616531346632303039643238373834616266
3064623232373233610a346432646565396235316631626137653731376365333531323866626665
62656638623330643539653763636364363738653932653831316238633939356462653636633463
6130613761633565616533633332376565373062396565396261
config:
- profile AmazonCloudWatchAgent:
region: "eu-west-1"
- role: christiangda.amazon_cloudwatch_agent # 使用角色提供的最小代理配置
vars:
cwa_agent_mode: "onPremise"
cwa_aws_region: "eu-west-1"
cwa_profile: "AmazonCloudWatchAgent"
当 cwa_agent_mode: "ec2"
从 JSON 配置文件读取配置
---
- hosts: servers
gather_facts: True
roles:
- role: christiangda.amazon_cloudwatch_agent # 使用相对路径为 `files/CloudWatch.json` 的 JSON 文件
vars:
cwa_agent_mode: "ec2"
cwa_conf_json_file_content: "{{ lookup('file', 'files/CloudWatch.json') | from_json }}"
从 YAML 配置文件读取配置
---
- hosts: servers
gather_facts: True
roles:
- role: christiangda.amazon_cloudwatch_agent # 使用相对路径为 `files/CloudWatch.yaml` 的 YAML 文件
vars:
cwa_agent_mode: "ec2"
cwa_conf_json_file_content: "{{ lookup('file', 'files/CloudWatch.yaml') | from_yaml }}"
使用内嵌 YAML 配置文件
---
- hosts: centos7, centos6, ubuntu1804, ubuntu1810, debian8, debian9, amzn2
become: True
roles:
- role: christiangda.epel_repo # 如果没有安装 EPEL 存储库
when: >
ansible_os_family == 'RedHat' and (
ansible_distribution == 'CentOS' or
ansible_distribution == 'RedHat' or
ansible_distribution == 'Amazon'
)
- role: christiangda.awscli_configure # 如果没有配置 AWS CLI 配置文件
vars:
awscliconf_path: '/root'
awscliconf_files:
credentials:
- AmazonCloudWatchAgent:
aws_access_key_id: !vault |
$ANSIBLE_VAULT;1.1;AES256
30376338613338326663373366303234623665633339303338613463313564633832363237306137
3362643039616631323339383332306536333962346133310a383265376665316235653261616136
61306566623531356263346439633761633830323636646236373736353530396134636536666532
3939636433636364310a316639366139366566623337623536346661633339343766323936346336
65333035366635396138656132643262626438333961326266396466626464643766
aws_secret_access_key: !vault |
$ANSIBLE_VAULT;1.1;AES256
65643230613939303737336632346432393234616437383532386139616364316233333933643735
6633636261383163323362623562323333323533336564310a323030343431366135343035326635
33623161376634643939636464306139386662303034616531346632303039643238373834616266
3064623232373233610a346432646565396235316631626137653731376365333531323866626665
62656638623330643539653763636364363738653932653831316238633939356462653636633463
6130613761633565616533633332376565373062396565396261
config:
- profile AmazonCloudWatchAgent:
region: "eu-west-1"
- role: christiangda.amazon_cloudwatch_agent # 使用在线 YAML 配置为您的代理
vars:
cwa_agent_mode: onPremise
cwa_aws_region: "eu-west-1"
cwa_profile: "AmazonCloudWatchAgent"
cwa_conf_json_file_content:
agent:
metrics_collection_interval: 60
region: es-west-1
logfile: "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
debug: false
metrics:
metrics_collected:
cpu:
resources:
- "*"
measurement:
- name: cpu_usage_idle
rename: CPU_USAGE_IDLE
unit: Percent
- name: cpu_usage_nice
unit: Percent
- cpu_usage_guest
totalcpu: false
metrics_collection_interval: 10
append_dimensions:
test: test1
date: "2017-10-01"
netstat:
measurement:
- tcp_established
- tcp_syn_sent
- tcp_close
metrics_collection_interval: 60
processes:
measurement:
- running
- sleeping
- dead
append_dimensions:
ImageId: "${aws:ImageId}"
InstanceId: "${aws:InstanceId}"
InstanceType: "${aws:InstanceType}"
AutoScalingGroupName: "${aws:AutoScalingGroupName}"
aggregation_dimensions:
- - AutoScalingGroupName
- - InstanceId
- InstanceType
- []
logs:
logs_collected:
files:
collect_list:
- file_path: "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
log_group_name: amazon-cloudwatch-agent.log
log_stream_name: amazon-cloudwatch-agent.log
timezone: UTC
- file_path: "/opt/aws/amazon-cloudwatch-agent/logs/test.log"
log_group_name: test.log
log_stream_name: test.log
timezone: Local
log_stream_name: my_log_stream_name
force_flush_interval: 15
在 AWS EC2 实例上进行最小配置
---
- hosts: all
gather_facts: True
become: true
become_user: root
become_method: sudo
remote_user: ec2-user
roles:
- role: christiangda.amazon_cloudwatch_agent # 假设您在 "EC2 实例" 上,并使用角色提供的默认代理配置
示例清单文件(inventory)
[all]
10.14.x.y
10.14.v.z
[amazon-1]
10.14.x.y
[amazon-2]
10.14.v.z
如何使用
ansible-playbook my-playbook.yml \
--inventory inventory \
--private-key [~/location of my key.pem] \
--become \
--become-user=ec2-user \
--user ec2-user
开发 / 贡献
此角色使用 Molecule 进行测试,且使用了 Python 虚拟环境。
我们仍然使用主 Git 分支:
- master
- develop
如果您想为此项目做出贡献,请执行以下步骤:
参考文献:
准备您的环境
- Python 3
mkdir ansible-roles
cd ansible-roles/
python3 -m venv venv
source venv/bin/activate
pip install pip --upgrade
pip install ansible
pip install molecule
pip install 'molecule[docker]'
pip install 'molecule[podman]'
pip install 'molecule[lint]'
pip install molecule-vagrant
pip install python-vagrant
pip install selinux
pip install docker
pip install pytest
pip install pytest-mock
pip install pylint
pip install rope
pip install autopep8
pip install yamllint
pip install flake8
pip install ansible-lint
克隆角色存储库(从您的分叉)并创建符号链接
注意:首先分叉主存储库。
git clone https://github.com/<你的 GitHub 用户>/ansible-role-amazon-cloudwatch-agent.git
ln -s ansible-role-amazon-cloudwatch-agent christiangda.amazon_cloudwatch_agent
cd christiangda.amazon_cloudwatch_agent
执行分子测试
可用场景:
- default -->
--driver-name docker
- podman -->
--driver-name podman
- vagrant -->
--driver-name vagrant
默认场景
逐步执行
molecule create [--scenario-name default]
molecule converge [--scenario-name default]
molecule verify [--scenario-name default]
molecule destroy [--scenario-name default]
或全都一起执行
molecule test [--scenario-name default]
podman 场景
逐步执行
molecule create --scenario-name podman
molecule converge --scenario-name podman
molecule verify --scenario-name podman
molecule destroy --scenario-name podman
或全都一起执行
molecule test --scenario-name podman
vagrant 场景
逐步执行
molecule create --scenario-name vagrant
molecule converge --scenario-name vagrant
molecule verify --scenario-name vagrant
molecule destroy --scenario-name vagrant
或全都一起执行
molecule test --scenario-name vagrant
此外,如果您想使用虚拟机进行测试,我有一个非常好的 ansible-playground 项目,使用 Vagrant 和 VirtualBox,建议试试!
许可证
此模块根据 GNU 通用公共许可证第 3 版发布: