简体   繁体   中英

VMware VM Portgroup change with Ansible 2.9.6

My script doesn't change the portgroup of a VM Network adapter, what am I doing wrong ?

Let's assume that I want to change the current portgroup named "A" to a different portgroup named "B".

---
- hosts: localhost
  gather_facts: no
  vars:
    vm_name: VM
  tasks:
  - name: Changing Portgroup for Network adapter 1
    vmware_guest_network:
      hostname: "{{ vc_host }}"
      username: "{{ vc_user }}"
      password: "{{ vc_pass }}"
      validate_certs: no
      name: "{{ vm_name }}"
      gather_network_info: false
      networks:
      - label: "Network adapter 1"
        name: "B"
        state: present
    delegate_to: localhost
    register: network_info 

I'm getting output that something changed, but in VM Settings nothing changed.

TASK [Changing Portgroup for Network Adapter 1] 
******************************************************************************
changed: [localhost -> localhost] 

I found that removing and adding a Network adapter changes the portgroup, but when I do that I cannot add a Network adapter type Flexible which I had in the first place.

Edit1: After updating Ansible to 2.9.12 I get OK output when running the script, so it really isn't changing anything.

TASK [Changing Portgroup for Network Adapter 1] ******************************************************************************
ok: [localhost]

Edit2: After a few days searching I found that it isn't possible to just change the portgroup with Ansible, so I used PowerCLI to help me with task.

---
- hosts: localhost
  gather_facts: no
  vars:
    vm_name: "VM"
  tasks:
  - name: "Changing the portgroup for {{ vm_name }}"
    win_command: 'powershell.exe -ExecutionPolicy ByPass -File C:\Scripts\change_portgroup.ps1 {{ vm_name }}'
    delegate_to: WIN_SRV

With powershell script going like this:

$OldNetwork = "PG old"
$NewNetwork = "PG new"

Get-VM -Name $args[0] |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

Edit 3: I got it working with vmware community module. (Thanks @sky-jokerxx)

First I installed it with command:

ansible-galaxy collection install community.vmware

Then used the module like this:

  - name: Change network
    community.vmware.vmware_guest_network:
      validate_certs: no
      hostname: '{{ vc_host }}'
      username: '{{ vc_user }}'
      password: '{{ vc_pass }}'
      name: '{{ vm_name }}'
      label: "Network adapter 1"
      network_name: "B"
      state: present
    delegate_to: localhost

The vmware_guest_network module has a lot of issues.

https://github.com/ansible-collections/community.vmware/issues/378

The following vmware_guest_network module is fixed some issues.

https://github.com/ansible-collections/community.vmware/pull/401

In the following procedure, you can use the above module.

# ansible-galaxy collection install community.vmware -p collections
# mkdir library
# cd library/
# curl -L https://raw.githubusercontent.com/ansible-collections/community.vmware/7ac9ebb9bf5df0f1ead3ef1a3ed35f2d4ad45622/plugins/modules/vmware_guest_network.py -O
# cd ..

https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html

Maybe it's fixed, so how about trying it?

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