Jimr
Jimr

Reputation: 258

Configuring EC2 autoscale policies and alarms with ansible

I'm trying to setup a our auto-scaling policies in ec2 using ansible. Each auto-scale group will have multiple policies (IE scale-up and scale-down), and could have multiple cloudwatch alarms per policy (CPU usage, load balancer latency).

I've got the vars for the scaling policies and alarms if a single autoscale group in a .yml file and am trying to build the policies with the playbook:

- 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

This is the closest I've come to making it work but I'm still getting the error: msg: unsupported parameter for module: when

Additionally, the item[0], item[1] and iterating over nested loops is getting messy is there a better way of doing this? If not how do I solve the error I'm getting?

Edit to add vars file:

An example of the vars file for one of our groups:

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

Upvotes: 2

Views: 1441

Answers (1)

Kashyap
Kashyap

Reputation: 17546

Wrong indentation & quoting.

- 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

Upvotes: 3

Related Questions