linuxansibleansible-2.xproxmox

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


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.


Solution

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