如何使用Ansible

时间:2017-03-10 09:49:16

标签: ansible ansible-2.x

我尝试使用Ansible从dict var获取特定值,但我的代码不能正常工作。

在我的代码下面:

- name: "Creating VMs for [{{env}}] environment"
  vmware_guest:
    hostname: "{{item.value.vcenter_hostname}}"
    username: "{{item.value.vsphere_username}}"
    password: "{{item.value.vsphere_password}}"
    esxi_hostname: "{{item.value.esxi_hostname}}"
    datacenter: "{{item.value.esxi_datacenter}}"
    folder: "{{item.value.vm_folder}}"
    name: "{{item.key}}"
    state: poweredon
    hardware:
      memory_mb: "{{item.value.vm_memory}}"
      num_cpus: "{{item.value.vm_cpu}}"
    nic:
    - vlan: "{{item.value.vm_networks_vlan}}"
      device_type: "{{item.value.vm_networks_device_type}}"
    template: "{{item.value.vm_template}}"
    wait_for_ip_address: yes
  register: vm_list
  with_dict: "{{vsphere_provisioning}}"

- name: "Print IP"
  debug:
    msg: "Ip Address: {{ item.value.instance.ipv4 }}"
  with_dict: "{{ vm_list }}"

下面是dict结构:

{
"changed": true, 
"failed": false, 
"instance": {
    "hw_eth13": {
        "addresstype": "assigned", 
        "ipaddresses": [
            "x.x.x.x", 
            "x::x:x:x:x"
        ], 
        "label": "Network adapter 1", 
        "macaddress": "x:x:x:x:x:x", 
        "macaddress_dash": "x-x-x-x-x-x", 
        "summary": "DVSwitch: x x x x x x x x-x x X x x x x x"
    }, 
    "hw_guest_full_name": "CentOS 4/5/6/7 (64-bit)", 
    "hw_guest_id": "centos64Guest", 
    "hw_interfaces": [
        "ethx"
    ], 
    "hw_memtotal_mb": 2048, 
    "hw_name": "XX_XX_XX", 
    "hw_power_status": "poweredOn", 
    "hw_processor_count": 1, 
    "hw_product_uuid": "xxx-xxx-xxx-xxx-xxx", 
    "ipv4": "XXX.XXX.XXX.XXX", 
    "ipv6": "XXX.XXX.XXX.XXX", 
    "module_hw": true
}}

Ansible返回的错误消息:

  

致命:[localhost]:失败! => {       “失败”:是的,       “msg”:“字段'args'的值无效,似乎包含未定义的变量。   错误是:'ansible.vars.unsafe_proxy.AnsibleUnsafeText对象'没有属性'instance'   该错误似乎出现在'/.../tasks/main.yml':第30行第3列,但可能在文件的其他位置,具体取决于确切的语法问题。   违规行似乎是: - 名称:“打印IP地址”在这里“   }

1 个答案:

答案 0 :(得分:0)

感谢您的回归,现在工作正常。

- name: "Print IP Address"
  debug:
    msg: "IP Address: {{ item.instance.ipv4 }}"
  with_items: "{{ vm_list.results }}"