Virtualization

為什麼ansible無法辨識vcenter windows機器?

  • July 23, 2021

我對 Ansible 很陌生,所以我可能配置錯誤

$$ I have a Docker container running Ansible service in it I have an Ansible repository that include the Ansible files (this is a .Git repository $$

我的意願是自動將 vCenter 伺服器中的每個實驗室恢復為特定快照

所以,我(在ansible-roles-explained-with-examples 指南的幫助下):

  • ansible-galaxy init使用命令名稱創建了一個角色vcenter(參見下面的目錄樹)
  • 在文件夾內創建了一些 vCenter 任務文件tasks(請參見下面的目錄樹)。以下是poweroff.yml任務文件的範例:
- name: Set the state of a virtual machine to poweroff
 community.vmware.vmware_guest_powerstate:
   hostname: "{{ vcenter_hostname }}"
   username: "{{ vcenter_username }}"
   password: "{{ vcenter_password }}"
   folder: "/{{ datacenter_name }}/{{ folder }}"
   # name: "{{ guest_name }}"
   name: "{{ ansible_hostname }}"
   validate_certs: no
   state: powered-off
 delegate_to: localhost
 register: deploy
  • 在文件中提供 vCenter 憑據vcenter\vars\main.yml,如下所示:
# vars file for vcenter
vcenter_hostname: vcenter.foo.com
vcenter_username: hiddai@foo.com
vcenter_password: f#0$o#1$0o
datacenter_name: FOO_Fighters
# datastore_name: 
cluster_name: FOO
folder: '/FOO/PRODUCT/DOMAIN.COM/' 
  • 將任務包含在tasks\main.yml帶有import-task密鑰的文件中,如下所示:
---
# tasks file for roles/vcenter
- import_tasks: poweroff.yml
# - import_tasks: poweron.yml
# - import_tasks: revert.yml
# - import_tasks: shutdown.yml
  • 在庫存庫中創建了一個all.yml內部 group_vars 文件夾(我不知道它是否是一種專業的方法),其中包括所有 winrm 詳細資訊,如下所示:
---
#WinRM Protocol Details
ansible_user: DOMAIN\user
ansible_password: f#0$o#1$0o
ansible_connection: winrm
ansible_port: 5985
ansible_winrm_scheme: http
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: ntlm
ansible_winrm_read_timeout_sec: 60
ansible_winrm_operation_timeout_sec: 58
  • 創建了一個revert_lab.yml包含角色的劇本,就像這樣
---
- name: revert an onpremis lab
 hosts: all
 roles:
 - vcenter

ansible.cfg的是這樣的:

[defaults]
inventory = /ansible/inventories
roles_path = ./roles:..~/ansible/roles

我執行了劇本來恢復實驗室中的所有機器:

ansible-playbook playbooks/revert_vcenter_lab.yml -i inventories/test/onpremis/domain.com/lab_r.yml

我得到的錯誤是:

TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Error when collecting winrm facts: You cannot call a method on a null-valued expression.  At line:15 char:17  + ...
$ansibleFacts.ansible_win_rm_certificate_expires = $_.Not ...  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      + CategoryInfo          :  
InvalidOperation: (:) [], RuntimeException      + FullyQualifiedErrorId : InvokeMethodOnNull      at <ScriptBlock>, <No file>: line 15  at <ScriptBlock>, <No file>: line  
13
ok: [vm1.domain.com]
ok: [vm2.domain.com]
ok: [vm3.domain.com]
ok: [vm4.domain.com]
ok: [vm5.domain.com]
ok: [vm6.domain.com]
ok: [vm7.domain.com]
ok: [vm8.domain.com]

TASK [vcenter : Set the state of a virtual machine to poweroff] ***********************************************************************************************************
fatal: [vm1.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM1'"}
fatal: [vm2.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM2'"}
fatal: [vm3.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM3'"}
fatal: [vm4.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM4'"}
fatal: [vm5.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM5'"}
fatal: [vm6.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM6'"}
fatal: [vm7.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM7'"}
fatal: [vm8.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM8'"}

PLAY RECAP ****************************************************************************************************************************************************************
vm1.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm2.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm3.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm4.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm5.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm6.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm7.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm8.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

a)我如何擺脫Error when collecting winrm facts錯誤?(看起來 playbook 無法辨識all.yml帶有 win 的文件,但為什麼?)

b)如何修復錯誤“無法為不存在的虛擬機設置電源狀態……”?(我們可以看到lab_r.yml文件(來自庫存庫)中提到的fqdns對機器的playbook訪問,但錯誤與vCenter平台中顯示的機器名稱有關……)

我的儲存庫:

C:.
├───ansible
│   │   ansible.cfg
│   ├───inventories
│   │   └───test
│   │       ├───cloud
│   │       └───onpremis
│   │           └───domain.com
│   │               │   lab_j.yml
│   │               │   lab_r.yml
│   │               └───group_vars
│   │                       all.yml
│   ├───playbooks
│   │       revert_lab.yml
│   └───roles
│       └───vcenter
│           ├───tasks
│           │       main.yml
│           │       poweroff.yml
│           │       poweron.yml
│           │       revert.yml
│           │       shutdown.yml
│           └───vars
│                   main.yml

我的庫存 lab_r.yml- 這是部分架構

---
all:
 children:
   root:
     children:
       center:
         children:
           appservers:
             hosts:
               vm1.domain.com:
           qservers:
             hosts:
               vm2.domain.com:
           dbservers:
             hosts:
               vm3.domain.com:

從文件中看不是很明顯,但是/vm/您的文件夾路徑中缺少該字元串。

- name: Set the state of a virtual machine to poweroff
 community.vmware.vmware_guest_powerstate:
   folder: "/{{ datacenter_name }}/vm/{{ folder }}"
   name: "{{ ansible_hostname }}"

我想需要區分數據中心、數據儲存、主機等中的其他資源。

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