cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
607
Views
0
Helpful
32
Replies

ansible attach vnic template to lan connectivity policy

roxyfoxtrotter
Level 1
Level 1

I have following lan connectivity policy and want to know how to attach a vnic template to it

 

cisco.intersight.intersight_rest_api:
        <<: *api_info_fc
        resource_path: /vnic/EthIfs
        query_params:
           $filter: "Name eq '{{ name_of_fc_vnic_0 }}'"
        api_body: {
             "Name": "{{ name_of_fc_vnic_0 }}",
             "MacAddressType": "POOL",
             "MacPool": "{{ mac_pool_fi_a_details.api_response.Moid }}",
             "Placement": {
                "SwitchId": "A",
                "AutoPciLink": true,
                "AutoSlotId": true,
             },
             "Cdn": {
                "Source": "vnic"
             },
             "FailoverEnabled": false,
             "FabricEthNetworkGroupPolicy": [ "{{ management_network_group_policy.api_response.Moid }}" ],
             "FabricEthNetworkControlPolicy": "{{ ethernet_network_control_policy.api_response.Moid }}",
             "EthQosPolicy": "{{ ethernet_qos_policy.api_response.Moid }}",
             "EthAdapterPolicy": "{{ ethernet_adapter_vmware_policy.api_response.Moid }}",
             "StaticMacAddress": "",
             "LanConnectivityPolicy": "{{ fc_lan_connectivity_policy.api_response.Moid }}",
             "Order": 0
         }
32 Replies 32

TASK [Check if each vNIC exists] ********************************************************************************************************************************
task path: create_vnics.yml:93
fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable.. 'dict object' has no attribute 'Moid'\n\nThe error appears to be in 'create_vnics.yml': line 93, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"Check if each vNIC exists\"\n      ^ here\n"
}

Looks like the lan connectivity policy may not exist in that org so there's no api_response with the lan connectivity policy Moid:

- name: "Get LAN Connectivity Policy Moid"
cisco.intersight.intersight_rest_api:
<<: *api_info
resource_path: /vnic/LanConnectivityPolicies
query_params:
$filter: "Name eq '{{ lan_connectivity_policy }}'"
register: lan_resp

- name: "Check if each vNIC exists"
cisco.intersight.intersight_rest_api:
<<: *api_info
resource_path: /vnic/EthIfs
query_params:
$filter: "Name eq '{{ item }}' and LanConnectivityPolicy.Moid eq '{{ lan_resp.api_response.Moid }}'"
loop: "{{ vnic_names }}"
loop_control:
label: "{{ item }}"
register: vnic_checks

You can run the playbook with -vvv to see all of the api resonses.

dsoper
Cisco Employee
Cisco Employee

What errors are you seeing with the latest playbook?  That's working for me as is in my Intersight account:

ansible-playbook test_vnic.yml

PLAY [Configure vNICs from Template] *******************************************************************************************************************************************************************************************************

TASK [Get Organization Moid] ***************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get MAC Pool Moid] *******************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get Network Group Moid] **************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get Network Control Policy Moid] *****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get QoS Policy Moid] *****************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get Ethernet Adapter Moid] ***********************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get vNIC Template Moid] **************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Get LAN Connectivity Policy Moid] ****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Check if each vNIC exists] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => (item=00-vSwitch0-A)
ok: [localhost] => (item=01-vSwitch0-B)

TASK [POST to derive vNIC if it does not exist] ********************************************************************************************************************************************************************************************
changed: [localhost] => (item=00-vSwitch0-A)
changed: [localhost] => (item=01-vSwitch0-B)

TASK [POST to update vNIC if it exists and template changed] *******************************************************************************************************************************************************************************
skipping: [localhost] => (item=00-vSwitch0-A)
skipping: [localhost] => (item=01-vSwitch0-B)
skipping: [localhost]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
localhost : ok=10 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

