
    VhO                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZ d d	lmZmZmZ d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Ze dk(  r e        yy)    )absolute_importdivisionprint_functionaL  
module: nxos_vrf
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: (deprecated, removed after 2026-07-25) Manages global VRF configuration.
description:
- This module provides declarative management of VRFs on CISCO NXOS network devices.
version_added: 1.0.0
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
- Trishna Guha (@trishnaguha)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- Unsupported for Cisco MDS
- Cisco NX-OS creates the default VRF by itself. Therefore, you're not allowed to
  use default as I(vrf) name in this module.
- C(vrf) name must be shorter than 32 chars.
- VRF names are not case sensible in NX-OS. Anyway, the name is stored just like it's
  inserted by the user and it'll not be changed again unless the VRF is removed and
  re-created. i.e. C(vrf=NTC) will create a VRF named NTC, but running it again with
  C(vrf=ntc) will not cause a configuration change.
options:
  name:
    description:
    - Name of VRF to be managed.
    aliases:
    - vrf
    type: str
  admin_state:
    description:
    - Administrative state of the VRF.
    default: up
    choices:
    - up
    - down
    type: str
  vni:
    description:
    - Specify virtual network identifier. Valid values are Integer or keyword 'default'.
    type: str
  rd:
    description:
    - VPN Route Distinguisher (RD). Valid values are a string in one of the route-distinguisher
      formats (ASN2:NN, ASN4:NN, or IPV4:NN); the keyword 'auto', or the keyword 'default'.
    type: str
  interfaces:
    description:
    - List of interfaces to check the VRF has been configured correctly or keyword
      'default'.
    type: list
    elements: str
  associated_interfaces:
    description:
    - This is a intent option and checks the operational state of the for given vrf
      C(name) for associated interfaces. If the value in the C(associated_interfaces)
      does not match with the operational state of vrf interfaces on device it will
      result in failure.
    type: list
    elements: str
  aggregate:
    description: List of VRFs definitions.
    type: list
    elements: dict
    suboptions:
      name:
        description:
        - Name of VRF to be managed.
        aliases:
        - vrf
        type: str
      admin_state:
        description:
        - Administrative state of the VRF.
        choices:
        - up
        - down
        type: str
      vni:
        description:
        - Specify virtual network identifier. Valid values are Integer or keyword 'default'.
        type: str
      rd:
        description:
        - VPN Route Distinguisher (RD). Valid values are a string in one of the route-distinguisher
          formats (ASN2:NN, ASN4:NN, or IPV4:NN); the keyword 'auto', or the keyword 'default'.
        type: str
      interfaces:
        description:
        - List of interfaces to check the VRF has been configured correctly or keyword
          'default'.
        type: list
        elements: str
      associated_interfaces:
        description:
        - This is a intent option and checks the operational state of the for given vrf
          C(name) for associated interfaces. If the value in the C(associated_interfaces)
          does not match with the operational state of vrf interfaces on device it will
          result in failure.
        type: list
        elements: str
      state:
        description:
        - Manages desired state of the resource.
        choices:
        - present
        - absent
        type: str
      description:
        description:
        - Description of the VRF or keyword 'default'.
        type: str
      delay:
        description:
        - Time in seconds to wait before checking for the operational state on remote
          device. This wait is applicable for operational state arguments.
        type: int
  purge:
    description:
    - Purge VRFs not defined in the I(aggregate) parameter.
    type: bool
    default: false
  state:
    description:
    - Manages desired state of the resource.
    default: present
    choices:
    - present
    - absent
    type: str
  description:
    description:
    - Description of the VRF or keyword 'default'.
    type: str
  delay:
    description:
    - Time in seconds to wait before checking for the operational state on remote
      device. This wait is applicable for operational state arguments.
    default: 10
    type: int
a  
- name: Ensure ntc VRF exists on switch
  cisco.nxos.nxos_vrf:
    name: ntc
    description: testing
    state: present
- name: Aggregate definition of VRFs
  cisco.nxos.nxos_vrf:
    aggregate:
      - name: test1
        description: Testing
        admin_state: down
      - name: test2
        interfaces: Ethernet1/2
