无法使用ansible获取Azure虚拟机的priavte IP

时间:2019-04-03 17:43:07

标签: ansible ansible-2.x ansible-inventory ansible-facts

我正在尝试将ansible创建的虚拟机的Private Ip地址写入主机清单。但是,我无法找到ansible的蔚蓝私有IP属性

以下是我用于获取远程主机的私有IP的代码,但ansible并未获取远程主机的任何私有IP

- name: Create virtual network inteface card
  azure_rm_networkinterface:
    resource_group: apache-corp
    name: ansibleServerNIC
    virtual_network: ansibleServerVnet
    subnet: ansibleServerSubnet
    public_ip_name: ansibleServerPublicIP
    security_group: ansibleNetworkSecurityGroup
  register: network_interface

- name: show priavte_ip
  debug: "{{ network_interface.ip_configuration.private_ip_address }}"

错误提示:

"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ip_configuration'\n\nT

2 个答案:

答案 0 :(得分:0)

这是我使用的代码:

 - name: Get network interface details
    azure_rm_networkinterface_facts:
      resource_group: "{{vm.resource_group}}"
      name: "{{vm.network_interface}}"
      profile: "{{vm.azure_profile}}"
    register: azure_networkinterfaces
    delegate_to: localhost

  - name: print internal IP
    debug: msg="{{vm.vm_name}} internal ip is {{azure_networkinterfaces.ansible_facts.azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress}}"

  - name: set value of vm internal IP
    set_fact: 
      vm_internal_ip: "{{azure_networkinterfaces.ansible_facts.azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress}}"

答案 1 :(得分:0)

由于某种原因我无法使示例正常工作,我不得不使用几乎没有什么不同的代码。

- name: Get facts for one network interface
  azure_rm_networkinterface_info:
    resource_group: "{{ vm_resource_group }}"
    name: "{{ vm_name }}-nic"
  register: networkinterface_output

- name: set value of vm internal IP
  set_fact:
    vm_internal_ip: "{{ networkinterface_output.networkinterfaces[0].ip_configurations[0].private_ip_address}}"

希望这对某人有帮助。 Ansible,networkinterface_info