# Replace a network\_name for a VM using var file?

**URL:** <https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056>\
**Category:** Get Help\
**Tags:** vmware\
**Created:** [May 29, 2024, 11:47am UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056 "2024-05-29T11:47:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hammer1810](https://avatars.discourse-cdn.com/v4/letter/h/b4bc9f/32.png) [@hammer1810](https://forum.ansible.com/u/hammer1810)\
**Post date:** [May 29, 2024, 11:47am UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/1 "2024-05-29T11:47:09Z")

</div>

Hi,

I need to change network\_name for vm servers in vmware.

I have a file named vlan.yml contain:

vlan:  
“name1” : “name2”  
“name3” : “name4”

On my playbook I use the following code

- name: Include vars of vlan.yaml into the ‘vlan’  
ansible.builtin.include\_vars:  
file: vlan.yml  
name: vlan

- name: Change Network Vlan  
community.vmware.vmware\_guest\_network:  
hostname: “----”  
username: “----”  
password: “----”  
datacenter: “----”  
folder: “/----/”  
validate\_certs: no  
name: “-----”  
networks:  
- state: present  
label: “Network adapter 1”  
network\_name: “{{ vlan.vlan.[network\_info[0].name] }}”  
delegate\_to: localhost

I would like: if the network\_name is “name1” become “name2” - if it’s “name3” become “name4”.

Thank you for your help,

Ham

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 29, 2024, 12:34pm UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/2 "2024-05-29T12:34:07Z")

</div>

Hi  
You should use [conditionals](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html) for your task like a `when:`  
to execute your task “when” the condition is true.

In your case, i’d use a when: on network name, and so, if network name is network 1 so my task will update it with value network2 as wanted.

2nd solution if you’re able to retrieve facts (i assume vmware collection does) so maybe you can use a when: on a specific facts (i.e : network name : XXX)

---

<div class="post-metadata">

**Author:** ![hammer1810](https://avatars.discourse-cdn.com/v4/letter/h/b4bc9f/32.png) [@hammer1810](https://forum.ansible.com/u/hammer1810)\
**Post date:** [May 29, 2024, 1:10pm UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/3 "2024-05-29T13:10:30Z")

</div>

the idea was to have source and target in a file because I have more thant 60 vlan to migrate.

I’ll try the ‘when’ command…

Thanks

Ham

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 29, 2024, 1:34pm UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/4 "2024-05-29T13:34:03Z")

</div>

If you put your code in a “tripple-back-tick” block, it would be much easier to read.

```yaml
- name: Include vars of vlan.yaml into the 'vlan'
  ansible.builtin.include_vars:
    file: vlan.yml
    name: vlan

- name: Change Network Vlan
  community.vmware.vmware_guest_network:
    hostname: "----"
    username: "----"
    password: "----"
    datacenter: "----"
    folder: "/----/"
    validate_certs: no
    name: "-----"
    networks:
      - state: present
        label: "Network adapter 1"
        network_name: "{{ vlan.vlan.[network_info[0].name] }}"
  delegate_to: localhost

```

So, presumably `network_info[0].name` is only one of about 60 you need to operate on. Do you have all these in some data structure you can loop over?

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 29, 2024, 1:56pm UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/5 "2024-05-29T13:56:15Z")

</div>

For a VLAN migration, it’s maybe easier to do it through PowerCLI, especially if you have to correlate old\_vlan =\> new vlan if you got a lot.

Or as @utoddl said, to loop over within your ansible task.

Both works.

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [May 29, 2024, 5:11pm UTC](https://forum.ansible.com/t/replace-a-network-name-for-a-vm-using-var-file/6056/6 "2024-05-29T17:11:12Z")

</div>

> [@motorbass](#):
>
> For a VLAN migration, it’s maybe easier to do it through PowerCLI

I agree. Ansible works best if you use it as an infastructure-as-code tool (which it basically is). I also prefer PowerCLI for things that I want do do once, like migrating VMs to another portgroup or similar.

Besides, I think ansible isn’t well suited for any `if-then-else` logic. You can implement it somehow, but it usually feels wrong to me if I do.
