christiangda.amazon_cloudwatch_agent
Ansible Rolle: christiangda.amazon_cloudwatch_agent
Diese Rolle installiert den AWS CloudWatch-Agenten.
Der beste Weg, diese Rolle zu installieren, ist der Befehl ansible-galaxy install christiangda.amazon_cloudwatch_agent
, das Ansible Galaxy-Repository ist christiangda.amazon_cloudwatch_agent.
Der Code des Repositories befindet sich unter https://github.com/christiangda/ansible-role-amazon-cloudwatch-agent.
Funktionen:
- Funktioniert auf AWS EC2-Instanzen/On-Premise-Instanzen
- Unterstützt verschiedene Versionen von Python, Ansible und Betriebssystemen (siehe die Matriz zur kontinuierlichen Integration für Details)
- Lädt den CloudWatch-Agenten aus dem AWS-Verteilungspaket herunter und installiert ihn
- Lädt den Dienst neu, wenn die Konfiguration geändert wird
- Bietet eine Standard-Agent-Konfigurationsdatei (eine minimale Konfiguration, nicht empfohlen)
- Dreht die CloudWatch-Agent-Protokolldatei
- Erlaubt Ihnen, Ihre eigene JSON-Datei / YAML-Datei oder INLINE-Konfiguration für Agent-, Metriken- und Protokollbereich zu laden
- Unterstützt
amd64
undarm64
Architekturen
Hinweise:
- Die Version 2.x.y ist nicht mit der Version 1.x.y kompatibel.
- Seit Version 2.x.y installiert diese Rolle standardmäßig nicht mehr das EPEL-Repository, Sie müssen sich jetzt selbst darum kümmern. Ich empfehle meine
ansible role
christiangda.epel_repo. - Seit Version 2.x.y erstellt diese Rolle nicht mehr AWS CLI-Profile (Konfiguration und Anmeldeinformationen), Sie müssen sich jetzt selbst darum kümmern. Ich empfehle meine
ansible roles
christiangda.awscli und christiangda.awscli_configure stattdessen. Siehe Beispiele unter (wenn cwa_agent_mode: "onPremise"). - Installiert automatisch das
collectd OS-Paket
, wenn die Verwendung von Custom Metrics with collectd in der Konfigurationsdatei erkannt wird. Je nachOS
könnte dascollectd-Paket
das EPEL-Repository benötigen. - RedHat-Benutzer: Es ist wichtig, ein gültiges Abonnement zu haben, um Probleme mit der Installation von Abhängigkeitspaketen zu vermeiden.
Weitere Details in VERSION.md.
Anforderungen
Diese Rolle funktioniert auf RedHat-, CentOS-, Amazon Linux-, Debian- und Ubuntu-Distributionen.
- 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)
Um die Kompatibilitätsmatrix von Python- und Ansible-Versionen zu sehen, siehe Travis-CI-Bau-Matrix.
Rollenvariablen
Variable | Standardwert |
---|---|
cwa_conf_json_file_content | "" --> Leer |
cwa_agent_mode | "ec2" |
cwa_aws_region | "" |
cwa_use_credentials | false |
cwa_profile | "AmazonCloudWatchAgent" |
cwa_agent_profile_path | /root |
cwa_http_proxy | "" --> Leer |
cwa_https_proxy | "" --> Leer |
cwa_no_proxy | "169.254.169.254" |
cwa_logrotate_file_size | "10M" |
cwa_logrotate_files | 5 |
cwa_debug | false |
Weitere Details: Siehe die Datei defaults/main.yaml.
Abhängigkeiten
- Im Falle von RedHat/Centos OS-Familie könnte das EPEL Repository notwendig sein.
- Wenn Sie
cwa_agent_mode: "onPremise"
setzen, ist die AWS CLI-Profil Konfiguration erforderlich. - Wenn Sie Custom Metrics with collectd abrufen wollen, wird das Paket
collectd
automatisch installiert und je nachOS
könnte das EPEL Repository erforderlich sein.
Beispiel-Playbook
Wenn cwa_agent_mode: "onPremise"
# Sie sollten ansible-vault verwenden, um es bereitzustellen
# Beispiel:
# 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 # Wenn Sie das EPEL-Repository nicht installiert haben
when: >
ansible_os_family == 'RedHat' and (
ansible_distribution == 'CentOS' or
ansible_distribution == 'RedHat' or
ansible_distribution == 'Amazon'
)
- role: christiangda.awscli_configure # Wenn Sie die AWS CLI-Profile nicht konfiguriert haben
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 # Verwendung der minimalen Agent-Konfiguration, die von der Rolle bereitgestellt wird
vars:
cwa_agent_mode: "onPremise"
cwa_aws_region: "eu-west-1"
cwa_profile: "AmazonCloudWatchAgent"
Wenn cwa_agent_mode: "ec2"
Lesen der Konfigurationsdatei aus der JSON-Konfigurationsdatei
---
- hosts: servers
gather_facts: True
roles:
- role: christiangda.amazon_cloudwatch_agent # Verwendung einer JSON-Datei, die Sie an dem Ort `files/CloudWatch.json` relativ zu diesem Playbook bereitstellen
vars:
cwa_agent_mode: "ec2"
cwa_conf_json_file_content: "{{ lookup('file', 'files/CloudWatch.json') | from_json }}"
---
- hosts: servers
gather_facts: True
roles:
- role: christiangda.epel_repo
when: >
ansible_os_family == 'RedHat' and (
ansible_distribution == 'CentOS' or
ansible_distribution == 'RedHat' or
ansible_distribution == 'Amazon'
)
- role: christiangda.awscli_configure
vars:
awscliconf_path: '/root'
awscliconf_files:
credentials:
- AmazonCloudWatchAgent:
aws_access_key_id: 'AKIAIOSFODNN7EXAMPLE'
aws_secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
- role: christiangda.amazon_cloudwatch_agent # Verwendung der minimalen Agent-Konfiguration, die von der Rolle bereitgestellt wird
vars:
cwa_agent_mode: "ec2"
Lesen der Konfigurationsdatei aus der YAML-Konfigurationsdatei
---
- hosts: servers
gather_facts: True
roles:
- role: christiangda.amazon_cloudwatch_agent # Verwendung einer YAML-Datei, die Sie an dem Ort `files/CloudWatch.yaml` relativ zu diesem Playbook bereitstellen
vars:
cwa_agent_mode: "ec2"
cwa_conf_json_file_content: "{{ lookup('file', 'files/CloudWatch.yaml') | from_yaml }}"
Verwendung einer INLINE-YAML-Konfigurationsdatei
---
- hosts: centos7, centos6, ubuntu1804, ubuntu1810, debian8, debian9, amzn2
become: True
roles:
- role: christiangda.epel_repo # Wenn Sie das EPEL-Repository nicht installiert haben
when: >
ansible_os_family == 'RedHat' and (
ansible_distribution == 'CentOS' or
ansible_distribution == 'RedHat' or
ansible_distribution == 'Amazon'
)
- role: christiangda.awscli_configure # Wenn Sie die AWS CLI-Profile nicht konfiguriert haben
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 # Verwendung einer ONLINE-YAML-Konfiguration für Ihren Agenten
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
Minimale Konfiguration auf einer AWS EC2-Instanz
---
- hosts: all
gather_facts: True
become: true
become_user: root
become_method: sudo
remote_user: ec2-user
roles:
- role: christiangda.amazon_cloudwatch_agent # Annahme, dass Sie sich auf "EC2-Instanz" befinden und die Standard-Agent-Konfiguration von der Rolle verwendet wird
Beispiel einer Inventar-Datei (inventory)
[all]
10.14.x.y
10.14.v.z
[amazon-1]
10.14.x.y
[amazon-2]
10.14.v.z
So verwenden Sie es
ansible-playbook my-playbook.yml \
--inventory inventory \
--private-key [~/standort meines schlüssels.pem] \
--become \
--become-user=ec2-user \
--user ec2-user
Entwicklung / Mitwirken
Diese Rolle wird mit Molecule getestet und wurde mit Python Virtual Environments entwickelt.
Außerdem verwenden wir die Haupt-Git-Zweige
- master
- develop
Wenn Sie zu diesem Projekt beitragen möchten, sollten Sie Folgendes tun:
- Forken Sie das Projekt
- Bereiten Sie Ihre Umgebung vor
- Beheben Sie das Problem im
develop
Zweig - Führen Sie
molecule test
aus - Erstellen Sie einen Pull-Request für den offiziellen Projekting
develop
Zweig
Referenzen:
Bereiten Sie Ihre Umgebung vor
- 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
Klonen Sie das Rollerepository (von Ihrem Fork) und erstellen Sie einen symbolischen Link
HINWEIS: Zuerst das Haupt-Repository forschen.
git clone https://github.com/<your github user>/ansible-role-amazon-cloudwatch-agent.git
ln -s ansible-role-amazon-cloudwatch-agent christiangda.amazon_cloudwatch_agent
cd christiangda.amazon_cloudwatch_agent
Führen Sie den Molecule-Test aus
Verfügbare Szenarien:
- default -->
--driver-name docker
- podman -->
--driver-name podman
- vagrant -->
--driver-name vagrant
Szenario standard
Schritt für Schritt
molecule create [--scenario-name default]
molecule converge [--scenario-name default]
molecule verify [--scenario-name default]
molecule destroy [--scenario-name default]
oder
Alles in einem
molecule test [--scenario-name default]
Szenario podman
Schritt für Schritt
molecule create --scenario-name podman
molecule converge --scenario-name podman
molecule verify --scenario-name podman
molecule destroy --scenario-name podman
oder
Alles in einem
molecule test --scenario-name podman
Szenario vagrant
Schritt für Schritt
molecule create --scenario-name vagrant
molecule converge --scenario-name vagrant
molecule verify --scenario-name vagrant
molecule destroy --scenario-name vagrant
oder
Alles in einem
molecule test --scenario-name vagrant
Zusätzlich, wenn Sie es mit VMs testen möchten, habe ich ein sehr nettes ansible-playground-Projekt, das Vagrant und VirtualBox verwendet, probieren Sie es aus!
Lizenz
Dieses Modul wird unter der GNU General Public License Version 3 veröffentlicht:
Autoreninformationen
Ansible Role for AWS CloudWatch Agent
ansible-galaxy install christiangda.amazon_cloudwatch_agent