我想用Ansible在OpenNebula中部署一些虚拟机。比如我用command / shell模块创建 vms (因为 Ansible 中没有opennebula模块,而且我没有足够的资格来编写它):
- name: Create VMs
become_user: oneadmin
command: onevm create --name "{{ item.1.name }}"
with_items: "{{ vms }}"
但当然我需要检查是否已经创建了同名的 vm,我的解决方案如下所示:
- name: Check what VMs already created
become_user: oneadmin
ignore_errors: yes
shell: onevm list --csv | grep -q "{{ item.name }}"
register: created_vms
with_items: "{{ vms }}"
loop_control:
label: "Check if VM {{ item.name }} created"
- name: Create VMs
become_user: oneadmin
command: onevm create --name "{{ item.1.name }}"
when: item.0|failed
with_together:
- "{{ created_vms.results }}"
- "{{ vms }}"
loop_control:
label: "Create VM {{ item.1.name }}"
它本身很麻烦,但在失败时,我在 Ansible 中看到了一个麻烦的输出:
TASK [create-vms : Check what VMs already created]
************************************************
failed: [10.1.48.190] (item=Check if VM audit created) => {"changed": true, "cmd": "onevm list --csv | grep -q \"audit\"", "delta": "0:00:00.806504", "end": "2017-06-28 12:49:00.808454", "failed": true, "item": lalalalala etc.
有没有更有效的方法来解决这个问题?