0

I am setting up a vmware job in Ansible Tower to snapshot a list of VM's, ideally, this list should be generated by AWX/Tower from the vSphere dynamic inventory. Inventory is named "lab_vm" in AWX and use either the hostname or the UUID of the VM.

How do I pass this through in my playbook variables file?

---
vars:
  vmware:
    host: '{{ lookup("env", "VMWARE_HOST") }}'
    username: '{{ lookup("env", "VMWARE_USER") }}'
    password: '{{ lookup("env", "VMWARE_PASSWORD") }}'
  vcenter_datacenter: "dc1"
  vcenter_validate_certs: false
  vm_name: "EVE-NG"
  vm_template: "Win2019-Template"
  vm_folder: "Network Labs"

my playbook

---
- name: vm snapshot
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: create snapshot
      vmware_guest_snapshot:
#        hostname: "{{ host }}"
#        username: "{{ user }}"
#        password: "{{ password }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: False
        name: "{{ vm_name }}"
        state: present
        snapshot_name: "Ansible Managed Snapshot"
        folder: "{{ vm_folder }}"
        description: "This snapshot is created by Ansible Playbook"
Jack
  • 5,801
  • 1
  • 15
  • 20
bakesale
  • 3
  • 3
  • You cannot specify an inventory in a playbook, you either pass it in the command line, either configure it in a `ansible.cfg`. The only thing you can do in a playbook is to use `add_host`, but from the look of it, your are more looking for a dynamic inventory than a `add_host`. https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/vmware_inventory.html – β.εηοιτ.βε Jun 14 '22 at 19:30
  • Related: https://stackoverflow.com/questions/47681937/ansible-specify-inventory-file-inside-playbook – β.εηοιτ.βε Jun 14 '22 at 19:30
  • Other possibly related: https://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html – β.εηοιτ.βε Jun 14 '22 at 19:32
  • That would normally be fine. But this module requires me to specify the name of the VM. My inventory is a separate file, maintained by AWX, named "lab_vm" https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_snapshot_module.html#ansible-collections-community-vmware-vmware-guest-snapshot-module So if you understand the nature of my question. How can it be possible for the playbook to call up the lookup to an inventory from AWX in the "name" parameter of this plugin – bakesale Jun 14 '22 at 20:30

1 Answers1

0

You're going about it backward. Ansible loops through the inventory for you. Use that feature, and delegate the task to localhost:

---
- name: vm snapshot
  hosts: all
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: create snapshot
      vmware_guest_snapshot:
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: False
        name: "{{ inventory_hostname }}"
        state: present
        snapshot_name: "Ansible Managed Snapshot"
        folder: "{{ vm_folder }}"
        description: "This snapshot is created by Ansible Playbook"
      delegate_to: localhost

I've not used this particular module before, but don't your want snapshot_name to be unique for each guest?

Jack
  • 5,801
  • 1
  • 15
  • 20
  • I would love for every snapshot name to be unique. I'm unsure of how that could be achieved. – bakesale Jun 15 '22 at 02:54
  • Just set `snapshot_name: "Ansible_Managed_Snapshot_{{ inventory_hostname }}_{{ lookup('pipe', 'date +\"%Y%m%d%H%M%S\"') }}"` The hostname and the DTS should be fine. – Jack Jun 15 '22 at 17:27
  • youre my hero. obviously i need to learn YAML. – bakesale Jun 16 '22 at 20:35