cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
281
Views
0
Helpful
9
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
         }
9 Replies 9

roxyfoxtrotter
Level 1
Level 1

any idea? I looked at the chrome developer tools under network and tried

"Cdn": {
     "Source": "vnic"
     "Value": "{{ vnic_template_fabA }}"
}

but its not working

There's an example of creating a vnic template and attaching to policies at https://github.com/CiscoDevNet/intersight-ansible/blob/main/playbooks/intersight_vnic_template.yml. The bulk API resource needs to be used to attach templates to policies.

does this create vnics if they are missing? I got my playbook to create the vnic templates but I also need to create vnics from vmnic0-vmnic7

Yes, the vnic_name in the playbook will be created if it doesn't already exist.

I am not sure why the task to creaste the vnics is getting skipped. Here is my modified playbook. the vnic templates get created but not the missing vnics: vmnic0-vmnic7

- name: "Configure vNIC Templates"
  vars:
    api_info: &api_info_vnic
      api_private_key: "{{ api_private_key }}"
      api_key_id: "{{ api_key_id }}"
      api_uri: "{{ api_uri | default(omit) }}"
      validate_certs: "{{ validate_certs | default(omit) }}"
      state: "{{ state | default('present') }}"

  block:
    - name: "Configure vNIC Templates in Intersight"
      cisco.intersight.intersight_rest_api:
        <<: *api_info_vnic
        resource_path: "/vnic/VnicTemplates"
        query_params:
          $filter: "Name eq '{{ item.name }}'"
        api_body:
          Name: "{{ item.name }}"
          Organization:
            Moid: "{{ intersight_org.api_response.Moid }}"
          Description: "vNIC Template for {{ item.name }}"
          Cdn:
            Source: "vnic"
          EnableOverride: false
          EthAdapterPolicy:
            Moid: "{{ ethernet_adapter_policy_details.api_response.Moid }}"
          EthQosPolicy:
            Moid: "{{ ethernet_qos_policy_details.api_response.Moid }}"
          FabricEthNetworkControlPolicy:
            Moid: "{{ ethernet_network_control_policy_details.api_response.Moid }}"
          FabricEthNetworkGroupPolicy:
            - Moid: "{{ item.eth_network_group_policy }}"
          FailoverEnabled: false
          MacPool:
            Moid: "{{ item.mac_pool }}"
          SwitchId: "{{ item.switch_id }}"
          Tags:
            - Key: "configmode"
              Value: "ansible"
            - Key: "prefix"
              Value: "{{ prefix }}"
      loop: "{{ vnic_templates }}"
      loop_control:
        label: "{{ item.name }}"
      register: template_resp

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

    - name: "Get Moid of vNICs by Name"
      cisco.intersight.intersight_rest_api:
        <<: *api_info_vnic
        resource_path: /vnic/EthIfs
        query_params:
          $filter: "Name eq '{{ item.vnic }}'"
      loop: "{{ vnic_templates }}"
      loop_control:
        label: "{{ item.vnic }}"
      register: vnic_resp

    - name: "Debug vnic_resp"
      debug:
        var: vnic_resp

# Derive profiles from template (if profiles don't already exist)
    - name: "POST to derive {{ item.vnic }}"
      intersight_rest_api:
        resource_path: /bulk/MoCloners
        update_method: post
        api_body: |
          {
            "Organization": {
              "Moid": "{{ intersight_org.api_response.Moid }}"
            },
            "Sources": [
              {
                "ClassId": "mo.MoRef",
                "ObjectType": "vnic.VnicTemplate",
                "Moid": "{{ template_resp.api_response.Moid }}"
              }
            ],
            "Targets": [
              {
                "Name": "{{ item.vnic }}",
                "LanConnectivityPolicy": "{{ lan_resp.api_response.Moid }}",
                "MacAddressType": "POOL",
                "ObjectType": "vnic.EthIf",
                "Placement": {},
                "ClassId": "vnic.EthIf"
              }
            ]
          }
      when: vnic_resp is not defined or vnic_resp.results is not defined or vnic_resp.results | length == 0 or vnic_resp.results[0].api_response is not defined
      loop: "{{ vnic_templates }}"
      loop_control:
        label: "{{ item.vnic }}"

 

i ran the playbook as a standalone but got this error :

 

