Anons79 Mini Shell

Directory : /lib/python2.7/site-packages/ansible/modules/network/cloudvision/
Upload File :
Current File : //lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyo

�
�Udac@`s�ddlmZmZmZeZidd6dgd6dd6ZdZd	Zd
Z	ddl
Z
ddlZddlm
Z
y&ddlZdd
lmZeZWnek
r�eZnXy0ddlmZddlmZmZeZWnek
reZnXd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%d�Z&e'dkr�e&�ndS(i(tabsolute_importtdivisiontprint_functions1.1tmetadata_versiontpreviewtstatust	communitytsupported_bys�
---
module: cv_server_provision
version_added: "2.4"
author: "EOS+ CS ([email protected]) (@mharista)"
short_description:
    Provision server port by applying or removing template configuration to an
    Arista CloudVision Portal configlet that is applied to a switch.
description:
  - This module allows a server team to provision server network ports for
    new servers without having to access Arista CVP or asking the network team
    to do it for them. Provide the information for connecting to CVP, switch
    rack, port the new server is connected to, optional vlan, and an action
    and the module will apply the configuration to the switch port via CVP.
    Actions are add (applies template config to port),
    remove (defaults the interface config) and
    show (returns the current port config).
options:
  host:
    description:
      - The hostname or IP address of the CVP node being connected to.
    required: true
  port:
    description:
      - The port number to use when making API calls to the CVP node. This
        will default to the default port for the specified protocol. Port 80
        for http and port 443 for https.
  protocol:
    description:
      - The protocol to use when making API calls to CVP. CVP defaults to https
        and newer versions of CVP no longer support http.
    default: https
    choices: [https, http]
  username:
    description:
      - The user that will be used to connect to CVP for making API calls.
    required: true
  password:
    description:
      - The password of the user that will be used to connect to CVP for API
        calls.
    required: true
  server_name:
    description:
      - The hostname or identifier for the server that is having it's switch
        port provisioned.
    required: true
  switch_name:
    description:
      - The hostname of the switch is being configured for the server being
        provisioned.
    required: true
  switch_port:
    description:
      - The physical port number on the switch that the new server is
        connected to.
    required: true
  port_vlan:
    description:
      - The vlan that should be applied to the port for this server.
        This parameter is dependent on a proper template that supports single
        vlan provisioning with it. If a port vlan is specified by the template
        specified does not support this the module will exit out with no
        changes. If a template is specified that requires a port vlan but no
        port vlan is specified the module will exit out with no changes.
  template:
    description:
      - A path to a Jinja formatted template file that contains the
        configuration block that will be applied to the specified switch port.
        This template will have variable fields replaced by the module before
        being applied to the switch configuration.
    required: true
  action:
    description:
      - The action for the module to take. The actions are add, which applies
        the specified template config to port, remove, which defaults the
        specified interface configuration, and show, which will return the
        current port configuration with no changes.
    default: show
    choices: [show, add, remove]
  auto_run:
    description:
      - Flag that determines whether or not the module will execute the CVP
        task spawned as a result of changes to a switch configlet. When an
        add or remove action is taken which results in a change to a switch
        configlet, CVP will spawn a task that needs to be executed for the
        configuration to be applied to the switch. If this option is True then
        the module will determined the task number created by the configuration
        change, execute it and wait for the task to complete. If the option
        is False then the task will remain in the Pending state in CVP for
        a network administrator to review and execute.
    type: bool
    default: 'no'
requirements: [Jinja2, cvprac >= 0.7.0]
s#
- name: Get current configuration for interface Ethernet2
  cv_server_provision:
    host: cvp_node
    username: cvp_user
    password: cvp_pass
    protocol: https
    server_name: new_server
    switch_name: eos_switch_1
    switch_port: 2
    template: template_file.j2
    action: show

- name: Remove existing configuration from interface Ethernet2. Run task.
  cv_server_provision:
    host: cvp_node
    username: cvp_user
    password: cvp_pass
    protocol: https
    server_name: new_server
    switch_name: eos_switch_1
    switch_port: 2
    template: template_file.j2
    action: remove
    auto_run: True

- name: Add template configuration to interface Ethernet2. No VLAN. Run task.
  cv_server_provision:
    host: cvp_node
    username: cvp_user
    password: cvp_pass
    protocol: https
    server_name: new_server
    switch_name: eos_switch_1
    switch_port: 2
    template: single_attached_trunk.j2
    action: add
    auto_run: True

- name: Add template with VLAN configuration to interface Ethernet2. Run task.
  cv_server_provision:
    host: cvp_node
    username: cvp_user
    password: cvp_pass
    protocol: https
    server_name: new_server
    switch_name: eos_switch_1
    switch_port: 2
    port_vlan: 22
    template: single_attached_vlan.j2
    action: add
    auto_run: True
s!
changed:
  description: Signifies if a change was made to the configlet
  returned: success
  type: bool
  sample: true
currentConfigBlock:
  description: The current config block for the user specified interface
  returned: when action = show
  type: str
  sample: |
    interface Ethernet4
    !
newConfigBlock:
  description: The new config block for the user specified interface
  returned: when action = add or remove
  type: str
  sample: |
    interface Ethernet3
        description example
        no switchport
    !
oldConfigBlock:
  description: The current config block for the user specified interface
               before any changes are made
  returned: when action = add or remove
  type: str
  sample: |
    interface Ethernet3
    !
fullConfig:
  description: The full config of the configlet after being updated
  returned: when action = add or remove
  type: str
  sample: |
    !
    interface Ethernet3
    !
    interface Ethernet4
    !
updateConfigletResponse:
  description: Response returned from CVP when configlet update is triggered
  returned: when action = add or remove and configuration changes
  type: str
  sample: "Configlet veos1-server successfully updated and task initiated."
portConfigurable:
  description: Signifies if the user specified port has an entry in the
               configlet that Ansible has access to
  returned: success
  type: bool
  sample: true
switchConfigurable:
  description: Signifies if the user specified switch has a configlet
               applied to it that CVP is allowed to edit
  returned: success
  type: bool
  sample: true
switchInfo:
  description: Information from CVP describing the switch being configured
  returned: success
  type: dict
  sample: {"architecture": "i386",
           "bootupTimeStamp": 1491264298.21,
           "complianceCode": "0000",
           "complianceIndication": "NONE",
           "deviceInfo": "Registered",
           "deviceStatus": "Registered",
           "fqdn": "veos1",
           "hardwareRevision": "",
           "internalBuildId": "12-12",
           "internalVersion": "4.17.1F-11111.4171F",
           "ipAddress": "192.168.1.20",
           "isDANZEnabled": "no",
           "isMLAGEnabled": "no",
           "key": "00:50:56:5d:e5:e0",
           "lastSyncUp": 1496432895799,
           "memFree": 472976,
           "memTotal": 1893460,
           "modelName": "vEOS",
           "parentContainerId": "container_13_5776759195930",
           "serialNumber": "",
           "systemMacAddress": "00:50:56:5d:e5:e0",
           "taskIdList": [],
           "tempAction": null,
           "type": "netelement",
           "unAuthorized": false,
           "version": "4.17.1F",
           "ztpMode": "false"}
taskCompleted:
  description: Signifies if the task created and executed has completed successfully
  returned: when action = add or remove, and auto_run = true,
            and configuration changes
  type: bool
  sample: true
taskCreated:
  description: Signifies if a task was created due to configlet changes
  returned: when action = add or remove, and auto_run = true or false,
            and configuration changes
  type: bool
  sample: true
taskExecuted:
  description: Signifies if the automation executed the spawned task
  returned: when action = add or remove, and auto_run = true,
            and configuration changes
  type: bool
  sample: true
taskId:
  description: The task ID created by CVP because of changes to configlet
  returned: when action = add or remove, and auto_run = true or false,
            and configuration changes
  type: str
  sample: "500"
N(t
AnsibleModule(tmeta(t	CvpClient(t
CvpLoginErrortCvpApiErrorc	C`s�t�}yI|j|jdg|jd|jdd|jdd|jd�Wn)tk
r}}|jdt|��nX|S(s� Connects to CVP device using user provided credentials from playbook.

    :param module: Ansible module with parameters and client connection.
    :return: CvpClient object with connection instantiated.
    thosttusernametpasswordtprotocoltporttmsg(R
tconnecttparamsRt	fail_jsontstr(tmoduletclientte((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyR3s	


cC`sI|jd}|jjj|�}|sE|jdtd|��n|S(s� Get dictionary of switch info from CVP.

    :param module: Ansible module with parameters and client connection.
    :return: Dict of switch info from CVP or exit with failure if no
             info for device is found.
    tswitch_nameRs%Device with name '%s' does not exist.(RRtapitget_device_by_nameRR(RRtswitch_info((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyREs
cC`s_|jjj|d|d�}|ddkr[|jdtd|d|df��ndS(	s� Check if switch is currently in compliance.

    :param module: Ansible module with parameters and client connection.
    :param sw_info: Dict of switch info.
    :return: Nothing or exit with failure if device is not in compliance.
    tkeyttypetcomplianceCodet0000Rs<Switch %s is not in compliance. Returned compliance code %s.tfqdnN(RRtcheck_complianceRR(Rtsw_infot
compliance((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytswitch_in_complianceTs
cC`s^d}|jdd}|jjj|d�}x'|D]}|d|kr7|}q7q7W|S(s Check CVP that the user specified switch has a configlet assigned to
        it that Ansible is allowed to edit.

    :param module: Ansible module with parameters and client connection.
    :param sw_info: Dict of switch info.
    :return: Dict of configlet information or None.
    Rs-serverRtnameN(tNoneRRRtget_configlets_by_device_id(RR$tconfigurable_configlettconfiglet_nametswitch_configletst	configlet((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytserver_configurable_configletds


cC`sTt}d|jd}x6|djd�D]!}tj||�r+t}q+q+W|S(sH Check configlet if the user specified port has a configuration entry
        in the configlet to determine if Ansible is allowed to configure the
        port on this switch.

    :param module: Ansible module with parameters and client connection.
    :param configlet: Dict of configlet info.
    :return: True or False.
    s^interface Ethernet%stswitch_porttconfigs
(tFalseRtsplittretmatchtTrue(RR-tconfigurabletregextconfig_line((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytport_configurablevs	
cC`s%t�}t||d�}|jddkr=||d<|S|jddkrct|�|d<n+|jddkr�d|jd	|d<n||d
<t||d|d�|d<|jjj|d|d|d
�}d|kr!|d|d<d|dkr!t|d<t|d<q!n|S(s� Take appropriate action based on current state of device and user
        requested action.

        Return current config block for specified port if action is show.

        If action is add or remove make the appropriate changes to the
        configlet and return the associated information.

    :param module: Ansible module with parameters and client connection.
    :param configlet: Dict of configlet info.
    :return: Dict of information to updated results with.
    R0tactiontshowtcurrentConfigBlocktaddtnewConfigBlocktremovesinterface Ethernet%s
!R/toldConfigBlockt
fullConfigRR'tdatatupdateConfigletResponsettasktchangedttaskCreated(	tdicttcurrent_configRtconfig_from_templatetupdated_configlet_contentRRtupdate_configletR5(RR-tresulttexisting_configtresp((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytconfiglet_action�s.
	



cC`s�d|jd}tj||tj�}|sL|jdtd|��n|jd\}}tjd||tj�}|s�||S|jd\}}||}|||!S(s] Parse the full port configuration for the user specified port out of
        the full configlet configuration and return as a string.

    :param module: Ansible module with parameters and client connection.
    :param config: Full config to parse specific port config from.
    :return: String of current config block for user specified port.
    s^interface Ethernet%sR/Rs interface section not found - %sit!(RR3tsearchtMRRtregs(RR0R7R4tblock_starttline_endt_t	block_end((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyRH�s
cC`s;t}d|}tj||tj�}|s7t}n|S(s� Test if the user provided Jinja template is valid.

    :param port: User specified port.
    :param template: Contents of Jinja template.
    :return: True or False
    s^interface Ethernet%s(R5R3R4RRR1(RttemplatetvalidR7R4((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytvalid_template�s
	cC`sgtjd�}tjd|dtj�}|j|jd�}|sj|jdtd|jd��ni|jdd6|jdd6}|jj	||jd�d	}|j
|�}ttj
|��}d
|kr!|jd
r�|jd
|d
<q!|jdtd|jd��n|j|�}t|jd|�sc|jdtd|��n|S(
s� Load the Jinja template and apply user provided parameters in necessary
        places. Fail if template is not found. Fail if rendered template does
        not reference the correct port. Fail if the template requires a VLAN
        but the user did not provide one with the port_vlan parameter.

    :param module: Ansible module with parameters and client connection.
    :return: String of Jinja template rendered with parameters or exit with
             failure.
    s./templatestloadert	undefinedRXRsCould not find template - %sR/tserver_nameit	port_vlansETemplate %s requires a vlan. Please re-run with vlan number provided.s9Template content does not configure proper interface - %s(tjinja2tFileSystemLoadertEnvironmenttDebugUndefinedtget_templateRRRR[t
get_sourcetparsetlistR	tfind_undeclared_variablestrenderRZ(Rttemplate_loadertenvRXRBttemp_sourcetparsed_contentt	temp_vars((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyRI�s,
 
c
C`s�d|jd}tj||tj�}|sL|jdtd|��n|jd\}}|| |}tjd||tj�}|r�|jd\}}	||	}	|d||	7}n|S(s� Update the configlet configuration with the new section for the port
        specified by the user.

    :param module: Ansible module with parameters and client connection.
    :param existing_config: String of current configlet configuration.
    :param new_config: String of configuration for user specified port to
                       replace in the existing config.
    :return: String of the full updated configuration.
    s^interface Ethernet%sR/Rs interface section not found - %sis!\ns
%s(RR3RQRRRRRS(
RRMt
new_configR7R4RTRUtupdated_configRVRW((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyRJ�s

cC`s�x�td�D]�}t|�}d|kr�t|d�dkr�xA|dD]2}d|dkrL|dddkrL|d	SqLWntjd
�q
WdS(s~ Poll device info of switch from CVP up to three times to see if the
        configlet updates have spawned a task. It sometimes takes a second for
        the task to be spawned after configlet updates. If a task is found
        return the task ID. Otherwise return None.

    :param module: Ansible module with parameters and client connection.
    :return: Task ID or None.
    it
taskIdListisConfiglet AssigntdescriptionRBtWORKFLOW_ACTIONsConfiglet PushtworkOrderIdiN(trangeRtlenttimetsleepR((Rtnumtdevice_infoRD((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytconfiglet_update_tasks	cC`s�t}xu|s}|jjj|�}|d}|dkr>tS|d	krm|jdtd||f��ntjd�q	WdS(
sS Poll CVP for the executed task to complete. There is currently no
        timeout. Exits with failure if task status is Failed or Cancelled.

    :param module: Ansible module with parameters and client connection.
    :param task: Task ID to poll for completion.
    :return: True or exit with failure if task is cancelled or fails.
    tworkOrderUserDefinedStatust	CompletedtFailedt	CancelledRsSTask %s has reported status %s. Please consult the CVP admins for more information.iN(R}R~(	R1RRtget_task_by_idR5RRRvRw(RRDt
task_completet	task_infottask_status((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytwait_for_task_completion*s	
cC`sQtdtdt�dtdtdd)�dtdddddg�d	tdt�d
tdtdt�dtdt�d
tdt�dtdt�dtdtdd)�dtdt�dtddddddg�dtdddt��}td|dt�}ts|jdd�nts7|jdd�ntdt�}t|�|_	y�t
|�|d<|jdd*kr�t||d�nt
||d�}|s�|jdtd |jd
��nt|d!<t||�s|jdtd"|jd|jd
f��nt|d#<t|d$<t|d%<t|d&<|jt||��|jdr|jddkrt|�}|r||d'<d(|jd
|jdf}|j	jj||�|j	jj|�t|d%<t||�}|rt|d&<qqt|d$<nWn)tk
r?}|jdt|��nX|j|�d)S(+s+ main entry point for module execution
    R
trequiredRtdefaultRthttpstchoicesthttpRRtno_logR]RR/R^RXtrequireR:R;R=R?tauto_runRtboolt
argument_spectsupports_check_modeRs%The Jinja2 python module is required.s%The cvprac python module is required.REt
switchInfos+Switch %s has no configurable server ports.tswitchConfigurables:Port %s is not configurable as a server port on switch %s.tportConfigurableRFttaskExecutedt
taskCompletedttaskIds0Update config on %s with %s action from Ansible.N(saddsremove(RGR5R1R(Rt
HAS_JINJA2Rt
HAS_CVPRACRRRRR&R.RR9tupdateRORzRtadd_note_to_tasktexecute_taskR�Rt	exit_json(R�RRLtswitch_configletttask_idtnotettask_completedR((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pytmain?sr	






 


t__main__((t
__future__RRRRt
__metaclass__tANSIBLE_METADATAt
DOCUMENTATIONtEXAMPLEStRETURNR3Rvtansible.module_utils.basicRR_R	R5R�tImportErrorR1tcvprac.cvp_clientR
tcvprac.cvp_client_errorsRRR�RRR&R.R9RORHRZRIRJRzR�R�t__name__(((s[/usr/lib/python2.7/site-packages/ansible/modules/network/cloudvision/cv_server_provision.pyt<module>sJ


`6r





						&			'				C

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]