2016-04-14 56 views
3

因此,我試圖使用ansible ec2_metric_alarm任務來創建一個監視我的自動縮放組的雲監視警報,並且如果ASG的CPU使用率高於或低於某個點。ansible ec2_metric_alarm不會附加到自動縮放策略

- 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語法或將我的自動縮放策略名稱轉換爲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"} 

回答

4

請仔細閱讀本鏈接這也解釋了Amazon Resource Name. 下面是提供的鏈接的摘錄。

亞馬遜資源名稱(ARN)唯一標識AWS資源。當您需要在整個AWS中明確指定資源時(例如IAM策略,Amazon關係數據庫服務(Amazon RDS)標記和API調用),我們需要ARN。

這裏是alarm_actions的一個例子:應該是這樣..

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

您應該創建一個縮放策略1日和使用已註冊的輸出搶你要使用的縮放政策阿爾恩。

下面是一個例子..

- 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 

現在你可以設置你的度量標準的報警使用縮放政策阿爾恩,像這樣。

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

表示感謝! –