TASK [Get LAN Connectivity Policy Moid] *************************************************************************************************************************
task path: create_vnics.yml:85
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: xxx
<127.0.0.1> EXEC /bin/sh -c 'echo ~xxx && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/xxx/.ansible/tmp `"&& mkdir "` echo /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177 `" && echo ansible-tmp-1746110601.914356-31689-165276699845177="` echo /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177 `" ) && sleep 0'
Using module file /opt/homebrew/Cellar/ansible/10.6.0/libexec/lib/python3.12/site-packages/ansible_collections/cisco/intersight/plugins/modules/intersight_rest_api.py
<127.0.0.1> PUT /Users/xxx/.ansible/tmp/ansible-local-31537g5x5g2bl/tmp1dr148h_ TO /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177/AnsiballZ_intersight_rest_api.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177/ /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177/AnsiballZ_intersight_rest_api.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/homebrew/Cellar/ansible/10.6.0/libexec/bin/python /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177/AnsiballZ_intersight_rest_api.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/xxx/.ansible/tmp/ansible-tmp-1746110601.914356-31689-165276699845177/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
    "api_response": {},
    "changed": false,
    "count": null,
    "invocation": {
        "module_args": {
            "api_body": null,
            "api_key_id": "xxxxxxxxx",
            "api_private_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "api_uri": "https://intersight/api/v1",
            "list_body": null,
            "query_params": {
                "$filter": "Name eq 'an_connectivity_policy'"
            },
            "resource_path": "/vnic/LanConnectivityPolicies",
            "return_list": false,
            "state": "present",
            "update_method": "patch",
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "trace_id": "NBad4d989c32b490173f60371ba716e53a"
}

api_response is empty so lan_connectivity_policy: "lan_connectivity_policy" isn't in your account.  The specified lan connectivity policy needs to already exist to create vnic from the template

ok I fixed it but now got another error on this task:

 

 "msg": "Code exception: (400, 'HTTP Error 400: Bad Request', b'{\"ClassId\":\"bulk.MoCloner\",\"Moid\":\"6813b57b99eb5c151ef3ad49\",\"ObjectType\":\"bulk.MoCloner\",\"Responses\":[{\"ObjectType\":\"bulk.RestResult\",\"ClassId\":\"bulk.RestResult\",\"Status\":400,\"Body\":\"{\\\\\"code\\\\\":\\\\\"InvalidRequest\\\\\",\\\\\"message\\\\\":\\\\\"Auto Slot Id is disabled. Slot Id cannot be empty for the vNIC 00-vSwitch0-A.\\\\\",\\\\\"messageId\\\\\":\\\\\"eth_slot_id_empty_no_auto_slot_id\\\\\",\\\\\"messageParams\\\\\":{\\\\\"1\\\\\":\\\\\"00-vSwitch0-A\\\\\"},\\\\\"traceId\\\\\":\\\\\"NBc22f8ab8f2ce82c69209d87ae5a67c36\\\\\"}\"}],\"Sources\":[{\"ClassId\":\"mo.MoRef\",\"ObjectType\":\"vnic.VnicTemplate\",\"Moid\":\"68124a3099eb5c151ef36baf\"}],\"Targets\":[{\"Name\":\"00-vSwitch0-A\",\"LanConnectivityPolicy\":\"68124c4f99eb5c151ef36c6c\",\"MacAddressType\":\"POOL\",\"ObjectType\":\"vnic.EthIf\",\"Placement\":{},\"ClassId\":\"vnic.EthIf\"}]}') "
}

If the PlacementMode on the lan connectivity policy is custom (not auto), there needs to be a Placement attribute in the bulk clone task:

"Placement": {
"AutoPciLink": true,
"AutoSlotId": true,
"ClassId": "vnic.PlacementSettings",
"Id": "",
"ObjectType": "vnic.PlacementSettings",
"PciLink": 0,
"PciLinkAssignmentMode": "Custom",
"SwitchId": "A",
"Uplink": 0
}

