1

我试图在ec2中使用安全策略设置自动扩展策略。每个自动比例组将具有多个策略(IE向上扩展和向下扩展),并且每个策略可能有多个CloudWatch警报(CPU使用率,负载平衡器延迟)。配合EC2自动扩展策略和告警

我已经得到了瓦尔的缩放策略和报警,如果在.yml文件中的单个自动缩放组和我试图建立与剧本的政策:

- ec2_scaling_policy: 
    state: present 
    region: "{{ region }}" 
    adjustment_type: "ChangeInCapacity" 
    asg_name: "{{ asg_name }}" 
    scaling_adjustment: "{{ item.scaling_adjustment }}" 
    min_adjustment_step: 1 
    cooldown: "{{ item.cooldown }}" 
    name: "{{item.name }}" 
    register: "sp_result" 
    with_items: scaling_policies 

- debug: msg="{{sp_result}}" 

- debug: 
    msg="{{item[1]['name']}} == {{item[0]['scaling_policy_name']}}" 
    with_nested: 
    - alarm_metrics 
    - sp_result.results 

- ec2_metric_alarm: 
    state: present 
    region: "{{ region }}" 
    name: "{{ item[0].name }}" 
    metric: "{{ item[0].metric }}" 
    namespace: "AWS/EC2" 
    statistic: "{{ item[0].statistics }}" 
    comparison: "{{ item[0].comparison }}" 
    threshold: "{{ item[0].threshold }}" 
    period: "{{ item[0].period }}" 
    evaluation_periods: "{{ item[0].evaluation_periods }}" 
    unit: "{{ item[0].unit }}" 
    description: "{{ item[0].description }}" 
    dimensions: "{{ item[0].dimensions }}" 
    alarm_actions: "{{ item[1]['arn'] }}" 
    when: "{{item[1]['name']}} == {{item[0]['scaling_policy_name']}}" 
    with_nested: 
    - alarm_metrics 
    - sp_result.results 

这是最接近我”但我仍然得到错误:msg: unsupported parameter for module: when

此外,项目[0],项目[1]和迭代嵌套循环越来越杂乱是否有更好的方式做到这一点?如果不是,我如何解决我得到的错误?

编辑补充瓦尔文件:

我们集团之一的瓦尔文件的一个示例:

asg_name: autoscale-group-prod 
region: us-east-1 
scaling_policies: 
    - scaling_adjustment: 1 
    name: policy-high-cpu 
    cooldown: 300 

    - scaling_adjustment: -1 
    name: policy-low-cpu 
    cooldown: 300 


alarm_metrics: 
    - name: group-high-cpu-alarm 
    metric: "CPUUtilization" 
    statistics: Average 
    comparison: ">=" 
    threshold: "85" 
    period: 300 
    evaluation_periods: 2 
    unit: "Percent" 
    description: "alerm when CPU utilization is >= 85% for 10 minutes." 
    dimensions: {"AutoScalingGroupName": 'autoscale-chameleon-prod'} 
    scaling_policy_name: policy-high-cpu 

    - name: group-healthy-host-alarm 
    metric: "HealthyHostCount" 
    statistics: Average 
    comparison: "<" 
    threshold: "1" 
    period: 300 
    evaluation_periods: 3 
    unit: "Count" 
    description: "alarm when there are no healthy instances behind the elb" 
    dimensions: {"ElasticLoadBalancerName": "GroupELB"} 
    scaling_policy_name: policy-high-cpu 

    - name: group-low-cpu-alarm 
    metric: "CPUUtilization" 
    statistics: Average 
    comparison: "<" 
    threshold: "50" 
    period: 300 
    evaluation_periods: 2 
    unit: "Percent" 
    description: "alerm when CPU utilization is < 50% for 10 minutes." 
    dimensions: {"AutoScalingGroupName": 'autoscale-chameleon-prod'} 
    scaling_policy_name: policy-low-cpu 
+0

对不起,没有什么可以做的..我误读你的'with_nested'作为'with_together'并且有一些希望。对于嵌套循环,你必须忍受'item [0]'和item [1 'AFAIK。 – Kashyap

回答

2

错误压痕&引用。

- ec2_metric_alarm: 
    state: present 
    region: "{{ region }}" 
    name: "{{ item[0].name }}" 
    metric: "{{ item[0].metric }}" 
    namespace: "AWS/EC2" 
    statistic: "{{ item[0].statistics }}" 
    comparison: "{{ item[0].comparison }}" 
    threshold: "{{ item[0].threshold }}" 
    period: "{{ item[0].period }}" 
    evaluation_periods: "{{ item[0].evaluation_periods }}" 
    unit: "{{ item[0].unit }}" 
    description: "{{ item[0].description }}" 
    dimensions: "{{ item[0].dimensions }}" 
    alarm_actions: "{{ item[1]['arn'] }}" 
    # Wrong: 
    # when: "{{item[1]['name']}} == {{item[0]['scaling_policy_name']}}" 
    # Correct: 
    when: "'{{item[1].name}}' == '{{item[0].scaling_policy_name}}'" 
    with_nested: 
    - alarm_metrics 
    - sp_result.results 
+0

你是正确的,只是给了我一个不同的错误:'在评估条件时出错:policy-high-cpu == policy-high-cpu'现在看看它,它显然更接近但仍然不正确。 – Jimr

+0

@Jimr引用问题。答案用正确的更新。 Abt另一个Q,发布你正在迭代的数据结构。 – Kashyap