【问题标题】:ansible ec2_metric_alarm will not attach to auto scaling policyansible ec2_metric_alarm 不会附加到 Auto Scaling 策略
【发布时间】:2016-04-15 03:35:17
【问题描述】:

所以我正在尝试使用 ansible ec2_metric_alarm 任务来创建一个云监视警报,以监控我的 Auto Scaling 组,如果 ASG 的 cpu 使用率高于或低于某个点,将激活 Auto Scaling 策略。

   - ec2_metric_alarm:
      aws_access_key: '{{ ami_access }}'
      aws_secret_key: '{{ ami_secret }}'
      state: present
      region: "{{regi}}"
      name: "{{item.names}}"
      metric: "CPUUtilization"
      namespace: "AWS/EC2"
      statistic: Average
      comparison: "{{item.compare}}"
      threshold: "{{item.limits}}"
      period: 60
      evaluation_periods: 1
      unit: "Percent"
      description: "{{item.desc}}"
      dimensions: {'AutoScalingGroupName':'{{auto_sc}}'}
      alarm_actions: "{{item.pol}}"
     with_items:
      - names: "cpuUP_{{auto_sc}}"
        compare: ">="
        limits: "20.0"
        desc: "This will alarm when the average cpu usage of the ASG is         greater than 20% for 1 minute"
        pol: "cpuUP_{{auto_sc}}_policy"
      - names: "cpuDown_{{auto_sc}}"
        compare: "<="
        limits: "10.0"
        desc: "This will alarm when the average cpu usage of the ASG is less than 10% for 1 minute"
        pol: "cpuDown_{{auto_sc}}_policy"

由于某种原因,我不能只使用我的自动缩放策略的字面名称(可能是“cpuDown_test3_policy”和“cpuUP_test3_policy”),我需要使用一种称为“arn 语法”的东西,因为我的错误消息一直在抱怨无效的 Arn 语法。

如何找到 arn 语法或将我的 Auto Scaling 策略名称转换为 arn 语法?

以下是我尝试按原样运行我的剧本时收到的错误消息供参考:

TASK [ec2_metric_alarm]     ********************************************************
failed: [localhost] => (item={u'pol': u'cpuUP_test3_policy', u'desc': u'This wil
l alarm when the average cpu usage of the ASG is greater than 20% for 1     minute',
 u'compare': u'>=', u'limits': u'20.0', u'names': u'cpuUP_test3'}) =>     {"failed":
 true, "item": {"compare": ">=", "desc": "This will alarm when the average     cpu u
sage of the ASG is greater than 20% for 1 minute", "limits": "20.0",     "names": "c
puUP_test3", "pol": "cpuUP_test3_policy"}, "msg": "BotoServerError: 400 Bad     Requ
est\n<ErrorResponse xmlns=\"http://monitoring.amazonaws.com/doc/2010-08-    01/\">\n
  <Error>\n    <Type>Sender</Type>\n    <Code>ValidationError</Code>\n        <Messa
ge>Invalid arn syntax: cpuUP_test3_policy</Message>\n  </Error>\n      <RequestId>d8
97c79a-01db-11e6-92d5-5fa534a149e9</RequestId>\n</ErrorResponse>\n"}
failed: [localhost] => (item={u'pol': u'cpuDown_test3_policy', u'desc':     u'This w
ill alarm when the average cpu usage of the ASG is less than 10% for 1     minute', 
u'compare': u'<=', u'limits': u'10.0', u'names': u'cpuDown_test3'}) =>     {"failed"
: true, "item": {"compare": "<=", "desc": "This will alarm when the average     cpu 
usage of the ASG is less than 10% for 1 minute", "limits": "10.0", "names":     "cpu
Down_test3", "pol": "cpuDown_test3_policy"}, "msg": "BotoServerError: 400     Bad Re
quest\n<ErrorResponse xmlns=\"http://monitoring.amazonaws.com/doc/2010-08-    01/\">
\n  <Error>\n    <Type>Sender</Type>\n    <Code>ValidationError</Code>\n        <Mes
sage>Invalid arn syntax: cpuDown_test3_policy</Message>\n  </Error>\n      <RequestI
d>d8b33ea6-01db-11e6-82db-8bfc9e3af9a2</RequestId>\n</ErrorResponse>\n"}

【问题讨论】:

    标签: amazon-web-services ansible boto


    【解决方案1】:

    请阅读此链接,其中解释了Amazon Resource Name. 以下是所提供链接的摘录。

    Amazon 资源名称 (ARN) 唯一标识 AWS 资源。当您需要在所有 AWS 中明确指定资源时,我们需要 ARN,例如在 IAM 策略、Amazon Relational Database Service (Amazon RDS) 标签和 API 调用中。

    以下是 alarm_actions: 应该是什么样子的示例..

    alarm_actions: ["arn:aws:autoscaling:region:account-id:scalingPolicy:policyid:autoScalingGroupName/groupfriendlyname:policyname/policyfriendlyname"]
    

    您应该首先创建一个扩展策略并使用注册的输出来获取您要使用的扩展策略的 arn。

    这是一个例子..

    - name: Scale Out policy
      local_action:
      module: ec2_scaling_policy
        state: present
        region: "{{ aws_region }}"
        name: "Name-ScaleOutPolicy"
        adjustment_type: "ChangeInCapacity"
        asg_name: "name_of_autoscale_group"
        scaling_adjustment: "-1"
        min_adjustment_step: "1"
        cooldown: "30"
      register: so_result
    

    现在您可以将指标警报设置为使用扩展策略 arn,如下所示。

    alarm_actions: ['{{ so_result["arn"] }}']
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2022-01-19
      • 2014-03-27
      • 2023-03-14
      • 2020-03-13
      相关资源
      最近更新 更多