- name: Aggregate definitions of VRFs with Purge
  cisco.nxos.nxos_vrf:
    aggregate:
      - name: ntc1
        description: purge test1
      - name: ntc2
        description: purge test2
    state: present
    purge: true
- name: Delete VRFs exist on switch
  cisco.nxos.nxos_vrf:
    aggregate:
      - name: ntc1
      - name: ntc2
    state: absent
- name: Assign interfaces to VRF declaratively
  cisco.nxos.nxos_vrf:
    name: test1
    interfaces:
      - Ethernet2/3
      - Ethernet2/5
- name: Check interfaces assigned to VRF
  cisco.nxos.nxos_vrf:
    name: test1
    associated_interfaces:
      - Ethernet2/3
      - Ethernet2/5
- name: >-
    Ensure VRF is tagged with interface Ethernet2/5 only (Removes from
    Ethernet2/3)
  cisco.nxos.nxos_vrf:
    name: test1
    interfaces:
      - Ethernet2/5
- name: Delete VRF
  cisco.nxos.nxos_vrf:
    name: ntc
    state: absent
z
commands:
  description: commands sent to the device
  returned: always
  type: list
  sample:
    - vrf context ntc
    - no shutdown
    - interface Ethernet1/2
    - no switchport
    - vrf member test2
N)deepcopy)AnsibleModule)remove_default_spec)get_interface_typeload_configrun_commandsc                 *    |D ]  }|d   | k(  s|c S  y )Nname )r   lstos      g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_vrf.pysearch_obj_in_listr      s"     V9H    c                 <    d| vrd}nd}| |dg}t        ||      }|S )Nzshow runjsontext)commandoutput)r   )r   moduler   cmdsbodys        r   execute_show_commandr      s2     623D%DKr   c                    t               }d}	 t        ||       d   }	 |d   d   }t        |t               r$|D ]  }i }|d   |d<   |j                  |        |S t        |t              ri }|d   |d<   |j                  |       |S # t        $ r t               cY S w xY w# t        t        t        f$ r t               cY S w xY w)Nzshow vrf allr   	TABLE_vrfROW_vrfvrf_namer   )listr   
IndexError	TypeErrorKeyError
isinstanceappenddict)r   objsr   r   	vrf_tablevrfobjs          r   get_existing_vrfsr,   
  s    6DG#GV4Q7%i0	 )T" 	CCj/CKKK	 K 
