
    Vh                          d dl mZ dZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ 	 d dlZdZ G d	 d
e      Zy# e$ r dZY w xY w)    )annotationsa  
author: Unknown (!UNKNOWN)
name: slack
type: notification
requirements:
  - whitelist in configuration
  - prettytable (python library)
short_description: Sends play events to a Slack channel
description:
  - This is an ansible callback plugin that sends status updates to a Slack channel during playbook execution.
options:
  http_agent:
    description:
      - HTTP user agent to use for requests to Slack.
    type: string
    version_added: "10.5.0"
  webhook_url:
    required: true
    description: Slack Webhook URL.
    type: str
    env:
      - name: SLACK_WEBHOOK_URL
    ini:
      - section: callback_slack
        key: webhook_url
  channel:
    default: "#ansible"
    description: Slack room to post in.
    type: str
    env:
      - name: SLACK_CHANNEL
    ini:
      - section: callback_slack
        key: channel
  username:
    description: Username to post as.
    type: str
    env:
      - name: SLACK_USERNAME
    default: ansible
    ini:
      - section: callback_slack
        key: username
  validate_certs:
    description: Validate the SSL certificate of the Slack server for HTTPS URLs.
    env:
      - name: SLACK_VALIDATE_CERTS
    ini:
      - section: callback_slack
        key: validate_certs
    default: true
    type: bool
N)context)open_url)CallbackBaseTFc                  X     e Zd ZdZdZdZdZdZd fd	Zd fd	Z	d Z
d	 Zd
 Zd Z xZS )CallbackModulezsThis is an ansible callback plugin that sends status
    updates to a Slack channel during playbook execution.
    g       @notificationzcommunity.general.slackTc                    t         t        |   |       t        s"d| _        | j
                  j                  d       d | _        t        j                         j                  d d | _        y )N)displayTzVThe `prettytable` python module is not installed. Disabling the Slack callback plugin.   )superr   __init__HAS_PRETTYTABLEdisabled_displaywarningplaybook_nameuuiduuid4hexguid)selfr   	__class__s     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/callback/slack.pyr   zCallbackModule.__init__X   s\    nd,W,= DMMM!! #, - "
 JJL$$Ra(	    c                   t         t        |   |||       | j                  d      | _        | j                  d      | _        | j                  d      | _        | j                  j                  dkD  | _	        | j                  d      | _
        | j                  d      | _        | j                  #d| _        | j                  j                  d	       y y )
N)	task_keysvar_optionsdirectwebhook_urlchannelusername   validate_certs
http_agentTz}Slack Webhook URL was not provided. The Slack Webhook URL can be provided using the `SLACK_WEBHOOK_URL` environment variable.)r   r   set_options
get_optionr    r!   r"   r   	verbosityshow_invocationr$   r%   r   r   )r   r   r   r   r   s       r   r&   zCallbackModule.set_optionsi   s    nd/)Q\ek/l??=9y1
3 $ 7 7! ;"oo.>?//,7# DMMM!! #. / $r   c                   ddi}| j                   | j                  |ddd}t        j                  |      }| j                  j                  |       | j                  j                  | j                         	 t        | j                  || j                  || j                        }|j                         S # t        $ r(}| j                  j                  d|        Y d }~y d }~ww xY w)NzContent-typezapplication/jsonnonezHhttps://cdn2.hubspot.net/hub/330046/file-449187601-png/ansible_badge.png)r!   r"   attachmentsparseicon_url)datar$   headersr%   z#Could not submit message to Slack: )r!   r"   jsondumpsr   debugr    r   r$   r%   read	Exceptionr   )r   r,   r0   payloadr/   responsees          r   send_msgzCallbackModule.send_msgz   s    .

 ||&@
 zz'"D!D,,-
	M  #22??H ==?" 	MMM!!$Gs"KLL	Ms   7=B5 5	C&>C!!C&c                   t         j                  j                  |j                        | _        d| j
                   dg}g }t        j                  rj| j                  r]t        j                  d   }t        j                  d   }t        j                  d   }t        j                  d   }t        j                  d   D cg c]!  }t         j                  j                  |      # }	}|j                  dd	j                  |	              |r)|d
