Last active
March 31, 2021 13:15
-
-
Save naingyeminn/230a140df1d8debd39e37b6849f08aa2 to your computer and use it in GitHub Desktop.
Ansible - Listen module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: servera.example.com | |
tasks: | |
- name: serverB listen on port 8888 | |
listen: | |
port: "8888" | |
async: 5 | |
poll: 0 | |
delegate_to: serverb.example.com | |
- name: connect from serverA to port 8888 of serverB | |
wait_for: | |
host: serverb.example.com | |
port: 8888 | |
state: started | |
delay: 0 | |
timeout: 5 | |
ignore_errors: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
DOCUMENTATION = ''' | |
--- | |
module: listen | |
short_description: create socket to listen on custom port | |
description: create socket to listen on custom port | |
''' | |
EXAMPLES = ''' | |
- name: listen on port 8888 | |
listen: | |
port: 8888 | |
async: 10 | |
poll: 0 | |
''' | |
import socket | |
import sys | |
import time | |
from ansible.module_utils.basic import * | |
def main(): | |
fields = { | |
"port": {"required": True, "type": "str"}, | |
} | |
module = AnsibleModule(argument_spec=fields) | |
response = {"socket": "listen on " + module.params['port']} | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_address = ('', int(module.params['port'])) | |
sock.bind(server_address) | |
sock.listen(1) | |
while True: | |
connection, client_address = sock.accept() | |
time.sleep(5) | |
connection.close() | |
break | |
module.exit_json(changed=False, meta=response) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment