Check status of VMware tools & reboot if VMware tools are not running

65 views
Skip to first unread message

Kiran Kumar

unread,
Apr 17, 2020, 3:30:38 AM4/17/20
to Ansible Project
Hi


I need to check status of VMware tools on vms & if VMware tools are not running, then reboot the vm. I have below playbook which works fine to find vm name & VMware tools status. Please help on how can i extract the vm name & take action as per status.  

Note : i have to use the VMware modules as the OS typically will be in hung status ( hence tools not running ) , hence avoid user OS level task ( and use VMware task ) 

Thanks 

---
- hosts: localhost
  gather_facts: false
  connection: local
  collections:
  - community.vmware
  - community.general
  vars_files:
    - ./vars/vmware_vars.yml
  vars:
    vm_list:
      - vm1
      - vm2
  tasks:
    - name: Gather VM info
      vmware_guest_info:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: no
        name: "{{ item }}"
      register: vm_info
      with_items: "{{ vm_list }}"
    - name: Tools status all
      debug:
        msg: "{{ vm_info.results  | json_query(jmesquery) }}"
      vars:
        jmesquery: "[?instance.hw_name == '{{ item }}'].instance.guest_tools_status"
      with_items: "{{ vm_list }}"

Abhijeet Kasurde

unread,
Apr 17, 2020, 4:26:53 AM4/17/20
to ansible...@googlegroups.com
---
- hosts: localhost
vars_files:
- vcenter_vars.yml
vars:
vm_list:
- test01
tasks:
- name: Get information about the virtual machine
vmware_guest_info:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
folder: /Asia-Datacenter1/vm/
name: "{{ item }}"
datacenter: Asia-Datacenter1
register: vm_info
with_items: "{{ vm_list }}"

- name: Get Tools status
set_fact:
desired_vms: "{{ vm_info.results | json_query(jmesquery) }}"
vars:
jmesquery: "[?instance.guest_tools_status == 'guestToolsNotRunning'].instance.hw_name"

- name: Reboot the given VM
vmware_guest_powerstate:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
folder: /Asia-Datacenter1/vm/
name: "{{ item }}"
state: 'reboot-guest'
with_items: "{{ desired_vms }}"



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3473c234-0db3-4808-b73d-d9ba8460b5ee%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Tony Wong

unread,
Apr 18, 2020, 2:22:29 PM4/18/20
to Ansible Project
Hi

why is it when i run this playbook I get this error

tony@ubuntu:~/ansiblework$ ansible-playbook vmware_tools.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to be in '/home/tony/ansiblework/vmware_tools.yml': line 9, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - name: Get information about the virtual machine
    ^ here

Jorge Rúa

unread,
Apr 18, 2020, 2:58:38 PM4/18/20
to ansible...@googlegroups.com
That error message often indicates an indentation issue or a missing module parameter. 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Apr 18, 2020, 4:11:43 PM4/18/20
to ansible...@googlegroups.com
That module was introduced in ansible 2.5: 

Do you run an older version?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Tony Wong

unread,
Apr 18, 2020, 4:15:24 PM4/18/20
to ansible...@googlegroups.com
this is the playbook

---
- name: get info vm
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: get info about the virtual machine
        vmware_guest_info:
          hostname: vcnerter
          username: admini...@vsphere.local
          password: xxxxxxxx
          validate_certs: False
          name: myvm
          schema: "vsphere"
          properties: ["config.hardware.memoryMB", "guest.disk", "overallStatus"]
          datacenter: DC
          register: info

Jean-Yves LENHOF

unread,
Apr 18, 2020, 4:20:36 PM4/18/20
to ansible...@googlegroups.com

Please as said by another people here, respect indentations stricly !

vmware_guest_info & name have not the same indentation as written by A. Kasurde

Regards,

JYL

Message has been deleted

David Foley

unread,
Apr 18, 2020, 4:26:07 PM4/18/20
to Ansible Project
---
nameget info vm
  hostslocalhost
  connectionlocal
  gather_factsfalse
  tasks:
  - nameget info about the virtual machine
    vmware_guest_info:
      hostnamevcnerter
      username
      passwordxxxxxxxx
      datacenterDC
      validate_certsFalse
      namemyvm
      schema"vsphere"
      properties: ["config.hardware.memoryMB""guest.disk""overallStatus"]
    delegate_tolocalhost
    registerinfo
  

