1

我是 Ansible 新手,我需要通过 ovirt_vm 模块与 Ovirt Manager 交互。我的目的是多次关闭存储在 .yml 文件中的虚拟机列表。

这是我的剧本:

---
- name: Manage VMs on ovirt-Manager
  hosts: MyHost
  connection: local

  vars_files:
    - ovirt_vars.yml
    #FQDN and credentials
    - ovirt-vms.yml
    #List of VMs

  vars:
    vm: "{{ VMs }}"

  pre_tasks:
    - name: Login to ovirt-M
      ovirt_auth:
        hostname: "{{ ovirt_fqdn }}"
        username: "{{ ovirt_user }}"
        password: "{{ ovirt_password }}"
      tags:
        - always

  tasks:
    - name: Show List of VMs from ovirt-vms.yml
      debug:
         msg: "{{ item }}"
      with_items:
         "{{ vm }}"

问题出在这个任务中

    - name: Shutdown multiple VMs
      ovirt_vm:
        auth: "{{ ovirt_auth }}"
        cluster: CL_OVIRT
        state: stopped
        name: "{{ vm |string }}" << HERE

如何将列表中的单个项目放入参数名称中?例如 VM01,VM02,...VM10


  post_tasks:
    - name: Logout from ovirt-M
      ovirt_auth:
        state: absent
        ovirt_auth: "{{ ovirt_auth }}"
      tags:
         - always

非常感谢你的帮助!

4

1 回答 1

0

正如我的评论中所述,只需使用您在前面的任务中使用的相同循环机制:debug

    - name: Shutdown multiple VMs
      ovirt_vm:
        auth: "{{ ovirt_auth }}"
        cluster: CL_OVIRT
        state: stopped
        name: "{{ item }}"
      with_items: "{{ vm }}"
      # alternatively, use the new loop syntax. See doc link above
      # loop: "{{ vm }}"
于 2021-02-18T12:39:26.833 回答