Ansible loop over nested dictionary subelements – list object has no attribute

Sometimes it could be tricky in Ansible to loop over a nested key-value list. Take for example the below dictionary which includes a nested list of disks. The upper element of the .yml file is vms which includes name, folder, cpus, sockets, memory and disk.

The disk element consists of disksize and disktype. This .yml file has been created on a previous post which explains how to automatically provision VMware servers.

 vms:
 name: test1-ansible
 folder: ansible
 cpus: 1
 sockets: 1
 memory: 64
 disk:
  - disksize: 64
    disktype: thin
  - disksize: 100
    disktype: thin 

If you try to loop over this list you will probably get an error like list object has no attribute.

With the subelements command you can loop over your main list (vms) and access your nested one (disk) with a different index.

In order to retrieve the vm name you should use item.0. The nested values are placed under item.1

---
- name: test playbook 
  hosts: localhost 
  vars_files: vms.yml
  tasks:
    - name: loop over nested
      debug:
        msg: '"{{ item.1.disksize }}"  "{{ item.1.disktype }}"'
      loop: "{{ vms| subelements('disk') }}" 

By performing a debug print, we can successfully get the nested keys values.

Configure passwordless authentication for ESXi – connect with publickey/privatekey

If you try to setup passwordless authentication for a Linux machine on your ESXi host and follow the same procedure that you would follow for a simple Linux box, the result will be a failure.

Normally you should do

ssh-keygen 

to create your pub, private keys and then

ssh-copy-id root@esxihost

to copy your keys on the esxi host. However this will not work and an additional step is required.

You should copy by your own the public key of your Linux machine to the ESXi host.

First of all enable ssh on the host and connect to the host.

The go to esxi and copy your public key under /etc/ssh/keys-root on authorized_keys. Keep in mind that permissions of this file should not be changed, otherwise it will not work.

After those actions you will be able to login passwordless on your esxi.

Cannot delete file [Datastore] vmkdump.dumpfile

If you face an issue during the deletion of a dump file that reside on a Datastore, you should deactivate the dump on the host and then try to delete it.

You can get a list of used dumpfiles on your ESXi host by connecting with ssh and performing the below command:

esxcli system coredump file list

This will output the dumpfiles that are currently in use, which will have true on the active column.

In order to de allocate the dump file you should unset the dump file and then retry to delete either by command or with the GUI.

esxcli system coredump file set -u

The command that will force the deletion is the below:

esxcli system coredump file remove -f /vmfs/volumes/volume/vmkdump/11111111-2222-3333-4444-555555555555.dumpfile