Using the ec2 tag to create a Metric on ansible alarm

I am trying to define an alarm for each new instance that I use using irreplaceable scripts. It is easy to achieve using:

- 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"

Note that I use cpu-low as the signal name. This is not what I want, since I can have more than one instance triggering this signal. Therefore, I would like to use the ec2 tag 'Name', which I do not know how to access.

I tried to use:

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

But this requires a resourceID, which I also do not have. Can I get ec2 tags on irreplaceable scripts?

+4
source share
2

, , :

- 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
+5

EC2, register, . , ec2_tag. :

- name: Set up metric alerts
  hosts: 127.0.0.1
  connection: local

  tasks:
    - name: Create CPU utilization metric alarm
      ec2_metric_alarm:
      args:
        state: present
        name: '{{ hostvars[item].ec_tag_Name }}'
        metric: CPUUtilization
        statistic: Average
        comparison: ">="
        threshold: 80.0
        unit: Percent
        period: 300
        evaluation_periods: 1
        description: >
          Triggered when CPU utilization is more than
          80% for 5 minutes
      with_items: groups['your_targets']

, ( local_action), hostvars[item] , ec2_tag_Name .

: AFAIK Ansible. , Ansible , EC2 , ec2_tag_Foo , ansible-playbook , , ec2.py --refresh-cache . . , ec2_facts .

+1

Source: https://habr.com/ru/post/1545849/


All Articles