1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #2848 from bcoca/jabber

new module to do notifications over jabber (xmpp)
This commit is contained in:
Michael DeHaan 2013-05-05 17:57:16 -07:00
commit aaebf2e88f

115
library/notification/jabber Normal file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: jabber
short_description: Send a message to jabber user or chat room
description:
- Send a message to jabber
options:
user:
description:
User as which to connect
required: true
password:
description:
password for user to connect
required: true
to:
description:
user ID or name of the room.
required: true
msg:
description:
- The message body.
required: true
default: null
host:
description:
host to connect, overrides user info
required: false
port:
description:
port to connect to, overrides default
required: false
default: 5222
encoding:
description:
message encoding
required: false
# informational: requirements for nodes
requirements: [ xmpp ]
author: Brian Coca
'''
EXAMPLES = '''
- description: message to jabber user/room
code: jabber: user=mybot@chatserver.tld password=secret to=mychaps@chatserver.tld msg="Ansible task finished"
- description: message specifying host and port
code: jabber user=mybot@chatserver.tld host=talk.chatserver.tld port=5223 password=secret to=mychaps@chatserver.tld msg="Ansible task finished"
'''
import os
import time
HAS_XMPP = True
try:
import xmpp
except ImportError:
HAS_XMPP = False
def main():
module = AnsibleModule(
argument_spec=dict(
user=dict(required=True),
password=dict(required=True),
to=dict(required=True),
msg=dict(required=True),
host=dict(required=False),
port=dict(required=False,default=5222),
encoding=dict(required=False),
),
supports_check_mode=True
)
if not HAS_XMPP:
module.fail_json(msg="xmpp is not installed")
jid = xmpp.JID(module.params['user'])
user = jid.getNode()
server = jid.getDomain()
port = module.params['port']
if module.params['host']:
host = module.params['host']
else:
host = server
password = module.params['password']
if module.params['encoding']:
xmpp.simplexml.ENCODING = params['encoding']
try:
conn=xmpp.Client(server)
if not conn.connect(server=(host,port)):
module.fail_json(rc=1, msg='Failed to connect to server: %s' % (server))
if not conn.auth(user,password,'Ansible'):
module.fail_json(rc=1, msg='Failed to authorize %s on: %s' % (user,server))
# some old servers require this, also the sleep following send
conn.sendInitPresence(requestRoster=0)
if not module.check_mode:
conn.send(xmpp.Message(module.params['to'], module.params['msg']))
time.sleep(1)
conn.disconnect()
except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e)
changed = True
module.exit_json(changed=changed, to=module.params['to'], user=module.params['user'], msg=module.params['msg'])
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()