- name: "Add vNIC0"
      cisco.intersight.intersight_rest_api:
        <<: *api_info_fc
        resource_path: "/vnic/EthIfs"
        query_params:
          "$filter": "Name eq '{{ name_of_vnic_0 }}'"
        api_body:
          "Name": "{{ name_of_vnic_0 }}"
          "MacAddressType": "POOL"
          "MacPool": "{{ mac_pool_fi_a_details.api_response.Moid }}"
          "Placement":
            "SwitchId": "A"
            "AutoPciLink": true
            "AutoSlotId": true
          "Cdn": { "Source": "vnic" }
          "FailoverEnabled": false
          "FabricEthNetworkGroupPolicy": ["{{ management_network_group_policy.api_response.Moid }}"]
          "FabricEthNetworkControlPolicy": "{{ ethernet_network_control_policy.api_response.Moid }}"
          "EthQosPolicy": "{{ ethernet_qos_policy.api_response.Moid }}"
          "EthAdapterPolicy": "{{ ethernet_adapter_vmware_policy.api_response.Moid }}"
          "StaticMacAddress": ""
          "LanConnectivityPolicy": "{{ lan_connectivity_policy.api_response.Moid }}"
          "Order": 0

This is part of my lan connectivity policy adding nics to the policy

 

do you mean I need to add the rest of the placement values in my lan connectivity pol?

Update the following task where the vnics are created from the template:

- name: "POST to derive vNIC if it does not exist"
cisco.intersight.intersight_rest_api:
<<: *api_info
resource_path: /bulk/MoCloners
update_method: post
api_body: >-
{{
{
"Organization": { "Moid": org_resp.api_response.Moid },
"Sources": [
{
"ClassId": "mo.MoRef",
"ObjectType": "vnic.VnicTemplate",
"Moid": template_resp.api_response.Moid
}
],
"Targets": [
{
"Name": item.item,
"LanConnectivityPolicy": lan_resp.api_response.Moid,
"MacAddressType": "POOL",
"ObjectType": "vnic.EthIf",
"Placement": {
"SwitchId": "A",
"AutoPciLink": true,
"AutoSlotId": true
},
"ClassId": "vnic.EthIf"
}
]
}
}}
when: item.api_response.Moid is not defined
loop: "{{ vnic_checks.results }}"
loop_control:
label: "{{ item.item }}"

You'll need to add those Placement attributes in the bulk/MoCloners task:

"Targets": [
{
"Name": item.item,
"LanConnectivityPolicy": lan_resp.api_response.Moid,
"MacAddressType": "POOL",
"ObjectType": "vnic.EthIf",
"Placement": {},
"ClassId": "vnic.EthIf"
}

ok its running fine now without errors. but the task to attach template to vnic is being skipped

The tasks using bulk/Mo will be skipped if the vnics already exist and there are no changes to the template.  The when conditions in the following tasks control when vnics are created from the template or updated if the template is changed:

# Derive profiles from template (if profiles don't already exist)
- name: "POST to derive vNICs"
intersight_rest_api:
resource_path: /bulk/MoCloners
update_method: post
api_body: {
"Organization": {
"Moid": "{{ org_resp.api_response.Moid }}"
},
"Sources": [
{
"ClassId": "mo.MoRef",
"ObjectType": "vnic.VnicTemplate",
"Moid": "{{ template_resp.api_response.Moid }}"
}
],
"Targets": [
{
"Name": "{{ item.vnic_name }}",
"LanConnectivityPolicy": "{{ lan_resp.api_response.Moid }}",
"MacAddressType": "POOL",
"ObjectType": "vnic.EthIf",
"Placement": {},
"ClassId": "vnic.EthIf"
}
]
}
when: item.api_response.Name is not defined
loop: "{{ vnic_resp.results }}"
loop_control:
label: "{{ item.vnic_name }}"
# POST updates to derived profiles if template was changed
- name: "POST to update vNICs"
intersight_rest_api:
resource_path: /bulk/MoMergers
update_method: post
api_body: {
"Sources": [
{
"ObjectType": "vnic.VnicTemplate",
"Moid": "{{ template_resp.api_response.Moid }}"
}
],
"Targets": [
{
"ObjectType": "vnic.EthIf",
"Moid": "{{ item.api_response.Moid }}"
}
],
"MergeAction":"Replace"
}
when: item.api_response.Name is defined and template_resp.changed
loop: "{{ vnic_resp.results }}"
loop_control:
label: "{{ item.vnic_name }}"

ok how do i modify it to just attach vnic to vnic template? Both already exist

I'll work up an example of attaching an existing vnic to a template.  A browser developer console will show the payloads needed for the bulk/Requests API resource.

Screenshot 2025-05-02 at 10.24.37 AM.png

Review Cisco Networking for a $25 gift card