简体   繁体   中英

Ansible: How to use multiple vCenter?

I use Ansible to take snapshots of VM in VMware vCenter.

If vcenter only one, then it works fine, but what if there are many vcenter?

How to describe multiple Vmware vCenter in the current role?

My current example for one vcenter:

# cat snapshot.yml

- name: ROLE_update
   hosts: all
   gather_facts: False
   become: yes
   become_method: su
   become_user: root
   vars_files:
     -  /ansible/vars/privileges.txt
   roles:
    - role: snapshot

# cat tasks/main.yml

- block:
    - name: "Create vmware snapshot of {{ inventory_hostname }}"
      vmware_guest_snapshot:
        hostname: "{{vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{datacenter_name}}"
        folder: find_folder.yml
        name: "{{inventory_hostname }}"
        state: present
        snapshot_name: snap check
        validate_certs: False
      delegate_to: localhost
      register: vm_facts

# cat find_folder.yml

- name: Find folder path of an existing virtual machine
  hosts: all
  gather_facts: False
  vars:
    ansible_python_interpreter: "/usr/bin/env python3"
  tasks:

    - name: "Find folder for VM - {{ vm_name }}"
      vmware_guest_find:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ datacenter_name }}"
        validate_certs: False
        name: "{{ vm_name }}"
      delegate_to: localhost
      register: vm_facts

# cat vars/main.yml variables declared

vcenter_hostname: <secret>
vcenter_username: <secret>
vcenter_password: <secret>

Thanks!

Is having the variables for the different vcenter in different ".yml" files an option? If so, you could easily call for those variables at runtime execution like:

ansible-playbook your_playbook.yml --extra-vars "your_vcenter_1.yml"

There are other ways i could think of, like using tags in your playbook for each task, and invoke your playbook using a specific tag. On each task with the different tags, on the import_role, you could define the new variables like this:

tasks:
- import_tasks: your_role.yml
  vars:
   vcenter_hostname: your_vcenter_1
  tags:
    - vcenter1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM