Ansible - 使用块模块

时间:2018-05-28 09:27:13

标签: ansible

我开始使用Ansible和AWS。 我创建了一个启动新实例的playbook,后来应该将现有卷附加到新实例:

- 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 }}"

到目前为止,这么好,一切正常。 我想添加一个清理方法,所以如果以后的模块中有任何类型的错误,我将不会有任何垃圾实例。 我知道这里的想法是使用block模块,但是当我尝试使用块时,没有真正发生的事情:

---
# 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 }}"

1 个答案:

答案 0 :(得分:3)

您将debug任务放入块中。 debug模块返回ok状态,因此:

  1. 它不会调用处理程序(这需要changed状态),
  2. 1. rescue - 部分永远不会被触发(这需要failed状态)。

    因此,预计“没有真正发生”。

    您需要将实际任务放入块中(假设它们是正确的,我保持原样):

    - 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