gk7  r#|j                  dd	j                  |              |r#|j                  dd	j                  |              |r|j                  d|        |r#|j                  ddj                  |              |j                  dt        j                  d    d       |j                  d| j                   d       dj                  |      g}
|r&dj                  |      }|
j                  d| d       dj                  |
      }|d|igdg ddg}| j                  |       y c c}w )Nz*Playbook initiated* (__)tags	skip_tags
extra_varssubset	inventoryzInventory:  z, allzTags:       zSkip Tags:  zLimit:      zExtra Vars:  zby *remote_user*z

*
```

```valuer   textfallbackfieldsrK   rL   color	mrkdwn_inr,   )ospathbasename
_file_namer   r   r   CLIARGSr)   abspathappendjoinr9   )r   playbooktitleinvocation_itemsr<   r=   r>   r?   ir@   	msg_items	_inv_itemmsgr,   s                 r   v2_playbook_on_startz#CallbackModule.v2_playbook_on_start   s   WW--h.A.AB &dii[3
 ??t33??6*D4I 6J__X.F5<__[5QR+RIR##l499Y3G2H$IJ '',tyy6G(HI '',tyy7K6L(MN '',vh(?@ '',sxx
7K6L(MNLL4 >?qABuT//023XXe_%			"23IuYKu56ii	"  S
 7	
 	 	+.C Ss   &Ic                    |j                   xs d|j                   d}d| j                   d| d}||dg ddg}| j                  |	       y
)zDisplay Play start messageszPlay name not specified ()z*Starting play* (_z_)

*rD   r   rI   )rK   rJ   rN   rO   rP   N)name_uuidr   r9   )r   playrc   r_   r,   s        r   v2_playbook_on_play_startz(CallbackModule.v2_playbook_on_play_start   sb     yyE7

|1E"499+WTF!<  ";	
 	+.r   c           	     P   t        |j                  j                               }t        j                  g d      }d}d}|D ]M  }|j                  |      }|d   dkD  rd}|d   dkD  rd}|j                  |gdD cg c]  }||   	 c}z          O g }	d| j                   d	g}
|s|rd
}|
j                  d       nd}|
j                  d       |
j                  d| d       dj                  |
      }|	j                  |d|ig|g dd       | j                  |	       yc c}w )z&Display info about playbook statistics)HostOkChangedUnreachableFailuresRescuedIgnoredFfailuresr   Tunreachable)okchangedrp   ro   rescuedignoredz*Playbook Complete* (_r;   dangerz

*Failed!*goodz
*Success!*rF   rG   rE   rH   rI   rM   rP   N)sorted	processedkeysprettytablePrettyTable	summarizeadd_rowr   rW   rX   r9   )r   statshoststro   rp   hskr,   r]   rN   r_   s                r   v2_playbook_on_statsz#CallbackModule.v2_playbook_on_stats   sg    u++-.## %G H  		PA"A}q !#"IIqc ,N OaQqT O O P		P $TYYKr2
	 {E]+E^,55)*ii	" S
 7	
 		 	+.;Os   :D#)N)NNN)__name__
__module____qualname____doc__CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMECALLBACK_NEEDS_WHITELISTr   r&   r9   r`   rf   r   __classcell__)r   s   @r   r   r   O   s@     "M-M#)"/"M:./`/0/r   r   )
__future__r   DOCUMENTATIONr1   rQ   r   ansibler   ansible.module_utils.urlsr   ansible.plugins.callbackr   rz   r   ImportErrorr    r   r   <module>r      sW    #4l  	   . 1O
w/\ w/	  Os   : AA