1

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!

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Nolan_1981
  • 23
  • 3
  • 1
    `How can I put a single item of my list in parameter name?` => by using the very exact same loop you have in your debug task right on top of it. – Zeitounator Feb 17 '21 at 11:31
  • Thank you, please can you help me with the syntax? I can't understand how can I use the loop inside the parameter name – Nolan_1981 Feb 18 '21 at 07:32

1 Answers1

0

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 }}"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66