Ansible

自定義事實沒有收到價值

  • September 27, 2021

這是與 vmware 對話的角色的劇本部分。

我正在嘗試設置一個(target_vm)接收虛擬機名稱的自定義事實。

但是當我使用調試模組列印它的值時,它似乎是空的。

我不知道我在set_fact模組中做錯了什麼。

這是劇本

- delegate_to: localhost
 become: no
 delegate_facts: yes
 vars:
   vc_hostname: 'vcenter.bio.local'
   vc_username: 'bio.local\ansible'
   vc_password: "{{ v_pass }}"
   vm_uuid: '4217200F-46D2-C9FD-E7FD-768D21B327E8' | lower
 block:
   - name: Gather only registered virtual machines
     vmware_vm_info:
       hostname: '{{ vc_hostname }}'
       username: '{{ vc_username }}'
       password: '{{ vc_password }}'
       validate_certs: False
       vm_type: vm
     delegate_to: localhost
     register: virtual_info
     no_log: true

   - name: Set facts target_vm when equal vm-name
     set_fact:
       target_vm: "{{ my_item.guest_name }}"
     with_items: "{{ virtual_info.virtual_machines }}"
     when: my_item.uuid == vm_uuid
     loop_control:
       loop_var: my_item


   - name: Print Guest Name
     debug:
       msg:
         - "{{ foo_item.guest_name }}"
         - "{{ target_vm | default ('') }}"
     with_items: "{{ virtual_info.virtual_machines }}"
     when: foo_item.uuid == vm_uuid
     loop_control:
       loop_var: foo_item

這是輸出

TASK [vmwaretaks : Gather only registered virtual machines] ***********************************************************************************************
ok: [testvm]

TASK [vmwaretaks : Set facts target_vm when equal vm-name] ************************************************************************************************
ok: [testvm] => (item={'guest_name': 'TESTVM', 'guest_fullname': 'Red Hat Enterprise Linux 6 (64-bit)', 'power_state': 'poweredOn', 'ip_address': '192.168.54.32', 'mac_address': ['00:32:52:97:e9:c8'], 'uuid': '4217200f-46d2-c9fd-e7fd-768d21b327e8', 'vm_network': {'00:32:52:97:e9:c8': {'ipv4': ['192.168.54.32'], 'ipv6': ['fe80::250:56ff:fe97:d4c3']}}, 'esxi_hostname': 'b3j15esx05.bio.local', 'cluster': 'JAS-Lab-DEP', 'attributes': {}, 'tags': []})

TASK [vmwaretaks : Print Guest Name] **********************************************************************************************************************
ok: [testvm] => (item={'guest_name': 'TESTVM', 'guest_fullname': 'Red Hat Enterprise Linux 6 (64-bit)', 'power_state': 'poweredOn', 'ip_address': '192.168.54.32', 'mac_address': ['00:32:52:97:e9:c8'], 'uuid': '4217200f-46d2-c9fd-e7fd-768d21b327e8', 'vm_network': {'00:32:52:97:e9:c8': {'ipv4': ['192.168.54.32'], 'ipv6': ['fe80::250:56ff:fe97:d4c3']}}, 'esxi_hostname': 'b3j15esx05.bio.local', 'cluster': 'JAS-Lab-DEP', 'attributes': {}, 'tags': []}) => {
   "msg": [
       "TESTVM",
       ""
   ]
}

PLAY RECAP ************************************************************************************************************************************************
testvm              : ok=13   changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

如您所見,列印 target_vm var 輸出的第二條消息為空

希望您能提供幫助。提前致謝!

好。似乎事實是由它的價值決定的,但我沒有正確列印它,所以我將在其他文章中再次公開這個問題。

引用自:https://serverfault.com/questions/1078439