使用ec2标签在ansible上创建度量标准警报

dou*_*lps 4 amazon-ec2 amazon-web-services ansible

我正在尝试使用ansible脚本为每个新实例定义一个警报.使用时很容易实现:

- name: Create CPU utilization metric alarm ec2_metric_alarm: state: present name: "cpu-low" metric: "CPUUtilization" statistic: Average comparison: ">=" threshold: 80.0 unit: "Percent" period: 300 evaluation_periods: 1 description: "It will be triggered when CPU utilization is more than 80% for 5 minutes"

请注意,我使用cpu-low作为警报名称.这不是我想要的,因为我可以有多个实例触发该警报.因此,我想使用'Name'我不知道如何访问的ec2标签.

我试图使用:

- name: List resource tags local_action: ec2_tag resource=XYZ state=list tags: [metric-alarms]

但这需要我也没有的resourceID.是否可以在ansible脚本上获取ec2标签?

dou*_*lps 5

这是我想出的在警报名称上使用实例名称的任务:

- name: Get instance ec2 facts
  action: ec2_facts
  register: ec2_facts

- name: Get resource tags from ec2 facts
  sudo: false
  local_action: ec2_tag
                resource={{ec2_facts.ansible_facts.ansible_ec2_instance_id}}
                region=us-east-1 state=list
  register: result

- name: Create CPU utilization metric alarm
  sudo: false
  local_action: ec2_metric_alarm
                state=present
                region=us-east-1
                name="{{result.Name}}-cpu-utilization"
                metric="CPUUtilization"
                statistic=Average comparison=">="
                threshold=80.0
                unit="Percent"
                period=300
                evaluation_periods=1
                description="It will be triggered when CPU utilization is more than 80% for 5 minutes"
                dimensions="InstanceId"="{{ec2_facts.ansible_facts.ansible_ec2_instance_id}}"
                alarm_actions=arn:aws:sns:us-east-1:123412341234:My_SNS_Notification
                ok_actions=arn:aws:sns:us-east-1:123412341234:My_SNS_Notification
Run Code Online (Sandbox Code Playgroud)