简体   繁体   English

使用ec2_ami和Ansible创建AMI

[英]Creating AMI using ec2_ami with Ansible

I am trying to create an AMI from an EC2. 我正在尝试从EC2创建AMI。 However, before doing so I would like to check if the AMI with the same name exists. 但是,在这样做之前,我想检查是否存在具有相同名称的AMI。 If it does, I would like to deregister it before attempting to create the AMI with the given name. 如果是这样,我想先注销它,然后再尝试使用给定名称创建AMI。

Issue1: How do I run AMI deregister ONLY if the AMI already exists. 问题1:仅在AMI已经存在的情况下,才应运行AMI注销。 Issue2: When the deregister call has been madem, how do I wait for before creating the AMI with the same name? 问题2:进行取消注册调用后,如何等待创建具有相同名称的AMI?

Here is what I have so far 这是我到目前为止的

- name: Check if AMI with the same name exists
  ec2_ami_find:
    name: "{{ ami_name }}"
  register: ami_find

- name: Deregister AMI if it exists
  ec2_ami:
    image_id: "{{ ami_find.results[0].ami_id }}"
    state: absent
  when: ami_find.results[0].state == 'available'

- pause:
    minutes: 5

- name: Creating the AMI from of the instance
  ec2_ami:
    instance_id: "{{ item.id }}"
    wait: yes
    name: "{{ ami_name }}"
  delegate_to: 127.0.0.1
  with_items: "{{ ec2.instances }}"
  register: image

EDIT: I am able to deregister the AMI when the state is 'available' and wait for a few minutes before attempting to create the new AMI (which has the same name). 编辑:当状态为“可用”时,我可以注销AMI,并等待几分钟,然后再尝试创建新的AMI(具有相同的名称)。 However, sometimes I get the following response. 但是,有时我会收到以下响应。 In which case I would like to continue with creating AMI. 在这种情况下,我想继续创建AMI。

TASK [createAMI : Check if AMI with the same name exists] **********************
ok: [local] => {"changed": false, "results": []}

首先检查结果是否为空,然后检查状态。

when: ami_find.results | length and ami_find.results[0].state == 'available'

Thanks to the comment above, I managed to add the following to the Deregister task and managed to deal with the empty response. 感谢上面的评论,我设法将以下内容添加到了Deregister任务中并设法处理了空响应。

- name: Check if AMI with the same name exists
  ec2_ami_find:
    name: "{{ ami_name }}"
  register: ami_find

- name: Deregister AMI if it exists
  ec2_ami:
    image_id: "{{ ami_find.results[0].ami_id }}"
    state: absent
  when: ami_find.results | length and ami_find.results[0].state == 'available'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM