如何使用 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

现在实验室所有机器都关闭了,执行剧本,出现以下错误:

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任务对我来说并不像独立任务那样有效

相关内容