Vmware Esxi with Ansible

731 views
Skip to first unread message

Bishwajit Samanta

unread,
Aug 28, 2018, 10:09:58 AM8/28/18
to Ansible Project
Hi,

 I am trying to connect esxi 6.5 with ansible. I have copied ssh keys using ssh-copy-id to root id of the esxi server but when i am executing ansible -m ping vmware(ip) it says permission denied. Any idea how to make it work out ?

192.168.119.129 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,keyboard-interactive).\r\n",
    "unreachable": true
}

Sudoers is not there in esxi servers.. am i missing any point 

Greg Porter

unread,
Aug 28, 2018, 7:07:09 PM8/28/18
to Ansible Project
Hmm.  Well, by default ESXi hypervisors have ssh turned off.  You need to manually go turn it on.  Make sure ssh is running on the host.  https://pubs.vmware.com/vsphere-6-5/index.jsp?topic=%2Fcom.vmware.vcli.getstart.doc%2FGUID-C3A44A30-EEA5-4359-A248-D13927A94CCE.html

HTH

Greg

Greg Porter

unread,
Aug 28, 2018, 7:16:32 PM8/28/18
to Ansible Project
Well.  after further thought...

When I did this, I used the VMware modules, like those at https://docs.ansible.com/ansible/latest/modules/list_of_cloud_modules.html#vmware

These require use of the VMware vSphere API Python Bindings , pyvmomi.  https://github.com/vmware/pyvmomi

You install pyvmomi on the machine you are running the Ansible playbooks from.

Then when you run a playbook that uses a VMware module, the playbook uses pyvmomi to connect directly to the ESXi hypervisor (or vCenter) using the VMware API.  Not ssh. 

The machine you run playbooks from has to have network access to the ESXi hypervisor or vCenter on the various ports that the vSphere API requires.

HTH some more.

Greg

Bishwajit Samanta

unread,
Aug 29, 2018, 6:40:39 AM8/29/18
to Ansible Project
Hi,

I am having below things in my Vm's (Vmware free license)

pyvmomi installed in Ubuntu 16 where ansible also installed (192.168.119.131)
ansible 2.7
Esxi 6.5(192.168.119.129)
1 VM (Ubuntu) in vmware named as Test (192.168.119.130) whose vmware guest_facts i am finding out through UUID

My host file (Ansible) 

[vmware]
192.168.119.129

My Playbook:

---
- name: Running Vmware Playbook
  hosts: vmware

  tasks:
    - name: Printing Vmware Guest Facts
      vmware_guest_facts:
        hostname: 192.168.119.129
        username: root
        password: xxxxxxxx
        datacenter: vntgautvol-64.localdomain
        validate_certs: no
        uuid: 56654D56-5A76-EBD5-DCC9-711988D1E157
      delegate_to: localhost
      register: facts

    - debug: var=facts.stdout

Error Statement: I guess my ansible playbook is still accepting ssh connection but it should use pyvmomi connection may be not sure what to do. Can anyone help me out ?

Error Log::-
==========

root@ansibleubuntu:~/ansible# ansible-playbook test.yml

PLAY [Running Vmware Playbook] *********************************************************************************

TASK [Gathering Facts] *****************************************************************************************
fatal: [192.168.119.129]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,keyboard-interactive).\r\n", "unreachable": true}
        to retry, use: --limit @/root/ansible/test.retry

PLAY RECAP *****************************************************************************************************
192.168.119.129            : ok=0    changed=0    unreachable=1    failed=0

Greg Porter

unread,
Aug 29, 2018, 1:55:43 PM8/29/18
to Ansible Project
I'm new at this too.  I just got my first ever playbook to run the other day.

So this is what my playbook looks like.  Note that I keep my credentials for vCenter in a vault, which is NOT required.

[glporter@montara 29Aug1743Z ansible]$
rToShowSnapshots.ymlAug1743Z ansible]$cat playbooks/vmware/testPlaybookForVcenterToShowSnapshots.yml

