
    Vh(<                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZmZmZ ddd	d
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_functiona  
module: nxos_hsrp
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: Manages HSRP configuration on NX-OS switches.
description:
- Manages HSRP configuration on NX-OS switches.
version_added: 1.0.0
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- Unsupported for Cisco MDS
- HSRP feature needs to be enabled first on the system.
- SVIs must exist before using this module.
- Interface must be a L3 port before using this module.
- HSRP cannot be configured on loopback interfaces.
- MD5 authentication is only possible with HSRPv2 while it is ignored if HSRPv1 is
  used instead, while it will not raise any error. Here we allow MD5 authentication
  only with HSRPv2 in order to enforce better practice.
options:
  group:
    description:
    - HSRP group number.
    required: true
    type: str
  interface:
    description:
    - Full name of interface that is being managed for HSRP.
    required: true
    type: str
  version:
    description:
    - HSRP version.
    default: '1'
    choices:
    - '1'
    - '2'
    type: str
  priority:
    description:
    - HSRP priority or keyword 'default'.
    type: str
  preempt:
    description:
    - Enable/Disable preempt.
    choices:
    - enabled
    - disabled
    type: str
  vip:
    description:
    - HSRP virtual IP address or keyword 'default'
    type: str
  auth_string:
    description:
    - Authentication string. If this needs to be hidden(for md5 type), the string
      should be 7 followed by the key string. Otherwise, it can be 0 followed by key
      string or just key string (for backward compatibility). For text type, this
      should be just be a key string. if this is 'default', authentication is removed.
    type: str
  auth_type:
    description:
    - Authentication type.
    choices:
    - text
    - md5
    type: str
  state:
    description:
    - Specify desired state of the resource.
    choices:
    - present
    - absent
    default: present
    type: str
aW  
- name: Ensure HSRP is configured with following params on a SVI
  cisco.nxos.nxos_hsrp:
    group: 10
    vip: 10.1.1.1
    priority: 150
    interface: vlan10
    preempt: enabled

- name: Ensure HSRP is configured with following params on a SVI with clear text authentication
  cisco.nxos.nxos_hsrp:
    group: 10
    vip: 10.1.1.1
    priority: 150
    interface: vlan10
    preempt: enabled
    auth_type: text
    auth_string: CISCO

- name: Ensure HSRP is configured with md5 authentication and clear authentication
    string
  cisco.nxos.nxos_hsrp:
    group: 10
    vip: 10.1.1.1
    priority: 150
    interface: vlan10
    preempt: enabled
    auth_type: md5
    auth_string: 0 1234

- name: Ensure HSRP is configured with md5 authentication and hidden authentication
    string
  cisco.nxos.nxos_hsrp:
    group: 10
    vip: 10.1.1.1
    priority: 150
    interface: vlan10
    preempt: enabled
    auth_type: md5
    auth_string: 7 1234

- name: Remove HSRP config for given interface, group, and VIP
  cisco.nxos.nxos_hsrp:
    group: 10
    interface: vlan10
    vip: 10.1.1.1
    state: absent
z
commands:
    description: commands sent to the device
    returned: always
    type: list
    sample: ["interface vlan10", "hsrp version 2", "hsrp 30", "ip 10.30.1.1"]
)AnsibleModule)get_capabilitiesget_interface_typeload_configrun_commandsN100textcisco)vippriority	auth_typeauth_stringc                     i }|D ]=  }| j                  |      }|s|j                  |      }|rt        |      ||<   9|||<   ? |S N)getstr)key_maptablenew_dictkeynew_keyvalues         h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_hsrp.pyapply_key_mapr      sV    H *++c"IIcNE$'J!$)!* O    c                     dj                  |       }i } d}	 t        ||g      d   }|dv r1|d   d   }t        |j	                  dd            }|d	k(  s|d
k(  rd}|S |dk(  rd}|S # t        $ r Y y w xY w)Nzshow interface {0} | jsonunknownr   )ethernetportchannelTABLE_interfaceROW_interfaceeth_modelayer3accesstrunklayer2svi)formatr
   
IndexErrorr   r   )	interface	intf_typemodulecommandmodebodyinterface_tables          r   get_interface_moder4      s    )00;GIDFWI.q1 //01/B?&&z8<=8twD K 
e	K  s   A% %	A10A1c           
         dj                  |       }i }dddddddd	d
