Search code examples
ansibleovirt

Shutdown multiple Virtual Machines on Ovirt by Ansible module


I am an Ansible newbie and I need to interact with Ovirt Manager by ovirt_vm module. My purpose is the multiple shutting down of a list of VMs stored in .yml file.

Here my playbook:

---
- 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 }}"

The problem is in this task

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

How can I put a single item of my list in parameter name? e.g. VM01,VM02,...VM10


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

Thank you very much for the help!


Solution

  • As stated in my comment, just use the same loop mechanism you are using in your preceding debug task:

        - 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 }}"