linux 错误是:'dict object'没有属性-当我在proxmox中运行ansible playbook创建VM时

5kgi1eie  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(107)

我正在创建一个Ansible Playbook来在Proxmox服务器中创建VM。我需要在proxmox中创建2个虚拟机(VM 1和VM 2),其属性存储在另一个文件“list.yaml”中,一些默认值存储在“defaults.yaml”中。所以我使用循环条件来创建2个VMS,但我得到一个错误,“错误是:“dict object”没有属性“”(下面提到了完整错误。
main Playbook.yaml

---
 2 - name: 'Creating VMs in Proxmox'
 3   hosts: all
 4   vars:
 5     list: '/vars/list.yaml'
 6   tasks:
 7     - name: 'including defualts'
 8       include_vars: /vars/defaults.yaml
 9     - name: 'including list'
10       include_vars: /vars/list.yaml
11     - name: 'Creating VMs'
12       community.general.proxmox_kvm:
13         api_user: "{{ user }}"
14         api_password: "{{ passwd }}"
15         api_host: "{{ host }}"
16         node: "{{ node_name }}"
17         vmid: "{{ vms.vm_id }}"
18         name: "{{ vms.vm_name }}"
19         vm_type: qemu
20         ostype: l26
21         disks:
22           - size: "{{ vms.vm_storage }}"
23             type: sata
24             storage: local-lvm
25         bootdisk: sata
26         cpu: "{{ vms.vm_cores }}"
27         sockets: "{{ vms.vm_sockets }}"
28         cpuunits: 1000
29         cores: 1
30         ballon: "{{ vms.vm_memory }}"
31         netif: '{"net0":"name=virtio,ip=dhcp,ip6=dhcp,bridge=vmbr1,rate=200"}'
32         localtime: true
33         state: present
34       with_items: "{{ list }}"

字符串
defaults.yaml

---
user: "root@pam"
passwd: "root"
host: "pve.localdomain"
node_name: "pve1"


list.yaml

vms:
  vm1:
    vm_id: "101"
    vm_name: "vm1"
    vm_cores: "2"
    vm_sockets: "1"
    vm_memory: "2048"
    vm_storage: "20G"
  vm2:
    vm_id: "102"
    vm_name: "vm2"
    vm_cores: "2"
    vm_sockets: "1"
    vm_memory: "2048"
    vm_storage: "20G"


空运行剧本时出错:
任务[正在创建VM] *[localhost]:失败!=> {“msg”:“任务包含一个带有未定义变量的选项。错误是:'dict object'没有属性'vm_id'\n\n错误似乎在'/root/ansible/playbooks/array/playbook. yaml'中:第16行,第7列,但可能\n在文件的其他地方,具体取决于确切的语法问题。\n\n有问题的行似乎是:\n\n\n - name:'正在创建VM'\n ^此处\n”}
播放重播 * 本地主机:ok=3已更改=0不可达=0失败=1已跳过=0已挽救=0已忽略=0
任何人都能搞清楚代码有什么问题让我知道我该如何纠正它。

63lcw9qa

63lcw9qa1#

等你找到了

vars:
  list: '/vars/list.yaml'

字符串
这定义了一个名为list的字符串变量,其值为list.yaml文件的路径。它不会加载文件中的内容。
为此,您可以将字典更改为列表,以便您的list.yaml看起来像这样:

vms:
  - vm_id: "101"
    vm_name: "vm1"
    vm_cores: "2"
    vm_sockets: "1"
    vm_memory: "2048"
    vm_storage: "20G"
  - vm_id: "102"
    vm_name: "vm2"
    vm_cores: "2"
    vm_sockets: "1"
    vm_memory: "2048"
    vm_storage: "20G"


你可以试试这个剧本:

- name: 'Creating VMs in Proxmox'
  hosts: localhost
  vars_files:
     - /vars/list.yaml
     - /vars/defaults.yaml
  tasks:
  - name: 'Creating VMs'
    community.general.proxmox_kvm:
      api_user: "{{ user }}"
      api_password: "{{ passwd }}"
      api_host: "{{ host }}"
      node: "{{ node_name }}"
      vmid: "{{ item.vm_id }}"
      name: "{{ item.vm_name }}"
      vm_type: qemu
      ostype: l26
      disks:
        - size: "{{ item.vm_storage }}"
          type: sata
          storage: local-lvm
      bootdisk: sata
      cpu: "{{ item.vm_cores }}"
      sockets: "{{ item.vm_sockets }}"
      cpuunits: 1000
      cores: 1
      ballon: "{{ item.vm_memory }}"
      netif: '{"net0":"name=virtio,ip=dhcp,ip6=dhcp,bridge=vmbr1,rate=200"}'
      localtime: true
      state: present
    loop: "{{ vms }}"

相关问题