gpt4 book ai didi

ubuntu - Ansible ec2 终止任务失败

转载 作者:行者123 更新时间:2023-12-04 18:37:44 27 4
gpt4 key购买 nike

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

这里可能是什么问题?

最佳答案

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

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

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

如果您的终止任务将在完全不同的剧本运行中执行,您可以使用 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

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

关于ubuntu - Ansible ec2 终止任务失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48261218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com