如何設定機器狀態:使用community.vmware.vmware_guest_powerstate任務開機?

如何設定機器狀態:使用community.vmware.vmware_guest_powerstate任務開機?

我對 Ansible 還很陌生,所以我可能配置錯誤
[我有一個在 CentOS8 中運行 Ansible 服務的 Docker 容器,
我有一個包含 Ansible 文件的 Ansible 存儲庫(這是一個 .Git 存儲庫]

我的意願是自動將vCenter Server 中的每個實驗室(實驗室由8 個虛擬機器、5 個Windows Server 2016 和3 個Windows 10 組成。DC 包括在這些電腦中啟用winrm 的政策)還原到特定快照。但首先我要嘗試:在實驗室機器打開時將其關閉,然後當實驗室機器關閉時打開它們的電源

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

  • ansible-galaxy init使用命令名稱建立角色vcenter(請參閱下面的目錄樹)
  • 在資料夾內建立了一些 vCenter 任務檔案tasks(請參閱下面的目錄樹)。以下是poweroff.ymlpoweron.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 }}/vm/{{ folder }}"
    name: "{{ ansible_hostname }}"
    # name: "{{ guest_name }}"
    validate_certs: no
    state: powered-off
    force: yes
  delegate_to: localhost
  register: deploy
- name: Set the state of a virtual machine to poweron using MoID
  community.vmware.vmware_guest_powerstate:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    folder: "/{{ datacenter_name }}/vm/{{ folder }}"
    name: "{{ ansible_hostname }}"
    # moid: vm-42
    validate_certs: no
    state: powered-on
  delegate_to: localhost
  register: deploy
  • 在文件中提供 vCenter 憑證vcenter\vars\main.yml,如下所示:
# vars file for vcenter
vcenter_hostname: vcenter.foo.com
vcenter_username: [email protected]
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

我成功執行了劇本,關閉了實驗室中所有機器的電源,然後我「打開」了角色中的開機任務,如下所示:

---
# tasks file for roles/vcenter
# - import_tasks: poweroff.yml
- import_tasks: poweron.yml
# - import_tasks: revert.yml
# - import_tasks: shutdown.yml

現在所有實驗室的機器都已關閉,執行 playbook 時會出現以下錯誤:

PLAY [revert vmware vcenter lab] *************************************************
TASK [Gathering Facts] ***********************************************************
fatal: [vm1.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: 
HTTPConnectionPool(host='vm1.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae4908d0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm2.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm2.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae487b00>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm3.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm3.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae48acc0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm4.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm4.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae48de80>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm5.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: 
HTTPConnectionPool(host='vm5.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae41f080>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm6.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm6.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae41d7f0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm7.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm7.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae428048>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm8.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm8.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae425588>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}

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

為什麼 poweroff 任務可以正常運作,而 poweron 任務卻不能?我該如何解決這個問題?

我的儲存庫:

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:

答案1

問題的解決方法是:
我將folder鍵值設為"/{{ datacenter_name }}/"
我將poweron任務添加到附加任務中revert- 意味著poweron任務僅當它是任務序列的一部分時才對我
有用不幸的是,該poweron任務作為獨立任務不適合我

相關內容