Tony Wong

unread,
Apr 18, 2020, 4:43:00 PM4/18/20
to ansible...@googlegroups.com
what editor can i use on my ubuntu box? I cant use anything other than vim. 


still getting the error. 

tony@ubuntu:~/ansiblework$ ansible-playbook info.yml

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/home/tony/ansiblework/info.yml': line 3, column 8, but may

be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

name: get info vm
  hosts: localhost
       ^ here

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Tony Wong

unread,
Apr 18, 2020, 4:46:14 PM4/18/20
to ansible...@googlegroups.com
Screen Shot 2020-04-18 at 1.45.30 PM.png

David Foley

unread,
Apr 18, 2020, 4:53:47 PM4/18/20
to Ansible Project
you missing " - " 


To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

David Foley

unread,
Apr 18, 2020, 4:55:19 PM4/18/20
to Ansible Project
Why Aren't you using your Local Machine using Atom or VS Code to build your Yaml files then using WinSCP copy the playbooks over ? 
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Tony Wong

unread,
Apr 18, 2020, 5:26:10 PM4/18/20
to ansible...@googlegroups.com
this is what i have. but still not working

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/672cc33a-758f-4083-a48b-0882cb11e453%40googlegroups.com.
Screen Shot 2020-04-18 at 2.25.17 PM.png

Dick Visser

unread,
Apr 18, 2020, 5:57:36 PM4/18/20
to ansible...@googlegroups.com
What does 'ansible --version' say?

Kiran Kumar

unread,
Apr 19, 2020, 2:29:10 AM4/19/20
to Ansible Project
Thanks Abhijeet ! 
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Kiran Kumar

unread,
Apr 19, 2020, 2:32:04 AM4/19/20
to Ansible Project
Use https://onlineyamltools.com/validate-yaml

This will help you validate the syntax 

Tony Wong

unread,
Apr 19, 2020, 11:39:16 AM4/19/20
to ansible...@googlegroups.com
tony@ubuntu:~/ansiblework$ ansible --version
ansible 2.8.2
  config file = /home/tony/ansiblework/ansible.cfg
  configured module search path = [u'/home/tony/ansiblework/library']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.16 (default, Apr  9 2019, 04:50:39) [GCC 8.3.0]

Tony Wong

unread,
Apr 19, 2020, 3:13:26 PM4/19/20
to Ansible Project

Screen Shot 2020-04-19 at 12.10.46 PM.png



tony@ubuntu:~/ansiblework$ ansible-playbook vmware_tools.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to be in '/home/tony/ansiblework/vmware_tools.yml': line 7, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - name: get info about the virtual machine
    ^ here

Stefan Hornburg (Racke)

