ansible - How to conditionally check the state of an openstack instance -
i trying conditionally check state of openstack cloud instance in playbook.
the playbook takes name of cloud instance parameter , deletes setting state absent using nova compute. want check if state absent (say name of non-existent instance has been entered) skip statement. how write that?
- nova_compute name: "{{item}}" state: absent with_items: "{{instance_name}}" when: ???
i'm not familiar openstack tasks, looks shouldn't terribly difficult do. first off, if want terminate instances , you're getting error because don't exist ignoring errors might suffice:
- nova_compute name: "{{item}}" state: absent with_items: "{{instance_name}}" ignore_errors: yes
if don't want you'd need split out multiple tasks, 1 check state of instance(s) , 1 terminate them.
the ansible documentation looks it's been updated ansible 2.0, i'm not sure if module names have changed sufficiently or not, given what's documented i'd suggest using task gather facts instance in question. if task returns error instance doesn't exist, you're after, should work:
- os_server_facts name: "{{instance_name}}" register: instance_state ignore_errors: yes
and can this:
- nova_compute name: "{{instance_name}}" state: absent when: instance_state not defined
depending on first task returns instance_state
might want when
clause bit more structured. i'd suggest running few tests , outputting instance_state
via debug module see if need beyond provided here.
if need list of instances should able expand tasks bit well. along these lines (obviously haven't tested these may not 100% correct):
- 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] not defined with_items: "{{instance_list}}"
Comments
Post a Comment