簡體   English   中英

Ansible vmware_vm_facts:獲取特定VM的mac地址並將其寫入現有庫存

[英]Ansible vmware_vm_facts: get mac address for a specific VM and write it to existing inventory

我的電腦書必須達到目標:

a)從在vsphere上運行的特定VM獲取mac地址b)將其添加到我的庫存文件中

我的測試環境是:
-Vsphere 6.5
-Ansible 2.7在Centos 7.6上運行

我已經能夠指出a)在這篇文章之后如何檢索特定字典的名稱 - Ansible

劇本:

# Deploy a guest from a template*  
- hosts: 127.0.0.1  
  vars:
    vcenter_hostname: virtvcsami1.virtlab.local  
    vcenter_user: administrator@vsphere.local  
    vcenter_pass: Esxilab!1  
    vcenter_datastore: vsanDatastore  
    vcenter_datacenter: DatacenterMI  
  tasks:  
  - name: Gather VMware guest facts  
    vmware_vm_facts:  
      validate_certs: False  
      hostname: "{{ vcenter_hostname }}"  
      username: "{{ vcenter_user }}"  
      password: "{{ vcenter_pass }}"  
      vm_type: vm  
    delegate_to: localhost  
    register: vm_guest_facts  

  - debug: msg="{{ item.value.mac_address }}"  
    loop: "{{ vm_guest_facts.virtual_machines|dict2items }}"

但現在我還有兩個問題需要解決:

問題1)

Playbook獲取所有虛擬機,而我只需要一個名為“testvm”的虛擬機

[root @ nlnxmi1 testvmcdromiso] #ansible-playbook getvminfo.yml

玩[127.0.0.1] ******************************************** ************************************************** ************************************************** ***********************************************
任務[收集事實] ********************************************* ************************************************** ************************************************** ****************************************
好的:[127.0.0.1]
任務[收集VMware客人事實] ***************************************** ************************************************** ************************************************** ********************************
好的:[127.0.0.1 - > localhost]
任務[調試] ********************************************** ************************************************** ************************************************** *************************************************
ok:[127.0.0.1] =>(item = {'key':u'kubemst01','value':{u'guest_fullname':u'CentOS 7(64-bit)',u'vm_network':{u '00:50:56:be:de:b9':{u'ipv4':[u'192.168.40.31'],u'ipv6':[u'fe80 :: 250:56ff:febe:deb9']} ,u'52:54:00:62:fe:fe':{u'ipv4':[u'192.168.122.1'],u'ipv6':[]}},u'cluster':u'VirtlabMI' ,u'esxi_hostname':u'virtesxmi3.virtlab.local',u'mac_address':[u'00:50:56:be:de:b9'],u'power_state':u'poweredOn',u'ip_address ':u'192.168.40.31',u'uuid':u'423e7580-1ca4-a6ca-5cb4-5b8fa963d527'}})=> {“msg”:[“00:50:56:be:de:b9” ]}

ok:[127.0.0.1] =>(item = {'key':u'testvm','value':{u'guest_fullname':> u'CentOS 7(64-bit)',u'vm_network':{ },u'cluster':u'VirtlabMI',> u'esxi_hostname':u'virtesxmi1.virtlab.local',u'mac_address':> [u'00:50:56:be:a3:a0'], u'power_state':u'poweredOn',u'ip_address':你',> u'uuid':u'423e8645-ca2a-1097-2e1c-624810a461d1'}})=> {“msg”:[“00 :50:56:是:a3:a0“]} ......

問題2)

將mac地址添加到現有庫存文件,或者,如果不可能,至少在某個文件中

我嘗試在最后添加以下代碼:

  - set_fact: vm_mac_address="prova"  

  - name: Register host to Inventory  
    lineinfile:  
      path: /etc/ansible/testvmcdromiso/inventory  
      regexp: '(testvm)'  
      line: '\1 macaddres={{ vm_mac_address }}'  
      backrefs: yes  

[root @nlnxmi1 testvmcdromiso] #cat inventory [testhost] testvm macaddress = prova

但正如你所看到我只是使用了一個“固定”的字符串,而我需要從正在運行的虛擬機中獲取mac地址,但即使經過2天的嘗試,也從未弄清楚:(

我只是一個有安慰的初學者。 請你幫助我好嗎?

最佳馬可

Playbook獲取所有虛擬機,而我只需要一個名為“testvm”的虛擬機

那不是問題。 看起來vmware_vm_facts模塊返回一個字典,所以只要求你想要的vm:

  - debug: msg="{{ vm_guest_facts.virtual_machines['testvm'].mac_address }}"  

將mac地址添加到現有庫存文件,或者,如果不可能,至少在某個文件中

由於您現在知道如何檢索MAC地址,因此您可以修改lineinfile任務以使用該信息:

  - name: Register host to Inventory  
    lineinfile:  
      path: /etc/ansible/testvmcdromiso/inventory  
      regexp: '(testvm)'  
      line: '\1 macaddres={{ vm_guest_facts.virtual_machines['testvm'].mac_address }}'  
      backrefs: yes  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM