从ansible中的模块输出中解析json数据

时间:2018-04-24 09:00:45

标签: ansible

这似乎相当简单,我只是无法正确获取语法。我有一个看起来像这样的ansible剧本

- name: Get VMs
  gather_facts: no
  hosts: test
  connection: local

  tasks:

   - name: Get all vms
     vmware_vm_facts:
      hostname: hostname
      username: username
      password: password
      validate_certs: false
     delegate_to: localhost
     register: vmfacts

   - name: Set vm name
     set_fact:
      vm_name: "{{ vmfacts.virtual_machines.keys() }}"

vmfacts变量的输出是

ok: [serverhostname] => {
    "vmfacts": {
        "changed": false,
        "failed": false,
        "virtual_machines": {
            "server1": {
                "esxi_hostname": "host1",
                "guest_fullname": "Microsoft Windows Server 2012 (64-bit)",
                "ip_address": "",
                "mac_address": [
                    "43:55:65:23:er:yr"
                ],
                "power_state": "poweredOff",
                "uuid": "839403jj-4jn8-8395-j4l2-i3j24ijil3",
                "vm_network": {
                    "11:22:33:844:we:re": {
                        "ipv4": [],
                        "ipv6": []
                    }
                }
            },
            "server2": {
                "esxi_hostname": "host2",
                "guest_fullname": "Microsoft Windows Server 2003 (32-bit)",
                "ip_address": "",
                "mac_address": [
                    "43:54:64:66:8s:47"
                ],
                "power_state": "poweredOff",
                "uuid": "423jlk2-80d9-jl22-2335-4324jklj432",
                "vm_network": {
                    "22:44:55:66:9d:47": {
                        "ipv4": [],
                        "ipv6": []
                     }
                }
            }
        }

我正在尝试获取每个vm的uuid,但是无法弄清楚如何通过变量vm名称(server1,server2)。我试过了

- name: Print UUID
     debug:
      var: "{{ vmfacts.virtual_machines[item].uuid }}"
     with_items: vm_name

抛出错误

 {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'vm_name'}

0 个答案:

没有答案