d	}	 t        ||g      d   }|d   d   }d|vr|d= dt        |      v rt        |||      }t        |t              r|g}|D ]f  }t        ||      }	|	d   j                         |	d<   |	d   dk(  rd|	d<   n|	d   dk(  rd|	d<   |	d   dk(  r|	d	   dk(  rd|	d	<   nd|	d	<   |	d   |k(  sd|	c S  |S # t        t
        t        t        f$ r i cY S w xY w)Nzshow hsrp group {0} all | jsonr-   groupversionr   preemptr   r   auth_encr   )	sh_if_indexsh_group_numsh_group_versionsh_cfg_prio
sh_preemptsh_vipsh_authentication_typesh_keystring_attrsh_authentication_datar   TABLE_grp_detailROW_grp_detailrA   unknown enum:v11v22md5hidden70)r+   r
   r   get_hsrp_group_unknown_enumAttributeErrorr,   	TypeErrorKeyError
isinstancedictr   lower)
r6   r-   r/   r0   hsrphsrp_keyr2   
hsrp_table
hsrp_groupparsed_hsrps
             r   get_hsrp_grouprZ      sj   .55e<GD #%!"-'"/
HFWI.q1,-.>?
j0,-c*o-4VWjQJ *d# \
  
#Hj9#.{#;#A#A#CK y!T)%(K	"#t+%(K	"{#u,:&(2*-J'*-J'{#y0#& K3 J	8< 	s   9C C87C8c                 v    d|d   v r1d|j                  d      d   d}t        | |      d   }d|v rdnd	|d<   |S )
aW  Some older NXOS images fail to set the attr values when using structured output and
    instead set the values to <unknown enum>. This fallback method is a workaround that
    uses an unstructured (text) request to query the device a second time.
    'sh_preempt' is currently the only attr affected. Add checks for other attrs as needed.
    rE   r>   r   |r   )outputr0   zmay preemptenableddisabled)splitr
   )r/   r0   rW   cmdouts        r   rN   rN      sS     *\22GMM#,>q,AB63'*1>#1E9J
< r   c                 J    dj                  |      dj                  |       g}|S )Ninterface {0}zno hsrp {0})r+   )r6   r-   commandss      r   get_commands_remove_hsrprf     s'    &&y1=3G3G3NOHOr   c                    g }ddddd}| j                  dd       }| j                  dd       }| j                  dd       }| j                  d	d       }	|r|d
k(  rd| d<   n
|dk(  rd| d<   |	rO|	dk(  r3|r-|j                  d	      t        j                  d	      k7  rd| d	<   n| d	= ndj                  | d	         | d	<   |rO|dk(  r3|r-|j                  d      t        j                  d      k7  rd| d<   n| d= ndj                  | d         | d<   | D ]V  }
 |j                  |
d      j                  d$i | }|r.|dk7  r)|
dk(  r|j                  d|       n|j	                  |       d }X | j                  dd       }| j                  dd       }| j                  dd       }|s|r|s|d   }n|s|d   }|dk7  rQ|dk(  r$dj                  ||      }|j	                  |       nb|dk(  r]dj                  |      }|j	                  |       n:|r8|j                  d      t        j                  d      k7  r|j	                  d       |r&|s$|j                  ddj                  |d                | j                  dd       }|rB|dk(  rd}n|d k(  rd!}|j                  d       |j                  dd"j                  |             |r5|d   j                  d#      s!|j                  dd"j                  |             |S )%Nzhsrp {group}z
{priority}z	{preempt}z{vip})r6   r   r8   r   r8   r6   r   r   r^   r_   z
no preemptdefaultzno priorityzpriority {0}zno ipzip {0}DNEr   r   r   r9   rJ   z%authentication md5 key-string {0} {1}r   zauthentication text {0}zno authenticationzhsrp {0}r7   rI   zhsrp version 2rG   zhsrp version 1rd   r-    )r   PARAM_TO_DEFAULT_KEYMAPr+   insertappend
startswith)deltar-   argsexistingre   config_argsr8   r6   r   r   r   r0   r   r   r9   r7   s                   r   get_commands_config_hsrprs   	  s	   H   	K ii	4(GIIgt$E
))E4
 CyyT*Hi(E)
"+E)y HLL48O8S8ST^8__$1j!*% . 5 5eJ6G HE*
)HLL/3J3N3Nu3UU&e%L#??5<8E%L 4+//#u-44=u=w%'g~7+( 		+t,I))M40KyyT*HK[)I}-K)#E!AHHS^_(f$3::;G(HLL7;R;V;V<   34:,,T'];<ii	4(Gc>&G^&G7#?11)<={%%k2OOA55i@AOr   c                     dj                  |       }	 t        ||gd      d   }d|j                         v ry|j                  d      }|d   j	                  d	      ry
y# t
        $ r Y yw xY w)Nzshow run interface {0}F)check_rcr   invalidri   
