
    Vh                         d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZ d dlmZmZ d Zd	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: ios_banner
author: Ricardo Carrillo Cruz (@rcarrillocruz)
short_description: Module to configure multiline banners.
description:
  - This will configure both login and motd banners on remote devices running Cisco
    IOS. It allows playbooks to add or remote banner text from the active running configuration.
version_added: 1.0.0
extends_documentation_fragment:
  - cisco.ios.ios
notes:
  - Tested against Cisco IOSXE Version 17.3 on CML.
  - This module works with connection C(network_cli).
    See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html)
options:
  banner:
    description:
      - Specifies which banner should be configured on the remote device. In Ansible
        2.4 and earlier only I(login) and I(motd) were supported.
    required: true
    choices:
      - login
      - motd
      - exec
      - incoming
      - slip-ppp
    type: str
  multiline_delimiter:
    description:
      - Specify the delimiting character than will be used for configuration.
    default: "@"
    type: str
  text:
    description:
      - The banner text that should be present in the remote device running configuration.  This
        argument accepts a multiline string, with no empty lines. Requires I(state=present).
    type: str
  state:
    description:
      - Specifies whether or not the configuration is present in the current devices
        active running configuration.
    default: present
    type: str
    choices:
      - present
      - absent
a  
- name: Configure the login banner
  cisco.ios.ios_banner:
    banner: login
    text: |
      this is my login banner
      that contains a multiline
      string
    state: present

- name: Remove the motd banner
  cisco.ios.ios_banner:
    banner: motd
    state: absent

- name: Configure banner from file
  cisco.ios.ios_banner:
    banner: motd
    text: "{{ lookup('file', './config_partial/raw_banner.cfg') }}"  # Use unix formatted text files (LF not CRLF) to avoid idempotency issues.
    state: present

- name: Configure the login banner using delimiter
  cisco.ios.ios_banner:
    banner: login
    multiline_delimiter: x
    text: this is my login banner
    state: present
z
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - banner login
    - this is my login banner
    - that contains a multiline
    - string
)Msearch)AnsibleModule)
get_configload_configc                    t               }| \  }}|j                  d   }|j                  j                  d      }|dk(  r:d|j                         v r(|d   r#|j	                  d|j                  d   z         |S |dk(  r|j                  d      r>t        |j                  d            dkD  r!|j                  d      j                  d	      }nd
}|d   ry|d   j                  d	      |k7  rbd|j                  d   z  }|dj                  |      z  }||d   j                  d	      z  }|dj                  |      z  }|j	                  |       |S )Nstatemultiline_delimiterabsenttextzno banner %sbannerpresent   
 z	banner %sz {0}
z
{0})	listparamsgetkeysappendlenrstripformatstrip)	updatesmodulecommandswanthaver   r   haved
banner_cmds	            h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/ios/plugins/modules/ios_banner.pymap_obj_to_commandsr&   x   s=   vHJD$MM'"E --++,ABVtyy{2tF|x)@@A O 
)	88FDHHV$4 5 9HHV$++D1EE<T&\006%?$v}}X'>>J(//*=>>J$v,,,T22J'..)<==JOOJ'O    c                 V   t        | d| j                  d   z        }|rit        d| j                  d   z   dz   |t              }|r@|j	                         }t        |j                  |      d   j                  d      d         }nd	}nd	}| j                  d   d
d}|r
||d<   d|d<   |S )z
    This function gets the banner config without stripping any whitespaces,
    and then fetches the required banner from it.
    :param module:
    :return: banner config dict object.
    z| begin banner %sr   )flagszbanner z	 \^C{1,}
r   z^C
r   Nr   )r   r   r   r   r   )r	   r   r   r   groupstrsplit)r   outregexoutputobjs        r%   map_config_to_objr1      s     V#6x9P#P
QC
y6==#::]JCQRSKKME#))E*A.44V<Q?@FF]]8,x
@CF GJr'   c                 |    | j                   d   }| j                   d   || j                   d   | j                   d   dS )Nr   r   r   r   )r   r   r   r   )r   )r   r   s     r%   map_params_to_objr3      sB    == D--)%}}-BCw'	 r'   c            
      z   t        t        dg d      t        d      t               t        dddg      	      } d
g}t        | |d      }t               }ddi}|r||d<   t        |      }t	        |      }t        ||f|      }||d<   |r|j                  st        ||       d|d<    |j                  di | y)z%main entry point for module executionT)loginmotdexecincomingzslip-ppp)requiredchoices@)defaultr   r   )r<   r:   )r   r   r   r   )r   r   )r   )argument_specrequired_ifsupports_check_modechangedFwarningsr    N )	dictr   r   r3   r1   r&   
check_moder
   	exit_json)r=   r>   r   rA   resultr!   r"   r    s           r%   mainrG      s    T+\] -V9y(.CD	M 33K# F
 vHF%zV$DV$D"D$<8H!F:  ) yFvr'   __main__N)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNrer   r   ansible.module_utils.basicr   Bansible_collections.cisco.ios.plugins.module_utils.network.ios.iosr	   r
   r&   r1   r3   rG   __name__rB   r'   r%   <module>rS      s`   $ A @ .`:

  4*0: zF r'   