【问题标题】:Why does ansible not work with vmware module?为什么 ansible 不适用于 vmware 模块?
【发布时间】:2020-12-16 22:43:24
【问题描述】:

Ansible 2.9.10 Vcenter 6.7

我对这个突然出现的问题感到很困惑。

几周前我上次运行它时一切正常,突然之间它完全停止工作,我不知道出了什么问题。

我有一本使用 ansible 简单关闭 VM 来宾的剧本

- hosts: all

  tasks:
- name: Power off VM
          vmware_guest_powerstate:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            name: "{{ guest_name | default(inventory_hostname) }}"
            state: shutdown-guest
            state_change_timeout: 600
            validate_certs: False
          delegate_to: localhost

现在,每当我运行 playbook 时,都会出现以下错误:

ansible-playbook -i hosts poweroff.yml -l myserverA.mydomain.com

TASK [Power off VM] ***********************************************************************************************************************
fatal: [myserverA.mydomain.com]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}

我已经测试了其他各种也可以工作的 vmware 模块,例如拍摄快照,但它们都不起作用。还要针对一组产生相同错误的不同服务器进行测试。

该错误令人困惑的是它声明“ssh”问题,但我认为 Ansible 节点通过 API 连接到 vcentre,因此端口 443,而不是使用 ssh?

我已经确认 Ansible 节点可以通过 ssh 进入服务器 myserverA.mydomain.com 并且可以 ping vcentre 主机,还可以 telnet 到端口 443 和 22。

谷歌搜索没有找到和我有同样问题的人。

请问有人有什么想法吗?

【问题讨论】:

  • 当您只想在本地主机上运行任务时是否需要hosts: all(使用delegate_to)?
  • 这更像是我的剧本的一个片段,在关闭 VM 后还会发生其他各种不使用 delegate_to 的任务。
  • 你的意思是只有vmware模块不工作?你检查过 ping 或命令模块吗?

标签: ansible vmware


【解决方案1】:

ssh错误的原因是gather_facts启用。
gather_facts 启用时,Ansible 连接到inventory_hostname
这个东西的解决办法是gather_facts禁用。
这是gather_facts禁用的剧本。

- hosts: all
  gather_facts: false

  tasks:
    - name: Power off VM
      vmware_guest_powerstate:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        name: "{{ guest_name | default(inventory_hostname) }}"
        state: shutdown-guest
        state_change_timeout: 600
        validate_certs: False
      delegate_to: localhost

【讨论】:

    【解决方案2】:

    查看插件文档中提供的以下示例。

    - 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/my_folder"
        moid: vm-42
        state: powered-on
      delegate_to: localhost
      register: deploy
    

    您应该连接到您的 vCenter 服务器而不是所有主机。您可以在保管库中添加您的凭据。

    vars_files:
        - secrets.yml
        - other.yml
    

    secrets.yml

    vcenter_username : administrator@vsphere.local
    vcenter_password : password
    vcenter_hostname : vcenter.hostname
    

    并使用 vault 命令运行 playbook

    ansible-playbook playbook.yml --ask-vault-pass
    

    如果仍然失败,请尝试在安装了 vcenter 的主机和 ansible 服务器之间建立 ssh 连接。 (ssh 密钥安装)

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 2018-06-19
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      相关资源
      最近更新 更多