It	$
+FCK%  v z8, vs"   B B  BB C Cc                    t               }| \  }}|j                  d   }|j                  d   }d}|D ]  }|d   }	|d   }
|d   }|j                  d      xs g }|rd}n|d   }|d= t        |	|      }|dk(  r#|r!|j	                  d	j                  |	             i|d
k(  so|s|j	                  dj                  |	             |D ]>  }|j                  |      }|s|dk7  s|dz   t        |      z   }|j	                  |       @ |
dk(  r|j	                  d       n|
dk(  r|j	                  d       |j	                  d       |s|d   dk7  s!|D ]`  }|j	                  dj                  |             t        |      dv r|j	                  d       |j	                  dj                  |	             b |rT|j                  d      rC||j                  d      k7  r/|j	                  dj                  |j                  d                   |D ]  }|j                  |      }|dk(  r>|j                  |      s+d|z   dz   |j                  |      z   }|j	                  |       W|sZ||j                  |      k7  so|dz   t        |      z   }|j	                  |        |
rA|
|j                  d      k7  r-|
dk(  r|j	                  d       n|
dk(  r|j	                  d       |r2|j                  ddj                  |	             |j	                  d       |r6|d   dk7  r-|d   s|D ]  }|j	                  dj                  |	             |j	                  d       |j	                  dj                  |             t        |      dv r|j	                  d       |j	                  dj                  |	              t        |      t        |d         k7  st        t        |      t        |d         z
        }|D ]  }|j	                  dj                  |	             |j	                  d       |j	                  dj                  |             t        |      dv r|j	                  d       |j	                  dj                  |	              t        t        |d         t        |      z
        }|D ]  }|j	                  dj                  |	             |j	                  d       |j	                  dj                  |             t        |      dv r|j	                  d       |j	                  dj                  |	              #|s'|d   dk(  s1|d   s8|d   D ]  }|j	                  dj                  |	             |j	                  d       |j	                  dj                  |             t        |      dv r|j	                  d       |j	                  dj                  |	               |rQt        |      }|rD|D ]?  }|d   dv rt        |d   |      }|r|j	                  d	j                  |d                A |S )Nstatepurge)rddescriptionvnir   admin_stater2   
interfacesabsentzno vrf context {0}presentzvrf context {0}default upzno shutdowndownshutdownexitr   zinterface {0})ethernetportchannelzno switchportzvrf member {0}z
no vni {0}zno zno vrf member {0})r7   
management)r!   paramsgetr   r&   formatstrr	   insertsetr,   )updatesr   commandswanthaver.   r/   argswr   r3   r2   r4   obj_in_haveitem	candidatecmdimissing_interfacessuperfluous_interfacesexistinghobj_in_wants                          r   map_obj_to_commandsrV   $  s   vHJD$MM'"EMM'"E'D tNy&hUU<(.B
EgJEgJ(t4HOO077=>i 1 8 8 >?  -D !dI Y)%;"Sj3y>9 ,	-
 $&OOM2 F*OOJ/'*Q-9"<' G (>(>q(AB-a0 5  %OOO< (8(?(?(EFG "u-#9O2O (;(;KOOE<R(ST  -D !dI I-&??40"'$,"4{t7L"LC$OOC0"yKOOD4I'I"Sj3y>9 ,- ;+//-2P#P"d* 6$. 
3OOA'8'?'?'EFOOF+*Q-9"<&|4!+ 	KA$OO,=,D,DT,JK$OOF3$OOO,B,B1,EF1!4 9   !) @$OO,<,C,CD,IJ	K ZCL0I,JJ-1
Oc+l2K.LL.* "4 	KA$OO,=,D,DT,JK$OOF3$OOO,B,B1,EF1!4 9   !) @$OO,<,C,CD,IJ	K 26L 9:S_L2. "8 	NA$OO,=,D,DT,JK$OOF3$OOO,B,B1,EF1!4 9   !) @$OO,?,F,Ft,LM	N  JqMY$>"<0!,\!: 	NA$OO,=,D,DT,JK$OOF3$OOO,B,B1,EF1!4 9   !) @$OO,?,F,Ft,LM	NWtNl $V, PV9 99"4QvY"EK& (<(C(CAfI(NOP Or   c                     | rL| j                         } | dk(  r|j                  d       y t        |       dkD  r|j                  d|        y | S y )Nr7   z#cannot use default as name of a VRFmsg    z"VRF name exceeded max length of 32)rY   r   )strip	fail_jsonlen)r   r   s     r   validate_vrfr^     sS    zz|9!FGY^!EDQK r   c                 *   g }| j                   j                  d      }|rg|D ]`  }|D ]&  }|j                  |      | j                   |   ||<   ( |j                         }t        |d   |       |d<   |j	                  |       b |S |j	                  t        | j                   d   |       | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d   | j                   d	   d
       |S )N	aggregater   r1   r2   r0   r3   r.   r4   associated_interfaces)r   r1   r2   r0   r3   r.   r4   ra   )r@   rA   copyr^   r&   )r   r+   r`   rM   keyds         r   map_params_to_objre     s   
C!!+.I 	D 388C=( &c 2DI3 		A$QvY7AfIJJqM	* J 	

$V]]6%:FC%}}];}}U+mmD)%}}];w/$mmL9)/7N)O		
 Jr   c                     t        j                  dj                  |       t         j                        }d}| |v r |j	                  |      j                  d      }|S )Nz(?:{0}\s)(?P<value>.*)$ value)recompilerB   Msearchgroup)argconfigr   extra_arg_regexrh   s        r   	get_valuerq     sN    jj!;!B!B3!GNOE
f}&&v.44W=Lr   c                    t               }| D ]6  }t        |      }|d= |d= dj                  |d         }	 t        ||      d   }|d   d   }|d   }	|	|d<   |d	   j                         |d
<   dj                  |	      }t        ||      d   }g d}
|
D ]  }t        |||      ||<    g |d<   dj                  |	      }	 t        ||      d   }|d   d   }|rZt        |t               r!|D ]  }|d   }|d   j                  |        n)t        |t              r|d   }|d   j                  |       |j                  |       9 |S # t        t
        f$ r t               cY c S w xY w# t        t
        f$ r d }Y w xY w)Ndelayr.   zshow vrf {0}r   r   r   r   r    	vrf_stater3   z&show run all | section vrf.context.{0})r2   r0   r1   r4   zshow vrf {0} interfaceTABLE_ifROW_ifif_name)r!   r   rB   r   r#   r"   lowerrq   r%   r&   r'   )rH   element_specr   r(   rK   r+   r   r   r)   r   extra_paramsparamvrf_intrP   intfs                  r   map_config_to_objr~     s   6D '|$LL ''&	2	'8;D[))4I $F&{399;M:AA$G#GV4Q73! 	8E"5$7CJ	8 L*11$7	'8;D:&x0G '4(  3AY<D%,,T23 GT*y)L!((.CO'P K? :& 	6M	$ :& 	G	s#   E	 E*	E'&E'*E>=E>c           	      R   d }d}| D ]  }|j                  d      |d   r&|s$t        j                  |j                  d          d}|t	        | ||      }|d   D ]H  }t        |d   |      }|s|j                  d      }	|	)||	vs.|j                  d|d	|d   
       J  y )NFra   changedrs   Tr   r4   z
Interface z not configured on vrf rX   )rA   timesleepr@   r~   r   r\   )
rH   r   ry   resultrI   is_delayrK   rP   rL   r4   s
             r   check_declarative_intent_paramsr     s    DH 55()1)XJJv}}W-.H<$T<@D*+ 	A,QvY=K(__\:
)az.A$$GH!F)T % 	r   c                    d}t        j                  |t        |            rkt        j                  d       t        | |ddi      }t        j                  |t        |            r| j                  d|z         | j                  d       y	y	)
zHChecks for VRF config errors and executes a retry in some circumstances.z%ERROR: Deletion of VRF .* in progress   catch_clierrorToptsz#VRF config (and retry) failure: %s rX   z4VRF config delayed by VRF deletion - passed on retryN)ri   rl   rC   r   r   r
   r\   warn)r   rG   	responsespatterns       r   vrf_error_checkr   '  ss    5G	yy#i.)

28H$7OP	99Wc)n-!F!RSJK *r   c                     t        t        ddg      t        d      t        d      t        d      t        ddddg      t        dd	      t        dd	      t        d
d      t        ddddg      	      } t        |       }t        |       t        t        dd|      t        dd            }|j                  |        ddgg}ddgg}t	        |||d      }t               }ddi}|r||d<   t        |      }t        || |      }	t        ||	f|      }
|
|d<   |
r.|j                  s"t        ||
ddi      }t        ||
|       d|d<   t        ||| |        |j                  di | y) z%main entry point for module executionrC   r*   )typealiases)r   r9   r:   )r   r7   choicesr!   )r   elementsint
   )r   r7   r6   r5   )	r   r1   r2   r0   r3   r4   ra   rs   r.   r'   )r   r   optionsboolF)r`   r/   r   r`   T)argument_specrequired_one_ofmutually_exclusivesupports_check_moder   warningsrG   r   r   Nr   )r'   r   r   updater   r!   re   r~   rV   
check_moder
   r   r   	exit_json)ry   aggregate_specr   r   r   r   r   r   rH   rI   rG   r   s               r   mainr   3  s   uug.e$eUeTD&>JVe4"?r*y9h:OP
L l+N 'FV^L.M
 &,-O!;/0#'- 	F vHF%zV$DT<8D"D$<8H!F:))8H$7OP	)4 y#D&,GFvr   __main__)!
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNri   r   rb   r   ansible.module_utils.basicr   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr	   r
   r   r   r   r,   rV   r^   re   rq   r~   r   r   r   __name__r   r   r   <module>r      s   $ A @ L\4l
 
   4 4IX8+\4	L5p zF r   