How retrieve the name of specific dictionary - Ansible

对着背影说爱祢 提交于 2019-12-11 05:34:27

问题


I need help to retrieve only the name of the Virtual Machines of my ESXI Host.

- name: VM-FACTS
  vmware_vm_facts:
    hostname: "{{ ansible_hostname }}"
    username: "{{ ansible_user }}"
    password: "{{ ansible_password }}"
    validate_certs: False
  delegate_to: localhost
  register: vmfacts

- name: Debug
  debug:
    var: vmfacts.virtual_machines

Output of the variable:

ok: [xxxxx] => {
    "vmfacts.virtual_machines": {
        "001-MV-Test": {
            "cluster": null,
            "esxi_hostname": "xxxxx",
            "guest_fullname": "Red Hat Enterprise Linux 7 (64-bit)",
            "ip_address": "10.252.130.196",
            "mac_address": [
                "00:50:56:b3:c5:b0"
            ],
            "power_state": "poweredOn",
            "uuid": "4233910e-4fde-7d1b-765d-b748bf9d1cd9",
            "vm_network": {
                "00:50:56:b3:c5:b0": {
                    "ipv4": [
                        "10.252.130.196"
                    ],
                    "ipv6": [
                        "fe80::250:56ff:feb3:c5b0"
                    ]
                }
            }
        }
    }
}

I want to have only the name of the virtual machine (001-MV-Test). Because, I want to retrieve more information with vmware_guest_facts.

Thanks!


回答1:


Is this the code that you're looking for?

- debug: msg="{{ item.key }}"
  loop: "{{ vmfacts.virtual_machines|dict2items }}"


来源:https://stackoverflow.com/questions/53453718/how-retrieve-the-name-of-specific-dictionary-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!