unread,
Apr 19, 2020, 3:48:03 PM4/19/20
to ansible...@googlegroups.com
On 4/19/20 9:13 PM, Tony Wong wrote:
> Screen Shot 2020-04-19 at 12.10.46 PM.png
>
>
>
> tony@ubuntu:~/ansiblework$ ansible-playbook vmware_tools.yml
>  [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
>
> ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
>
> The error appears to be in '/home/tony/ansiblework/vmware_tools.yml': line 7, column 5, but may
> be elsewhere in the file depending on the exact syntax problem.
>
> The offending line appears to be:
>
>   tasks:
>   - name: get info about the virtual machine
>     ^ here
>

The task need to be indented:

tasks:
- name: get info about the virtual machine

Regards
Racke

>
>
> On Saturday, April 18, 2020 at 11:32:04 PM UTC-7, Kiran Kumar wrote:
>
> Use https://onlineyamltools.com/validate-yaml <https://onlineyamltools.com/validate-yaml>
> <https://groups.google.com/d/msgid/ansible-project/3000e6a2-0db0-4bd5-8fba-ec114f5240e4%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/672cc33a-758f-4083-a48b-0882cb11e453%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/672cc33a-758f-4083-a48b-0882cb11e453%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/7fafb13d-25b3-4faf-8a67-8029faefc633%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7fafb13d-25b3-4faf-8a67-8029faefc633%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Tony Wong

unread,
Apr 19, 2020, 4:51:03 PM4/19/20
to ansible...@googlegroups.com
it is already indented

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/921f762c-f305-a8a4-98fd-77789c515fec%40linuxia.de.

David Foley

unread,
Apr 19, 2020, 4:54:38 PM4/19/20
to Ansible Project
Can you Install Ansible 2.9 on a Test Machine and Retest on this version ? 


Tony Wong

unread,
Apr 19, 2020, 5:06:58 PM4/19/20
to ansible...@googlegroups.com
doesnt look like i can do it from apt on ubuntu 18

I need ubuntu 19 for ansible 2.9?

On Sun, Apr 19, 2020 at 1:54 PM David Foley <david...@mycit.ie> wrote:
Can you Install Ansible 2.9 on a Test Machine and Retest on this version ? 


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

David Foley

unread,
Apr 19, 2020, 5:14:55 PM4/19/20
to Ansible Project
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

No you don't need to have the latest Ubuntu 19 
 

Tony Wong

unread,
Apr 19, 2020, 5:55:12 PM4/19/20
to ansible...@googlegroups.com
tony@ubuntu:~/ansiblework$ sudo apt-add-repository ppa:ansible/ansible
 Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/
 More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Hit:1 https://download.docker.com/linux/ubuntu bionic InRelease
Hit:2 http://ppa.launchpad.net/ansible/ansible/ubuntu cosmic InRelease
Ign:3 http://archive.ubuntu.com/ubuntu cosmic InRelease
Ign:4 http://archive.ubuntu.com/ubuntu cosmic-updates InRelease
Ign:5 http://archive.ubuntu.com/ubuntu cosmic-backports InRelease
Ign:6 http://archive.ubuntu.com/ubuntu cosmic-security InRelease
Err:7 http://archive.ubuntu.com/ubuntu cosmic Release
  404  Not Found [IP: 91.189.88.152 80]
Err:8 http://archive.ubuntu.com/ubuntu cosmic-updates Release
  404  Not Found [IP: 91.189.88.152 80]
Err:9 http://archive.ubuntu.com/ubuntu cosmic-backports Release
  404  Not Found [IP: 91.189.88.152 80]
Err:10 http://archive.ubuntu.com/ubuntu cosmic-security Release
  404  Not Found [IP: 91.189.88.152 80]
Reading package lists... Done
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-updates Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-backports Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-security Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
tony@ubuntu:~/ansiblework$ sudo apt-get update
Hit:1 https://download.docker.com/linux/ubuntu bionic InRelease
Ign:2 http://archive.ubuntu.com/ubuntu cosmic InRelease
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu cosmic InRelease
Ign:4 http://archive.ubuntu.com/ubuntu cosmic-updates InRelease
Ign:5 http://archive.ubuntu.com/ubuntu cosmic-backports InRelease
Ign:6 http://archive.ubuntu.com/ubuntu cosmic-security InRelease
Err:7 http://archive.ubuntu.com/ubuntu cosmic Release
  404  Not Found [IP: 91.189.88.142 80]
Err:8 http://archive.ubuntu.com/ubuntu cosmic-updates Release
  404  Not Found [IP: 91.189.88.142 80]
Err:9 http://archive.ubuntu.com/ubuntu cosmic-backports Release
  404  Not Found [IP: 91.189.88.142 80]
Err:10 http://archive.ubuntu.com/ubuntu cosmic-security Release
  404  Not Found [IP: 91.189.88.142 80]
Reading package lists... Done
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-updates Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-backports Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'http://archive.ubuntu.com/ubuntu cosmic-security Release' no longer has a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Apr 20, 2020, 4:30:09 AM4/20/20
to ansible...@googlegroups.com
You are using vmware_guest_info module, which was introduced in
ansible 2.9: https://docs.ansible.com/ansible/latest/modules/vmware_guest_info_module.html

On that page it also says that the module used to be called
vmware_guest_facts prior to 2.9.

Given that you have difficulty upgrading, try to use the old module name?
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkq8tcayQrMEKM3f2xBV9%3DxBrdOr%3D0s58ERhBMhto2iNgA%40mail.gmail.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Tony Wong

unread,
Apr 22, 2020, 10:42:05 AM4/22/20
to ansible...@googlegroups.com
I just upgraded ansible to 2.9

but still getting this error



Screen Shot 2020-04-22 at 7.41.18 AM.png

Dick Visser

unread,
Apr 22, 2020, 10:49:23 AM4/22/20
to ansible...@googlegroups.com
You continue to get that error because you continue to mess up the indentation.
Sort that first.

https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkrwWVr6i6SUBbAir9iNtYFcxUjAjiBqEFGOMFGwwa6AXg%40mail.gmail.com.

Tony Wong

unread,
Apr 22, 2020, 11:25:28 AM4/22/20
to ansible...@googlegroups.com
I dont understand where am I getting the ident issue?

Screen Shot 2020-04-22 at 8.24.26 AM.png

Abhijeet Kasurde

unread,
Apr 22, 2020, 11:44:21 AM4/22/20
to ansible...@googlegroups.com
I attached a correct playbook.



--
Thanks,
Abhijeet Kasurde
Screenshot 2020-04-22 at 9.12.52 PM.png

Tony Wong

unread,
Apr 22, 2020, 11:55:16 AM4/22/20
to ansible...@googlegroups.com
Screen Shot 2020-04-22 at 8.54.15 AM.png

David Foley

unread,
Apr 22, 2020, 12:00:26 PM4/22/20
to Ansible Project
Still ident While Fixing The Last Ident Issue you seem to have moved everything to the Left: 

Abhijeet Kasurde Just attached the Correct Way of Doing It;

I would Recommend Reading / Watching Some Ansible Videos before taken on some Projects: you may end-up doing more harm then good within your Environment  

Tony Wong

unread,
Apr 22, 2020, 12:01:45 PM4/22/20
to ansible...@googlegroups.com
my ansible version

tony@ubuntu:~/ansiblework$ ansible --version
ansible 2.9.0

  config file = /home/tony/ansiblework/ansible.cfg
  configured module search path = [u'/home/tony/ansiblework/library']
  ansible python module location = /home/tony/.local/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Apr 15 2020, 17:20:14) [GCC 7.5.0]