---
- hosts: 127.0.0.1
  #
  gather_facts: false
  connection: local

  # Do some random command vCenter task....
  tasks:
    # On the command line use --extra-vars="my_vault=/path/to/vault"
    - include_vars: "{{ my_vault }}"
      no_log: true

    - name: Gather facts about the virtual machine's snapshots in given vCenter
      vmware_guest_snapshot_facts:
        datacenter: smx
        hostname: redacted
        uuid:  420b20a6-ab00-fe60-3a75-b9ed3a666f72
        password: "{{ vcenter_morro_mgmt_smx_password }}"
        username: "{{ vcenter_morro_mgmt_smx_username }}"
        validate_certs: no
      delegate_to: localhost
      register: facts
      no_log:  true

    - name: Display all facts known for a VM
      debug:
        msg: "{{ item }}"
      with_items:

[glporter@montara 29Aug1752Z ansible]$ ansible-playbook -i inventories/smx/hosts  playbooks/vmware/testPlaybookForVcenterToShowSnapshots.yml   --ask-vault-pass --extra-vars "my_vault=/home/glporter/git/st-utilities/ansible/vault/smx_vault.yml" -v
Using /home/glporter/git/st-utilities/ansible/ansible.cfg as config file
Vault password:

PLAY [127.0.0.1] **********************************************************************************************

TASK [include_vars] *******************************************************************************************
ok: [127.0.0.1] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

TASK [Gather facts about the virtual machine's snapshots in given vCenter] ************************************
ok: [127.0.0.1 -> localhost] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

TASK [Display all facts known for a VM] ***********************************************************************
ok: [127.0.0.1] => (item={'failed': False, u'changed': False, u'guest_snapshots': {u'current_snapshot': {u'id': 215, u'state': u'poweredOn', u'creation_time': u'2018-08-20T18:43:48.086085+00:00', u'description': u'bar', u'name': u'test snap 2'}, u'snapshots': [{u'state': u'poweredOn', u'creation_time': u'2018-08-20T14:17:41.428299+00:00', u'description': u'before install of pyvmomi', u'name': u'2018-08-20', u'id': 213}, {u'state': u'poweredOn', u'creation_time': u'2018-08-20T18:42:59.533428+00:00', u'description': u'foo', u'name': u'test snap 1', u'id': 214}, {u'state': u'poweredOn', u'creation_time': u'2018-08-20T18:43:48.086085+00:00', u'description': u'bar', u'name': u'test snap 2', u'id': 215}]}}) => {
    "msg": {
        "changed": false,
        "failed": false,
        "guest_snapshots": {
            "current_snapshot": {
                "creation_time": "2018-08-20T18:43:48.086085+00:00",
                "description": "bar",
                "id": 215,
                "name": "test snap 2",
                "state": "poweredOn"
            },
            "snapshots": [
                {
                    "creation_time": "2018-08-20T14:17:41.428299+00:00",
                    "description": "before install of pyvmomi",
                    "id": 213,
                    "name": "2018-08-20",
                    "state": "poweredOn"
                },
                {
                    "creation_time": "2018-08-20T18:42:59.533428+00:00",
                    "description": "foo",
                    "id": 214,
                    "name": "test snap 1",
                    "state": "poweredOn"
                },
                {
                    "creation_time": "2018-08-20T18:43:48.086085+00:00",
                    "description": "bar",
                    "id": 215,
                    "name": "test snap 2",
                    "state": "poweredOn"
                }
            ]
        }
    }
}

PLAY RECAP ****************************************************************************************************
127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=0

Greg Porter

unread,
Aug 29, 2018, 1:58:42 PM8/29/18
to Ansible Project
So I think to answer your question, look at the top of the playbook
---
- hosts: 127.0.0.1
  #
  gather_facts: false
  connection: local

I ran this against my Ansible script running host, with a local connection, and I said "don't ask about facts"

So I think it runs on the ansible host
The ansible host has pyvmomi so it can talk to ESXi
I supply ESXI creds
The ansible host uses pyvmomi ON ITSELF and then connects via the API to the specified ESXi

Something like that.

Greg

On Tuesday, 28 August 2018 07:09:58 UTC-7, Bishwajit Samanta wrote:
Reply all
Reply to author
Forward
0 new messages