使用Ansible禁用EC2实例终止保护

mju*_*eri 2 amazon-ec2 ansible

我目前正在启用termination_protection使用Ansible创建的所有实例,以防止从控制台等意外终止。现在,我希望能够使用Ansible终止特定的实例,但我不知道如何在其上禁用终止保护。

我认为这可以解决问题:

- name: Disable termination protection
  ec2:
    instance_ids: "{{ instance_ids }}"
    region: "{{ aws_region }}"
    termination_protection: no
Run Code Online (Sandbox Code Playgroud)

运行该错误消息的方式:

fatal: [localhost]: FAILED! => {
  "changed": false, 
  "failed": true, 
  "msg": "image parameter is required for new instance"
}
Run Code Online (Sandbox Code Playgroud)

看起来Ansible会将我的脚本解释为实例创建请求。

有没有办法改变termination protection另一个模块?我唯一想到的另一种方法是通过Ansible中的一项任务使用aws clishell,但这有点麻烦。

Kon*_*rov 5

让我们看一下源代码

runningstopped电话startstop_instances()
国家restarted呼吁restart_instances()
这两个函数都支持source_dest_checktermination_protection赋予值。

因此,您可以致电:

 - ec2:
     instance_ids: "{{ instance_ids }}"
     state: restarted
     region: "{{ aws_region }}"
     termination_protection: no
Run Code Online (Sandbox Code Playgroud)

如果您不介意重新启动服务器。

或使用状态作为参数来查询当前状态ec2_remote_facts并调用ec2模块–这将更改termination_protection,但不会影响实例的状态。