when tools status "guestToolsNotRunning" , power off the vm.

19 views
Skip to first unread message

Kiran Kumar

unread,
Apr 9, 2020, 3:22:59 AM4/9/20
to Ansible Project
Please suggest how can i add when clause in above, ie when tools status "guestToolsNotRunning" , power off the vm.


- hosts: localhost
  gather_facts: false
  connection: local
  vars_files:
    - ./vars/vmware_vars.yml
  vars:
    vm_list:
      - vm1
  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
      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 9, 2020, 4:27:18 AM4/9/20
to ansible...@googlegroups.com
Hi Kiran,

Do you want to run this operation on specific VMs or all the VMs which are "guestToolsNotRunning" status?



--
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/efe1317e-1af2-4f6b-b82b-1dd5866d0233%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Abhijeet Kasurde

unread,
Apr 9, 2020, 7:00:16 AM4/9/20
to ansible...@googlegroups.com
Hi Kiran,

I did something like this -

---
- hosts: localhost
vars_files:
- vcenter_vars.yml
collections:
- community.vmware
- community.general
vars:
vm_list:
- debian_10_clone
- my_clone_deb_1
- test01
tasks:
- name: Get all tags
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: Tools status
set_fact:
desired_vms: "{{ vm_info.results | community.general.json_query(jmesquery) }}"
vars:
jmesquery: "[?instance.guest_tools_status == 'guestToolsNotRunning'].instance.hw_name"

- debug:
msg: "{{ desired_vms }}"

--
Thanks,
Abhijeet Kasurde

Kiran Kumar

unread,
Apr 9, 2020, 1:03:58 PM4/9/20
to Ansible Project
Hi Abhijit

Thanks for your reply, Only Specific VMs where the status is guestToolsNotRunning

Best
Kiran

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 9, 2020, 1:06:24 PM4/9/20
to Ansible Project
Hi Abhijit

Thanks for your reply. I did as per you note,  How can i check the output & also then how can i use that in action block ie shutdown action. 


---

- 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
      debug:
        msg: "{{ vm_info.results  | json_query(jmesquery) }}"
      vars:
        jmesquery: "[?instance.hw_name == '{{ item }}'].instance.guest_tools_status"
      with_items: "{{ vm_list }}"

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



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


--
Thanks,
Abhijeet Kasurde


--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages