我是一个Ansible新手,我需要通过ovirt_vm模块与Ovirt管理器交互。我的目的是多次关闭存储在。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
非常感谢你的帮助!
正如我在评论中所述,只需使用您在前面的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 }}"