【问题标题】:Ansible ec2 termination task failingAnsible ec2 终止任务失败
【发布时间】:2018-06-23 23:30:20
【问题描述】:

我在 Ansible 中有以下实例创建任务:

- name: Provisioning Spot instaces
  ec2:
    assign_public_ip: no
    spot_price: "{{ ondemand4_price }}"
    spot_wait_timeout: 300
    assign_public_ip: no
    aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
    aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
    security_token: "{{ assumed_role.sts_creds.session_token }}"
    region: "{{ aws_region }}"
    image: "{{ image_instance }}"
    instance_type: "{{ large_instance }}"
    key_name: "{{ ssh_keyname }}"
    count: "{{ ninstances }}"
    state: present
    group_id: "{{ cypher_priv_sg }}"
    vpc_subnet_id: "{{ private_subnet_id }}"
    instance_profile_name: 'Cypher-Ansible'
    wait: true
    instance_tags:
      Name: Cypher-Worker
    #delete_on_termination: yes
  register: ec2
  ignore_errors: True

然后终止任务是:

- name: Terminate instances that were previously launched
  connection: local
  become: false
  ec2:
    state: 'absent'
    instance_ids: '{{ ec2.instance_ids }}'
    region: '{{ aws_region }}'
  register: TerminateWorker
  ignore_errors: True

但是,它没有终止我的 Worker 实例,而是抛出一个错误,上面写着:

TASK [Terminate instances that were previously launched] ***********************
task path: /path/to/file/Ansible/provision.yml:373
fatal: [x.y.a.202]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'ec2' is undefined\n\nThe error appears to have been in '/path/to/file/Ansible/provision.yml': line 373, column 7, but maybe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Terminate instances that were previously launched\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'ec2' is undefined"
}

这可能是什么问题?

【问题讨论】:

    标签: ubuntu amazon-ec2 ansible


    【解决方案1】:

    您的任务乍一看还不错。但是为什么你在终止时使用“连接”和“成为”标志?只是询问,因为您没有在配置任务中使用它们。

    EDIT2:您的供应和终止任务是否在同一场比赛中?如果是,您可以像这样访问已注册的“ec2”变量:

    - name: Terminate instances that were previously launched
      ec2:
        state: 'absent'
        instance_ids: '{{ item.instance_id }}'
        region: "{{ aws_region }}"
        wait: yes
        wait_timeout: 500
      with_items: "{{ ec2.instances }}"
    

    如果您的终止任务在同一 playbook 运行的另一个 play 中,您必须使用 set_fact 任务使其可用于其他 play。

    如果您的终止任务将在完全不同的 playbook 运行中执行,您可以使用 ec2_instance_facts 找到您的实例 ID,如下所示:

    - name: get ec2 instance id by its name tag
      ec2_instance_facts:
        filters:
          "tag:ec2_instance_name": "{{ ecs_instance_name }}"
          instance-state-name: running
      register: instances
    

    使用此方法,您必须通过配置任务设置上述标签。

    【讨论】:

    • 谢谢。有效。顺便说一句,item.id 而不是item.instance_id 可以访问实例ID
    【解决方案2】:

    您需要在该任务中指定 ec2 var。 你可以加: with_items: {{ ec2 }} 在您的终止任务结束时,它将从上述任务中注册的 var 中获取它。

    【讨论】:

    • 不工作。请问可以添加代码示例吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多