Tomer Leibovich
Tomer Leibovich

Reputation: 372

Ansible - Working with block module

I'm starting out with Ansible and AWS. I've created a playbook that launch new instance and later should attach existing volume to the new instance:

- name:                     launch instance
  ec2:
    key_name:               "{{ aws_vars.key_name }}"
    group:                  "{{ aws_vars.security_group }}"
    instance_type:          "{{ aws_vars.instance_type }}"
    image:                  "{{ aws_vars.image }}"
    region:                 "{{ aws_vars.region }}"
    wait:                   yes
    count:                  1
    instance_tags:          "{{ tags }}"
    monitoring:             yes
    vpc_subnet_id:          "{{ subnetid }}"
    assign_public_ip:       yes
  register:                 destination

- name:                     Attach volumes
  ec2_vol:
    device_name:            xvdf
    instance:               "{{ destination.instances[0].instance_id }}"
    region:                 "{{ aws_vars.region }}"
    tags:                   "{{ ec2_tags }}"
    id:                     "{{ volume_id }}"
    delete_on_termination:  yes
  with_items:               "{{ destination }}"

So far, so good, everything works. I would like to add a clean-up method, so in case there will be an error of any type in the later modules, I won't have any garbage instances. I've understood that the idea here will be to use block module, however when I've tried working with block here, nothing really happens:

---
# EC2 Migrations.

- hosts:                      localhost,
  connection:                 local
  gather_facts:               no

  tasks:
  - name:                   Vars
    include_vars:
      dir:                  files
      name:                 aws_vars

  - name:                   Create instance
    block:
      - debug:
          msg:              "Launcing EC2"
        notify:
         - launch_instance
         - Cloudwatch
    rescue:
      - debug:
          msg:              "Rolling back"
        notify:             stop_instance


 handlers:
  - name:                 launch_instance
    ec2:
      key_name:           "{{ aws_vars.key_name }}"
      group:              "{{ aws_vars.security_group }}"
      instance_type:      "{{ aws_vars.instance_type }}"
      image:              "{{ aws_vars.image }}"
      region:             "{{ region }}"
      wait:               yes
      count:              1
      monitoring:         yes
      assign_public_ip:   yes
    register:             new_ec2
  - debug:                msg="{{ new_ec2.instances[0].id }}"

  - name:                 stop_instance
    ec2:
      instance_type:      "{{ aws_vars.instance_type }}"
      instance_ids:       "{{ new_ec2.instances[0].id  }}"
      state:              stopped

  - name:                 Cloudwatch
    ec2_metric_alarm:
      state:              present
      region:             "{{ aws_vars.region }}"
      name:               "{{ new_ec2.id }}-High-CPU"
      metric:             "CPUUtilization"
      namespace:          "AWS/EC2"
      statistic:          Average
      comparison:         ">="
      threshold:          "90.0"
      period:             300
      evaluation_periods: 3
      unit:               "Percent"
      description:        "Instance CPU is above 90%"
      dimensions:            "{'InstanceId': '{{ new_ec2.instances[0].id }}' }"
      alarm_actions:      "{{ aws_vars.sns_arn }}"
      ok_actions:         "{{ aws_vars.sns_arn }}"
    with_items:           "{{ new_ec2 }}"

Upvotes: 0

Views: 855

Answers (1)

techraf
techraf

Reputation: 68469

You put debug task into the block. The debug module returns ok status, so:

  1. it does not call a handler (this required changed status),

1.the rescue-section is never triggered (this requires failed status).

Thus it is expected that "nothing really happens".


You need to put your actual tasks into the block (I leave them intact, assuming they are correct):

- name: Create instance
  block:
    - name: launch_instance
      ec2:
        key_name:           "{{ aws_vars.key_name }}"
        group:              "{{ aws_vars.security_group }}"
        instance_type:      "{{ aws_vars.instance_type }}"
        image:              "{{ aws_vars.image }}"
        region:             "{{ region }}"
        wait:               yes
        count:              1
        monitoring:         yes
        assign_public_ip:   yes
      register:             new_ec2

    - debug:
        msg: "{{ new_ec2.instances[0].id }}"

    - name: Cloudwatch
      ec2_metric_alarm:
        state:              present
        region:             "{{ aws_vars.region }}"
        name:               "{{ new_ec2.id }}-High-CPU"
        metric:             "CPUUtilization"
        namespace:          "AWS/EC2"
        statistic:          Average
        comparison:         ">="
        threshold:          "90.0"
        period:             300
        evaluation_periods: 3
        unit:               "Percent"
        description:        "Instance CPU is above 90%"
        dimensions:            "{'InstanceId': '{{ new_ec2.instances[0].id }}' }"
        alarm_actions:      "{{ aws_vars.sns_arn }}"
        ok_actions:         "{{ aws_vars.sns_arn }}"
      with_items:           "{{ new_ec2 }}"

  rescue:
    - name: stop_instance
      ec2:
        instance_type:      "{{ aws_vars.instance_type }}"
        instance_ids:       "{{ new_ec2.instances[0].id  }}"
        state:              stopped

Upvotes: 3

Related Questions