2

I try to execute this ansible yml file to get vm info in vsphere,

I have two files:

hosts file:

[Web]
192.168.11.11 #ip of my vsphere server

test.vmware2.yaml file:

- name: Gather all registered virtual machines
  community.vmware.vmware_vm_info:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
  delegate_to: localhost
  register: vminfo

I execute with this command:

ansible -i /mnt/hosts test.vmware2.yaml

I have this message:

ERROR! 'community.vmware.vmware_vm_info' is not a valid attribute for a Play

The error appears to be in '/mnt/test.vmware2.yaml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Gather all registered virtual machines
  ^ here

I want to know what is the problem ?

thanks for your response

sadok-f
  • 1,387
  • 15
  • 28
Mila88
  • 23
  • 5
  • maybe you can put here the ansible version you use? `ansible --version` – sadok-f Oct 15 '20 at 10:20
  • Is that all you have in `test.vmware2.yaml`? – seshadri_c Oct 15 '20 at 10:26
  • i have this version of ansible ansible --version ansible 2.9.6 config file = /etc/ansible/ansible.cfg configured module search path = ['/home/gthuraisingam/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] – Mila88 Oct 15 '20 at 11:01
  • its that all i have in the file [test.vmware2.yaml] – Mila88 Oct 15 '20 at 11:02

1 Answers1

1

I think you want to execute an ansible-playbook:

ansible-playbook -i /mnt/hosts test.vmware2.yaml

then you playbook file should look like this:

 - hosts: Web
   gather_facts: false
   become: false
   vars:
      vcenter_hostname: 10.10.10.1
      vcenter_username: user
      vcenter_password: password
   tasks:
     - name: Gather all registered virtual machines
       community.vmware.vmware_vm_info:
         hostname: '{{ vcenter_hostname }}'
         username: '{{ vcenter_username }}'
         password: '{{ vcenter_password }}'
       delegate_to: localhost
       register: vminfo

    - debug:
        msg: "{{ item.guest_name }}, {{ item.ip_address }}"
      with_items:
        - "{{ vminfo.virtual_machines }}"
sadok-f
  • 1,387
  • 15
  • 28
  • i execut this but i have this message :[WARNING]: Could not match supplied host pattern, ignoring: Web PLAY [Web] ********************************************************************************************************************************************************************************************* skipping: no hosts matched PLAY RECAP ************* – Mila88 Oct 15 '20 at 11:04
  • is the `/mnt/hosts` contains the group `[Web]`? you also can use `localhost` as host, since you're delegating the task to `localhost` – sadok-f Oct 15 '20 at 11:11
  • how can i use that, i try to execute since my computer 192.168.10.10 and my target is get information since vsphere 192.168.11.11 – Mila88 Oct 15 '20 at 11:21
  • how about `ansible-playbook playbook.yml` and change the hosts from `Web` to `localhost`, like this `- hosts: localhost` – sadok-f Oct 15 '20 at 11:26
  • i change Web to localhost in the fie test.vmware2.yaml but now i have this error message: TASK [Gather all registered virtual machines] ********************************************** fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'vcenter_hostname' is undefined\n\nThe error appears to be in '/mnt/test.vmware2.yaml': line 5, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Gather all registered virtual machines\n ^ here\n"} – Mila88 Oct 15 '20 at 11:31
  • yes you need to declare the variable you pass to your tasks, it can be done in the inventory level (group_vars) or in your playbook, to add a new option called `vars` and it a list of your variables, I'll update my post up for an exmple – sadok-f Oct 15 '20 at 11:35
  • now i have this message:ASK [Gather all registered virtual machines] ********************************************************************************************************************************************************** fatal: [localhost -> localhost]: FAILED! => {"changed": false, "msg": "Unable to connect to vCenter or ESXi API at 192.168.11.11 on TCP/443: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)"} – Mila88 Oct 15 '20 at 11:42
  • add `validate_certs : false` to the task options – sadok-f Oct 15 '20 at 11:43
  • i think its ok i'me connected, i have this message : PLAY [localhost] *************************************************************************************************************************************************************************************** TASK [Gather all registered virtual machines] ********************************************************************************************************************************************************** ok: [localhost -> localhost] PLAY RECAP **************************************************************************** – Mila88 Oct 15 '20 at 11:46
  • i want to know how to list all vm with ip please – Mila88 Oct 15 '20 at 11:47
  • you need to print the registered variable, I'll update the code up – sadok-f Oct 15 '20 at 11:50
  • that work :) , is it possible to have only guest_name and ip_address ? – Mila88 Oct 15 '20 at 11:58
  • i have this message : TASK [debug] ******************************************************************************************************************************************************************************************************* fatal: [localhost]: FAILED! => {"msg": "'vm_info' is undefined"} – Mila88 Oct 15 '20 at 12:13
  • i have many information, it possible to have only 'guest_name' and 'ip_address' like a list please ? – Mila88 Oct 15 '20 at 12:19
  • did you check my post? it should only print 2 attributes – sadok-f Oct 15 '20 at 12:20
  • i see your post but that print many information like 'guest_fullname', 'power_state', 'mac_address', 'ip_address', 'uuid' and more – Mila88 Oct 15 '20 at 12:22
  • maybe you can put here the full output – sadok-f Oct 15 '20 at 12:29
  • i commented this line msg: "{{ item.guest_name }}, {{ item.ip_address }}" and i have the same result, i think this line is not taken – Mila88 Oct 15 '20 at 12:33