The error was: 'dict object' has no attribute – when I run ansible playbook for creating VM in proxmox

huangapple go评论82阅读模式
英文:

The error was: 'dict object' has no attribute - when I run ansible playbook for creating VM in proxmox

问题

主要 Playbook.yaml

---
2 - name: '在Proxmox中创建虚拟机'
3   hosts: all
4   vars:
5     list: '/vars/list.yaml'
6   tasks:
7     - name: '包含默认值'
8       include_vars: /vars/defaults.yaml
9     - name: '包含列表'
10       include_vars: /vars/list.yaml
11     - name: '创建虚拟机'
12       community.general.proxmox_kvm:
13         api_user: "{{ user }}"
14         api_password: "{{ passwd }}"
15         api_host: "{{ host }}"
16         node: "{{ node_name }}"
17         vmid: "{{ item.value.vms.vm_id }}"
18         name: "{{ item.value.vms.vm_name }}"
19         vm_type: qemu
20         ostype: l26
21         disks:
22           - size: "{{ item.value.vms.vm_storage }}"
23             type: sata
24             storage: local-lvm
25         bootdisk: sata
26         cpu: "{{ item.value.vms.vm_cores }}"
27         sockets: "{{ item.value.vms.vm_sockets }}"
28         cpuunits: 1000
29         cores: 1
30         balloon: "{{ item.value.vms.vm_memory }}"
31         netif: '{"net0":"name=virtio,ip=dhcp,ip6=dhcp,bridge=vmbr1,rate=200"}'
32         localtime: true
33         state: present
34       with_dict: "{{ 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"

在运行Playbook时,检查变量是否正确设置,并确保引用了正确的数据结构。如果还有问题,请提供更多信息以获取更多帮助。

英文:

I am creating a Ansible Playbook to create VMs in Proxmox server. I need to create 2 VMs (VM1 & VM2) in proxmox, its attributes are stored in another file "list.yaml" and some defaults values are stored in "defaults.yaml". so I used loop condition to create the 2 VMS but I m getting a error as "The error was: 'dict object' has no attribute" (full error is mentioned below.

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"

Error when i dry run the playbook:


TASK [Creating VMs] *****************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'vm_id'\n\nThe error appears to be in '/root/ansible/playbooks/array/playbook.yaml': line 16, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: 'Creating VMs'\n ^ here\n"}

PLAY RECAP **************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0


anyone could figure out, what's wrong with the coding? and let me know how can i rectify it.

答案1

得分: 0

当你执行

```yaml
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"

然后你可以尝试使用这个 playbook:

- name: '在 Proxmox 中创建虚拟机'
  hosts: localhost
  vars_files:
     - /vars/list.yaml
     - /vars/defaults.yaml
  tasks:
  - name: '创建虚拟机'
    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 }}"
英文:

When you do

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

this defines a string variable called list with the path of the list.yaml file as a value. It does not load what's in the file.

To do so you can change your dictionary to a list so that your list.yaml looks like this :

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"

Then you can try with this playbook :

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

huangapple
  • 本文由 发表于 2023年7月24日 19:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定