-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckIfPortIsOpen.yml
More file actions
72 lines (57 loc) · 1.82 KB
/
Copy pathcheckIfPortIsOpen.yml
File metadata and controls
72 lines (57 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
# Executes a Python script which is embedded in YAML and
# check if the given port of the given host is open in the firewall.
#
# To pass the parameters host and port define in the AAP surveys
# with the host as type text and the port as type integer.
# Or execute it with
# ansible-playbook checkIfPortIsOpen.yml -e in_host=127.0.0.1 -e in_port=443
#
# @author Stefan Schnell <mail@stefan-schnell.de>
# @license MIT
# @version 0.2.0
- name: Executes inline Python script and check if port at host is open
hosts: localhost
gather_facts: false
vars:
in_host: "127.0.0.1"
in_port: 443
python_code: |
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
def main():
host: str = "{{ in_host }}"
port: int = {{ in_port }}
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((host, port))
if result == 0:
print("Port is open")
else:
print("Port is not open")
except Exception as err:
print("Error:", repr(err))
finally:
sock.close()
if __name__ == "__main__":
main()
tasks:
- name: Create Python script
ansible.builtin.copy:
content: "{{ python_code }}"
dest: /tmp/checkPort.py
follow: true
mode: "0777"
- name: Execute Python script
ansible.builtin.command:
cmd: /usr/bin/python3 /tmp/checkPort.py
changed_when: result.rc != 0
register: result
- name: Print result of the Python script
ansible.builtin.debug:
msg: "{{ result.stdout_lines }}"
- name: Delete Python script
ansible.builtin.file:
path: /tmp/checkPort.py
state: absent