I got some time tonight to go over the following:
tasks:
- name: Get all existing guest domains, regardless their states
action: shell …
register: all_vm_names
- name: Get all active guest domains
action: shell …
register: active_vm_names
- name: Destroy active domains one by one
action: virt name=$item command=destroy
with_items: ${active_vm_names.stdout_lines}
only_if: ${active_vm_names}
I changed the above to only_if: ${active_vm_names.stdout}
- name: Undefine all domains one by one
action: virt name=$item command=undefine
with_items: ${all_vm_names.stdout_lines}
only_if: ${all_vm_names}
ditto: only_if: ${all_vm_names.stdout}
If I understand the following statement correctly, then each with_items task would be executed only if the ${…} is True.
with_items expanding first will expand to multiple task executions,
but there is a “only_if” on each expansion.
But, I got the following by running the above with ansible-playbook -vv:
TASK: [Get all existing guest domains, regardless their states, except sl0] *********************
REMOTE_MODULE command … #USE_SHELL
changed: [barn0] => {“changed”: true, “cmd”: “…”, “delta”: “0:00:00.018831”, “end”: “2012-10-15 23:01:27.838914”, “rc”: 0, “start”: “2012-10-15 23:01:27.820083”, “stderr”: “”, “stdout”: “ctos1\nctos2”} … (1)
TASK: [Get all active guest domains, except sl0] *********************
REMOTE_MODULE command … #USE_SHELL
changed: [barn0] => {“changed”: true, “cmd”: …, “delta”: “0:00:00.020940”, “end”: “2012-10-15 23:01:27.983329”, “rc”: 0, “start”: “2012-10-15 23:01:27.962389”, “stderr”: “”, “stdout”: “”} … (2)
TASK: [Destroy them one by one] *********************
fatal: [zettar3] => Traceback (most recent call last):
File “/usr/lib/pymodules/python2.7/ansible/runner/init.py”, line 227, in _executor
exec_rc = self._executor_internal(host)
File “/usr/lib/pymodules/python2.7/ansible/runner/init.py”, line 276, in _executor_internal
return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port)
File “/usr/lib/pymodules/python2.7/ansible/runner/init.py”, line 327, in _executor_internal_inner
if not utils.check_conditional(conditional):
File “/usr/lib/pymodules/python2.7/ansible/utils.py”, line 146, in check_conditional
return eval(conditional.replace(“\n”, “\n”))
File “”, line 0
^
SyntaxError: unexpected EOF while parsing
FATAL: all hosts have already failed – aborting
shell commands in (1) and (2) above have return code (rc) 0, and thus they are constructed correctly for the shell module. The register variable all_vm_names’s stdout consists of valid entries. The active_vm_names’s stdout is empty, so the task named Destroy active domains one by one shouldn’t execute. But before it gets to that, the core parsing code errored out. Any other suggestion please?
Though to be honest it seems quite easier to give the virt module a
command for ‘undefine_all’. That’s what I’d do. (Send me a patch?)
I gave this aspect some more deliberation, and came to the conclusion that a undefine_all is not enough. At least based on our usage, from time to time, we may wish to preserve a few VMs. But so far I don’t have a clean syntax for a list of exceptions. I don’t wish to introduce that pollute ansible’s current overall clean syntax. I will think more…
Regards,
– Zack