fatal: [localhost]: FAILED! => {
"msg": "The conditional check 'vnic_resp.api_response is not defined or not vnic_resp.api_response.Results' failed. The error was: error while evaluating conditional (vnic_resp.api_response is not defined or not vnic_resp.api_response.Results): 'dict object' has no attribute 'Results'\n\nThe error appears to be in 'create_vnics.yml': line 100, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n # POST to derive vNIC '{{ vnic_name }}'\n - name: \"POST to derive vNIC '{{ vnic_name }}'\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"
}

this is the playbook I copied from the github repo but I keep getting error on the create vnic task

---
- name: "Configure vNIC from Template"
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
    api_info: &api_info
      api_private_key: "ansible-automation-SecretKey.txt"
      api_key_id: "xxxxxxxxxx
      api_uri: "https://cva"
      validate_certs: false
      state: present

    prefix: "DC1"
    org_name: "my_org"
    template_name: "name_of_vnic_template_A"
    lan_connectivity_policy: "fc_lan_connectivity_policy"
    vnic_name: "vnic0"
    mac_pool: "{{ prefix }}-Mac-Pool-A"
    network_group: "{{ prefix }}-vSwitch0-Policy"
    network_control_policy: "{{ prefix }}-Enable-CDP-LLDP-Policy"
    qos_policy: "{{ prefix }}-EthernetQoS-Policy"
    ethernet_adapter_policy: "{{ prefix }}-EthAdapter-VMware-Policy"

  tasks:
    - name: "Get Organization Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /organization/Organizations
        query_params:
          $filter: "Name eq '{{ org_name }}'"
      register: org_resp

    - name: "Get vNIC Template Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /vnic/VnicTemplates
        query_params:
          $filter: "Name eq '{{ template_name }}'"
      register: template_resp

    - name: "Check if FC vNIC '{{ vnic_name }}' exists"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /vnic/EthIfs
        query_params:
          $filter: "Name eq '{{ vnic_name }}'"
      register: vnic_resp

    - name: "Get MAC Pool Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /macpool/Pools
        query_params:
          $filter: "Name eq '{{ mac_pool }}'"
      register: mac_resp

    - name: "Get Network Group Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /fabric/EthNetworkGroupPolicies
        query_params:
          $filter: "Name eq '{{ network_group }}'"
      register: network_resp

    - name: "Get Network Control Policy Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /fabric/EthNetworkControlPolicies
        query_params:
          $filter: "Name eq '{{ network_control_policy }}'"
      register: control_resp

    - name: "Get QoS Policy Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /vnic/EthQosPolicies
        query_params:
          $filter: "Name eq '{{ qos_policy }}'"
      register: qos_resp

    - name: "Get Ethernet Adapter Moid"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /vnic/EthAdapterPolicies
        query_params:
          $filter: "Name eq '{{ ethernet_adapter_policy }}'"
      register: adapter_resp

    # Check if vNIC exists
    - name: "Get {{ vnic_name }} Moid"
      cisco.intersight.intersight_rest_api:
          <<: *api_info
          resource_path: /vnic/EthIfs
          query_params:
            $filter: "Name eq '{{ vnic_name }}'"
      register: vnic_resp

    - 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: "POST to derive vNIC '{{ vnic_name }}'"
      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": "{{ vnic_name }}",
                "LanConnectivityPolicy": "{{ lan_resp.api_response.Moid }}",
                "MacAddressType": "POOL",
                "ObjectType": "vnic.EthIf",
                "Placement": {},
                "ClassId": "vnic.EthIf"
              }
            ]
          }
      when: vnic_resp.api_response is not defined or not vnic_resp.api_response

    - name: "POST to update vNIC '{{ vnic_name }}'"
      cisco.intersight.intersight_rest_api:
        <<: *api_info
        resource_path: /bulk/MoMergers
        update_method: post
        api_body: {
          "Sources": [
            {
              "ObjectType": "vnic.VnicTemplate",
              "Moid": "{{ template_resp.api_response.Moid }}"
            }
          ],
          "Targets": [
            {
              "ObjectType": "vnic.EthIf",
              "Moid": "{{ vnic_resp.api_response.Moid }}"
            }
          ],
          "MergeAction":"Replace"
        }
      when: vnic_resp.api_response and vnic_resp.changed

 

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 107, 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: \"POST to derive vNIC '{{ vnic_name }}'\"\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"
}

Looks like the conditions in the example won't work in all cases and the query for an existing vnic needs to be more specific.  I'll work on an updated example with fixes.

dsoper
Cisco Employee
Cisco Employee

I've updated the example at https://github.com/CiscoDevNet/intersight-ansible/blob/main/playbooks/intersight_vnic_template.yml with a more specific query and a list of vnics to create or update from a template.

Review Cisco Networking for a $25 gift card