简体   繁体   English

如何有条件地检查openstack实例的状态

[英]How to conditionally check the state of an openstack instance

I am trying to conditionally check the state of an openstack cloud instance in my playbook. 我正在尝试有条件地检查剧本中openstack云实例的状态。

The playbook takes the name of a cloud instance as a parameter and then deletes it by setting the state to absent using nova compute. 该剧本将云实例的名称作为参数,然后通过使用nova computing将状态设置为不存在来删除它。 What I want to do is check if the state is already absent (say the name of a non-existent instance has been entered) to skip the statement. 我想做的是检查状态是否已经不存在(例如已经输入了不存在的实例的名称)以跳过该语句。 How would I write that? 我该怎么写?

- nova_compute
     name: "{{item}}"
     state: absent
  with_items: "{{instance_name}}"
  when: ???

I'm not familiar with the openstack tasks, but it looks like this shouldn't be terribly difficult to do. 我不熟悉openstack任务,但是看起来这并不难做到。 First off, if all you want to do is terminate all your instances and you're getting an error because some already don't exist then simply ignoring errors might suffice: 首先,如果您要做的就是终止所有实例,并且由于某些不存在而收到错误,则只需忽略错误就可以了:

- nova_compute
     name: "{{item}}"
     state: absent
  with_items: "{{instance_name}}"
  ignore_errors: yes

If you don't want to do that then you'd need to split this out into multiple tasks, one to check the state of the instance(s) and another one to terminate them. 如果您不想这样做,则需要将其拆分为多个任务,一个任务检查实例的状态,另一个任务终止实例。

The Ansible documentation looks like it's recently been updated for Ansible 2.0, so I'm not sure if the module names have changed sufficiently or not, but given what's currently documented I'd suggest using a task to gather facts for the instance in question. Ansible文档看起来像它是最近为Ansible 2.0更新的,所以我不确定模块名称是否已足够更改,但是鉴于当前已记录的内容,我建议使用任务来收集有关实例的事实 If the task returns an error then the instance doesn't exist, which is what you're really after, so something like this should work: 如果任务返回错误,则说明该实例不存在,这就是您真正要执行的操作,因此应执行以下操作:

- os_server_facts
      name: "{{instance_name}}"
      register: instance_state
      ignore_errors: yes

And then you can do something like this: 然后您可以执行以下操作:

- nova_compute
      name: "{{instance_name}}"
      state: absent
      when: instance_state is not defined 

Depending on what the first task returns for instance_state you might want the when clause to be a bit more structured. 根据第一个任务为instance_state返回的内容,您可能希望when子句更加结构化。 I'd suggest running a few tests and outputting instance_state via the debug module to see if you need to do anything beyond what I provided here. 我建议运行一些测试,并通过调试模块输出instance_state ,以查看是否需要执行我在此提供的内容之外的其他任何操作。

If you need to do this with a list of instances you should be able to expand the tasks a bit to do that as well. 如果您需要使用实例列表来执行此操作,则还应该可以扩展一些任务来执行此操作。 Something along these lines (obviously I haven't actually tested these so they may not be 100% correct): 遵循以下原则(显然我没有实际测试过这些,因此它们可能不是100%正确的):

- os_server_facts
      name: "{{item}}"
      register: instance_state
      ignore_errors: yes
      with_items: "{{instance_list}}"

- nova_compute
      name: "{{item}}"
      state: absent
      when: instance_state[item] is not defined 
      with_items: "{{instance_list}}"

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

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