Anons79 Mini Shell

Directory : /lib/python2.7/site-packages/ansible/modules/net_tools/netbox/
Upload File :
Current File : //lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyc

�
�Udac@`s�ddlmZmZmZeZidd6dgd6dd6ZdZd	Zd
Z	ddl
Z
ddlZddlm
Z
mZdd
lmZmZmZmZmZmZmZddlmZddlmZdZyddlZeZWn#e k
rej!�Ze"ZnXd�Z#d�Z$d�Z%d�Z&d�Z'd�Z(d�Z)d�Z*d�Z+dd�Z,d�Z-e.dkr�e#�ndS(i(tabsolute_importtdivisiontprint_functions1.1tmetadata_versiontpreviewtstatust	communitytsupported_bys�
---
module: netbox_ip_address
short_description: Creates or removes IP addresses from Netbox
description:
  - Creates or removes IP addresses from Netbox
notes:
  - Tags should be defined as a YAML list
  - This should be ran with connection C(local) and hosts C(localhost)
author:
  - Mikhail Yohman (@FragmentedPacket)
  - Anthony Ruhier (@Anthony25)
requirements:
  - pynetbox
version_added: '2.8'
options:
  netbox_url:
    description:
      - URL of the Netbox instance resolvable by Ansible control host
    required: true
  netbox_token:
    description:
      - The token created within Netbox to authorize API access
    required: true
  data:
    description:
      - Defines the IP address configuration
    suboptions:
      family:
        description:
          - Specifies with address family the IP address belongs to
        choices:
          - 4
          - 6
      address:
        description:
          - Required if state is C(present)
      prefix:
        description:
          - |
            With state C(present), if an interface is given, it will ensure
            that an IP inside this prefix (and vrf, if given) is attached
            to this interface. Otherwise, it will get the next available IP
            of this prefix and attach it.
            With state C(new), it will force to get the next available IP in
            this prefix. If an interface is given, it will also force to attach
            it.
            Required if state is C(present) or C(new) when no address is given.
            Unused if an address is specified.
      vrf:
        description:
          - VRF that IP address is associated with
      tenant:
        description:
          - The tenant that the device will be assigned to
      status:
        description:
          - The status of the IP address
        choices:
          - Active
          - Reserved
          - Deprecated
          - DHCP
      role:
        description:
          - The role of the IP address
        choices:
          - Loopback
          - Secondary
          - Anycast
          - VIP
          - VRRP
          - HSRP
          - GLBP
          - CARP
      interface:
        description:
          - |
            The name and device of the interface that the IP address should be assigned to
            Required if state is C(present) and a prefix specified.
      description:
        description:
          - The description of the interface
      nat_inside:
        description:
          - The inside IP address this IP is assigned to
      tags:
        description:
          - Any tags that the IP address may need to be associated with
      custom_fields:
        description:
          - must exist in Netbox
    required: true
  state:
    description:
      - |
        Use C(present), C(new) or C(absent) for adding, force adding or removing.
        C(present) will check if the IP is already created, and return it if
        true. C(new) will force to create it anyway (useful for anycasts, for
        example).
    choices: [ absent, new, present ]
    default: present
  validate_certs:
    description:
      - If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates.
    default: 'yes'
    type: bool
s

- name: "Test Netbox IP address module"
  connection: local
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Create IP address within Netbox with only required information
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          address: 192.168.1.10
        state: present
    - name: Force to create (even if it already exists) the IP
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          address: 192.168.1.10
        state: new
    - name: Get a new available IP inside 192.168.1.0/24
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          prefix: 192.168.1.0/24
        state: new
    - name: Delete IP address within netbox
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          address: 192.168.1.10
        state: absent
    - name: Create IP address with several specified options
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          family: 4
          address: 192.168.1.20
          vrf: Test
          tenant: Test Tenant
          status: Reserved
          role: Loopback
          description: Test description
          tags:
            - Schnozzberry
        state: present
    - name: Create IP address and assign a nat_inside IP
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          family: 4
          address: 192.168.1.30
          vrf: Test
          nat_inside:
            address: 192.168.1.20
            vrf: Test
          interface:
            name: GigabitEthernet1
            device: test100
    - name: Ensure that an IP inside 192.168.1.0/24 is attached to GigabitEthernet1
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          prefix: 192.168.1.0/24
          vrf: Test
          interface:
            name: GigabitEthernet1
            device: test100
        state: present
    - name: Attach a new available IP of 192.168.1.0/24 to GigabitEthernet1
      netbox_ip_address:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          prefix: 192.168.1.0/24
          vrf: Test
          interface:
            name: GigabitEthernet1
            device: test100
        state: new
s�
ip_address:
  description: Serialized object as created or already existent within Netbox
  returned: on creation
  type: dict
msg:
  description: Message indicating failure or info about what has been achieved
  returned: always
  type: str
N(t
AnsibleModuletmissing_required_lib(tfind_idstnormalize_datatcreate_netbox_objecttdelete_netbox_objecttupdate_netbox_objecttIP_ADDRESS_ROLEtIP_ADDRESS_STATUS(t	ipaddress(tto_textcC`sstdtdddt�dtdddtdt�dtdddt�d	tdtd
dddd
dg�dtddd
t��}td|dt�ats�tjdtd�dt�nt}d}d}tj	d}tj	d}tj	d}tj	d	}tj	d}yt
j|d|d|�}	Wn!tk
rVtjdd�nXyt
|	|�}
Wn%tk
r�tjdd|�nXt
|
|�}t|�}ykt|	|�}|dkr�tt||
||�S|d
krtjt||��Stjdd|�SWnTt
jk
rI}
tjdtj|
j��Stk
rn}
tjdt|
��SXdS(s/
    Main entry point for module execution
    t
netbox_urlttypetstrtrequiredtnetbox_tokentno_logtdatatdicttstatetdefaulttpresenttchoicestabsenttnewtvalidate_certstboolt
argument_spectsupports_check_modetmsgtpynetboxt	exceptiontipamtip_addressesttokent
ssl_verifys,Failed to establish connection to Netbox APIs#Incorrect application specified: %ssInvalid state %sN(snewR(RtTruetFalseRtmoduletHAS_PYNETBOXt	fail_jsonR	tPYNETBOX_IMP_ERRtparamsR&tapit	ExceptiontgetattrtAttributeErrorRt_check_and_adapt_datat_handle_state_new_presentt	exit_jsontensure_ip_address_absenttRequestErrortjsontloadsterrort
ValueErrorR(R#tchangedtapptendpointturlR*RRR!tnbtnb_apptnb_endpointt	norm_datate((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pytmain�sR$	






cC`s�t||�}|jd�rIt|dt�rItd|d��n|jd�rxtj|dj��|d<n|jd�r�tj|dj��|d<n|S(Ntvrfs%%s does not exist - Please create VRFRtrole(R
tgett
isinstancetintR?RtlowerR(RDR((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyR74s#  cC`s�|jd�rV|dkr1|jt||��S|dkr�|jt||��SnG|dkr{|jt|||��S|dkr�|jt||��SdS(NtaddressRR (RLR9tensure_ip_address_presenttcreate_ip_addresst$ensure_ip_in_prefix_present_on_netiftget_new_available_ip_address(R.RRERFR((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyR8CscC`st|t�s't}i|d6|d6Syt||�}Wntk
rTt|�SXi}|snt||�St||tj	�\}}|tkr�tj
dd|d�n|r�d|d}t}||d<n |j�}t}d|d}i|d6|d6|d6Sd	S(
s�
    :returns dict(ip_address, msg, changed): dictionary resulting of the request,
    where 'ip_address' is the serialized ip fetched or newly created in Netbox
    R%R@s&Request failed, couldn't update IP: %sRPsIP Address %s updatedtdiffsIP Address %s already existst
ip_addressN(
RMRR-t
_search_ipR?t_error_multiple_ip_resultsRRRR.t
check_modeR0R,t	serialize(RFRR@tnb_addrtresulttip_addrRUR%((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRQZs,

	
cC`sDi|dd6}|jd�r1|d|d<n|j|�}|S(NRPRJtvrf_id(RL(RFRtget_query_paramsR]((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRW}s
cC`s:t}d|kr$idd6|d6Sidd6|d6SdS(NRJsReturned more than resultR%R@s3Returned more than one result - Try specifying VRF.(R-(RR@((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRX�scC`svt|t�s't}i|d6|d6St||tj�\}}t}d|d}i|d6|d6|d6|d6S(NR%R@sIP Addresses %s createdRPRVRU(RMRR-RR.RYR,(RFRR@R]RUR%((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRR�scC`s�t|t�s't}i|d6|d6S|jd�sG|jd�rVtd��ni|dd6|dd6}|jd�r�|d|d	<n|j|�}|r�|d
j�}t}d|d}i|d
6|d6|d6St||�SdS(s�
    :returns dict(ip_address, msg, changed): dictionary resulting of the request,
    where 'ip_address' is the serialized ip fetched or newly created in Netbox
    R%R@t	interfacetprefixs#A prefix and interface are requiredtinterface_idtparentRJR^i����sIP Address %s already attachedRPRVN(RMRR-RLR?tfilterRZRT(RERFRR@R_tattached_ipsR]R%((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRS�s  c	C`si|dd6}|jd�r1|d|d<ni}|jj|�}|sut}d|d}i|d6|d6S|jj�r�t|j|tj�\}}t}d|d}||d	<n&t}d
|d}i|d6|d6S|j	i|d6|d6|d6�|S(NRaRJR^s'%s does not exist - please create firstR%R@sIP Addresses %s createdRPRUs$No available IPs available within %sRV(
RLtprefixesR-t
available_ipstlistRR.RYR,tupdate(	RERtprefix_queryR\RaR@R%R]RU((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyRT�s&
"cC`s�tj|�}t|j�}|j}i|d6|d6}|rN||d<n|jj|�}|s�|r�td||f��q�td|��n|S(NRatmask_lengthR^s5Prefix %s does not exist in VRF %s - Please create its+Prefix %s does not exist - Please create it(Rt
ip_networkRtnetwork_addresst	prefixlenRfRLR?(RERaR^t
ipaddr_prefixtnetworktmasktprefix_query_paramst	prefix_id((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyt_get_prefix_id�s	

cC`s�t|t�s't}i|d6|d6Syt||�}Wntk
rTt|�SXi}|r�t|tj�\}}t	}d|d}||d<nt}d|d}|j
i|d6|d6�|S(s%
    :returns dict(msg, changed)
    R%R@sIP Address %s deletedRPRUsIP Address %s already absent(RMRR-RWR?RXR
R.RYR,Ri(RFRR@R]R\tdummyRUR%((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyR:�s"

t__main__(/t
__future__RRRRt
__metaclass__tANSIBLE_METADATAt
DOCUMENTATIONtEXAMPLEStRETURNR<t	tracebacktansible.module_utils.basicRR	t2ansible.module_utils.net_tools.netbox.netbox_utilsR
RRR
RRRtansible.module_utils.compatRtansible.module_utils._textRtNoneR1R&R,R/tImportErrort
format_excR-RIR7R8RQRWRXRRRSRTRtR:t__name__(((sV/usr/lib/python2.7/site-packages/ansible/modules/net_tools/netbox/netbox_ip_address.pyt<module>sB


mX4	


	<			#							

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