
    Vh                      r    d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ ddZd Zd	 Zdd
Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: nclu
deprecated:
  removed_in: 6.0.0
  why: This collection and all content in it is unmaintained and deprecated.
  alternative: Unknown.
author: "Cumulus Networks (@isharacomix)"
short_description: Configure network interfaces using NCLU
description:
    - Interface to the Network Command Line Utility, developed to make it easier
      to configure operating systems running ifupdown2 and Quagga, such as
      Cumulus Linux. Command documentation is available at
      U(https://docs.cumulusnetworks.com/cumulus-linux/System-Configuration/Network-Command-Line-Utility-NCLU/)
options:
    commands:
        description:
            - A list of strings containing the net commands to run. Mutually
              exclusive with I(template).
    template:
        description:
            - A single, multi-line string with jinja2 formatting. This string
              will be broken by lines, and each line will be run through net.
              Mutually exclusive with I(commands).
    commit:
        description:
            - When true, performs a 'net commit' at the end of the block.
              Mutually exclusive with I(atomic).
        default: false
        type: bool
    abort:
        description:
            - Boolean. When true, perform a 'net abort' before the block.
              This cleans out any uncommitted changes in the buffer.
              Mutually exclusive with I(atomic).
        default: false
        type: bool
    atomic:
        description:
            - When true, equivalent to both I(commit) and I(abort) being true.
              Mutually exclusive with I(commit) and I(atomic).
        default: false
        type: bool
    description:
        description:
            - Commit description that will be recorded to the commit log if
              I(commit) or I(atomic) are true.
        default: "Ansible-originated commit"
notes:
    - Supports check_mode. Note that when using check_mode, I(abort) is always true.
a  

- name: Add two interfaces without committing any changes
  community.network.nclu:
    commands:
        - add int swp1
        - add int swp2

- name: Modify hostname to Cumulus-1 and commit the change
  community.network.nclu:
    commands:
        - add hostname Cumulus-1
    commit: true

- name: Add 48 interfaces and commit the change.
  community.network.nclu:
    template: |
        {% for iface in range(1,49) %}
        add int swp{{iface}}
        {% endfor %}
    commit: true
    description: "Ansible - add swps1-48"

- name: Fetch Status Of Interface
  community.network.nclu:
    commands:
        - show interface swp1
  register: output

- name: Print Status Of Interface
  ansible.builtin.debug:
    var: output

- name: Fetch Details From All Interfaces In JSON Format
  community.network.nclu:
    commands:
        - show interface json
  register: output

- name: Print Interface Details
  ansible.builtin.debug:
    var: output["msg"]

- name: Atomically add an interface
  community.network.nclu:
    commands:
        - add int swp1
    atomic: true
    description: "Ansible - add swp1"

- name: Remove IP address from interface swp1
  community.network.nclu:
    commands:
        - del int swp1 ip address 1.1.1.1/24

- name: Configure BGP AS and add 2 EBGP neighbors using BGP Unnumbered
  community.network.nclu:
    commands:
        - add bgp autonomous-system 65000
        - add bgp neighbor swp51 interface remote-as external
        - add bgp neighbor swp52 interface remote-as external
    commit: true

- name: Configure BGP AS and Add 2 EBGP neighbors Using BGP Unnumbered via Template
  community.network.nclu:
    template: |
      {% for neighbor in range(51,53) %}
      add bgp neighbor swp{{neighbor}} interface remote-as external
      add bgp autonomous-system 65000
      {% endfor %}
    atomic: true

- name: Check BGP Status
  community.network.nclu:
    commands:
        - show bgp summary json
  register: output

- name: Print BGP Status In JSON
  ansible.builtin.debug:
    var: output["msg"]
a  
changed:
    description: whether the interface was changed
    returned: changed
    type: bool
    sample: true
msg:
    description: human-readable report of success or failure
    returned: always
    type: str
    sample: "interface bond0 config updated"
N)AnsibleModulec                     | j                  d|z        \  }}}|sd|v sd|v r| j                  |xs |       t        |      S )z$Run a command, catch any nclu errorsz/usr/bin/net %sERROR)msg)run_command	fail_jsonstr)modulecommanderrmsg_rcoutput_errs         j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/network/plugins/modules/nclu.pycommand_helperr      sO     ,,->-HIS&$
g7d?V-v.v;    c                     t        | dd      }t        j                  d      }d}t        j                  ||      r+t        j                  ||      d   }|j                  |d      }|j                         S )z*Check the pending diff of the nclu buffer.pendingzKError in pending config. You may want to view `net pending` on this target.z6net add/del commands since the last ['"]net commit['"]z[94mr    )r   recompilesearchsplitreplacestrip)r   r   
delimeter1color1s       r   check_pendingr!      sd    VY0}~GXYJF	yyW%((:w/2//&"-==?r   c                 4   d}g }|r|}n|r|j                         }d}	|}
|s|rd}	|rd}
|
rt        | d       t        |       }g }|D ]5  }|j                         s|t        | |j                         d|z        gz  }7 dj	                  |      }i }t        |       }||k(  rd}nd}d|i}| j
                  rt        | d       |	rB|r@| j
                  s4t        | d|z        }d|v rd}t        | d       n t        | d	      d
k(  rd}n|
rt        | d       |||fS )NFTabortzFailed on line %s
preparedzcommit description '%s'zcommit ignoredzshow commit lastr   )
splitlinesr   r!   r   join
check_mode)r   command_listcommand_stringcommitatomicr#   description_changedcommands	do_commitdo_abortbeforeoutput_linesliner   diffafterresults                     r   run_nclur8      sR   HH	!,,.IH	Hvw' 6"FL _::<^FDJJLBUX\B\]^^L_ YY|$F D&!EE"vw' 	hv'8'8(AK(OPv%H67+F$672=H	vw'VT!!r   c                    t        t        t        dd      t        dd      t        ddd      t        ddd      t        ddd      t        ddd            d	g d
      }|j                  j                  dd       }|j                  j                  dd       }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  rd}t        |||||||      \  }}	}
||	|
d}| s |j                  di | y | r||	dS y )NFlist)requiredtyper   zAnsible-originated commit)r;   r<   defaultbool)r/   templater-   r#   r+   r,   T))r/   r?   )r+   r,   )r#   r,   )argument_specsupports_check_modemutually_exclusiver/   r?   r+   r,   r#   r-   )changedr	   r5   )rC   r	    )r   dictparamsgetr(   r8   	exit_json)testingr   r)   r*   r+   r,   r#   r-   r.   r   r5   r7   s               r   mainrJ      sJ   u62u51%e=XYE>U?U?*A !1F ==$$Z6L]]&&z48N]]x(F]]x(FMMg&E--##M2K%flNFTZ\acnoHfdF "6"	#F33 
r   __main__)N)F)
__future__r   r   r   r<   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   r   r!   r8   rJ   __name__rD   r   r   <module>rS      s]    A @2hQf
 
 4	3"l!4H zF r   