Tony Wong

unread,
Apr 22, 2020, 12:09:32 PM4/22/20
to ansible...@googlegroups.com
ok I got it now. 

thank you

but where is my info for the vm?


Screen Shot 2020-04-22 at 9.08.40 AM.png

David Foley

unread,
Apr 22, 2020, 12:39:38 PM4/22/20
to Ansible Project
You need to debug out the message

Tony Wong

unread,
Apr 22, 2020, 1:00:49 PM4/22/20
to ansible...@googlegroups.com
ok got it




On Wed, Apr 22, 2020 at 9:39 AM David Foley <david...@mycit.ie> wrote:
You need to debug out the message

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Tony Wong

unread,
Apr 22, 2020, 1:09:00 PM4/22/20
to ansible...@googlegroups.com
hi to run this for multiple machines, do i need to put it in a vars file?

Tony Wong

unread,
Apr 22, 2020, 1:13:34 PM4/22/20
to ansible...@googlegroups.com
getting and warning 
Screen Shot 2020-04-22 at 10.12.36 AM.png

David Foley

unread,
Apr 22, 2020, 1:16:33 PM4/22/20
to Ansible Project
You need to do loop on the var

Tony Wong

unread,
Apr 22, 2020, 1:21:00 PM4/22/20
to ansible...@googlegroups.com
I am finding switching from powercli to ansible for vmware tough. any benefits to using ansible vs powercli?

On Wed, Apr 22, 2020 at 10:16 AM David Foley <david...@mycit.ie> wrote:
You need to do loop on the var

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Apr 22, 2020, 1:34:12 PM4/22/20
to ansible...@googlegroups.com
With all due respect, this is a support mailing list, not a chat room.
I will suggest to spend some time doing research first, before firing
off a ton of questions that could have been answered by Googling for
10 seconds.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkriVo2%2BSp0DZpH8aGV7Bkxs_b-rRjmius4J1LF7y5xvxg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages