Ansible With_Item as Variable

30 views
Skip to first unread message

David Foley

unread,
Apr 14, 2020, 11:53:43 AM4/14/20
to Ansible Project
Hi All,

Could do with some Help with this issue:

I am trying to get the Powerstate of a Virtual Machine and power off when powerstate != poweroff
and looking at a way to copy the copy the Cluster variable to a Text File:

---
    - name: Running Create Virtual Machine Playbook
      hosts: localhost
      gather_facts: false
      connection: local
      tasks:
      - name: Getting Info from Virtual Machine
        vmware_vm_info:
          validate_certs: no
          hostname: "{{ vcenter }}"
          username: "{{ user }}"
          password: "{{ password }}"
          folder: "/"
        delegate_to: localhost
        register: vminfo
      - debug:
          msg: "{{ item.power_state }}"
        with_items:
          - "{{ vm_info.virtual_machines | json_query(query) }}"
        vars:
          query: "[?guest_name=='win201_old']"
          
       - vmware_guest_powerstate:
           validate_certs: no
           hostname: "{{ vcenter }}"
           username: "{{ user }}"
           password: "{{ password }}"
           name: win201_old
           state: powered-off
         delegate_to: localhost
         when: "{{ item.power_state }}" != "poweredOff"

       - name: Copy Variable
         copy:
          content: '{{ item.cluster }}'
          dest: /srv/ansible/test.txt 
       delegate_to: localhost
Reply all
Reply to author
Forward
0 new messages