r-   T)r+   r
   rT   r`   rn   rQ   )r-   r/   r0   r2   raw_lists        r   
is_defaultrz   a  su    &--i8GFWI>qA

$zz$'H|&&{3 s   $A %A 	A+*A+c                 r    dj                  |       }d|j                         v r|j                  d|       y y )N zinvalid ip addressz+Invalid VIP. Possible duplicate IP address.)msgr   )joinrT   	fail_json)r2   r   r/   new_bodys       r   validate_configr   r  s8    wwt}Hx~~//JPST 0r   c                  D   t        t        dd      t        d      t        ddgdd      t        dd	      t        dd
dgd      t        dd	      t        ddgd      t        dd	      t        ddgdd      	      } t        | d      }t               }t        d|      }|j                  d   j	                         }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }d}d }|rk|j                         }t        |      d k(  r|d!   }|d"   }n&t        |      d"k(  r|d!   }n|j                  d#$       |dk7  r|d%k7  r|j                  d&$       t        |      }|j                  d'd(      }t        |      }|d)k7  r?|d*k(  r:t        ||      d+k(  r|j                  d,|-       |d.k(  r|j                  d/|-       t        |||      }|d0k(  r|j                  d1|-       |s|r|r|s|j                  d2$       t        ||||	|
|||3      }t        d4 |j                         D              }t        |||      }|j                  dd       dk(  r|d   dk(  rN|j                  d5$       n;|j                  dd       s)|r'|d   dk(  r|d   dk(  r|dk(  r|j                  d6$       g }|dk(  rat        t        |j                               j!                  |j                                     }|rDt#        ||||      }|j%                  |       n$|dk(  r|rt'        ||      }|j%                  |       |rz|j(                  r |j*                  d;i | n[t-        ||       |d*k(  r0|dk(  r+|j/                  d!d7       t1        ||      }t3        ||
|       d|d8<   d9|v r|j5                  d!       ||d:<    |j*                  d;i | y )<NTr   )requiredtype)r   rG   rI   F)choicesrh   r   )r   r   r_   r^   )r   r   r   r   rJ   )r   r   absentpresent)r   r   rh   )	r6   r-   r7   r   r8   r   r   r   state)argument_specsupports_check_mode)changedwarningsr-   r6   r7   r   r   r8   r   r   r   rM      r      zInvalid auth_string)r}   rL   z(Invalid auth_string, only 0 or 7 allowednetwork_apinxapir!   cliconfri   z3That interface does not exist yet. Create it first.)r}   r-   loopbackz'Loopback interfaces don't support HSRP.r)   z>That interface is a layer2 port.
Make it a layer 3 port first.zDWhen using auth parameters, you need BOTH auth_type AND auth_string.)r6   r7   r   r8   r   r   r   r9   c              3   0   K   | ]  \  }}|	||f  y wr   rj   ).0kvs      r   	<genexpr>zmain.<locals>.<genexpr>  s     Etq!q}QFEs   
	z2It's recommended to use HSRP v2 when auth_type=md5zIExisting auth_type is md5. It's recommended to use HSRP v2 when using md5zconfig tr   	configurere   rj   )rS   r   listparamsrT   r`   lenr   r   r   r   rz   r4   itemsrZ   set
differencers   extendrf   
check_mode	exit_jsonr	   rl   r
   r   pop)r   r/   r   resultsr-   r6   r7   r   r   r8   r   r   auth_full_stringr9   r   kstrdevice_infor   r.   r1   rp   proposedrq   re   ro   r0   r2   s                              r   mainr   x  sK   Du-%c3ZuE551%*i)@5Qee,?ee4Hi05)T
M DQFvH584Gk*002IMM'"EmmI&GMM'"E}}Z(HmmI&G
--
Ck*I}}]3HK%%'t9>AwHq'KY!^q'K!67s?x3!KL"6*K//-9K"9-IJ;)#;i(E1L#   
"=#  
 iF;DxT 	 	

 Kk]   	D Etzz|EEHeY7H ||K&%/I#%!XY\\+t,Y3&8K+@E+IuXaOab   H	S)*55hnn6FGH.uixPGOOG$	(	.ui@GOOG$F'w') i'EY,>:.#FH5c62!%GIh&Q"GJFwr   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r	   r
   rk   r   r4   rZ   rN   rf   rs   rz   r   r   __name__rj   r   r   <module>r      s    @ @ 
M^/b
 5  	 
&0f

Up"U~ B zF r   