-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping_addr.py
81 lines (67 loc) · 2.55 KB
/
ping_addr.py
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
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
name: ping_addr
plugin_type: inventory
version_added: '2.8'
author:
- Matt Hickok
requirements:
- python >= 2.7
extends_documentation_fragment:
- constructed
description: Creates an Ansible inventory based on a ping sweep of the network.
options:
plugin:
description: The name of this plugin, it should always be set to ping_addr for this plugin to
recognize it as it's own.
type: str
required: true
choices: ping_addr
network:
description: the network in CIDR notation
type: string
required: true
'''
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
from ansible.parsing.utils.addresses import parse_address
import os
import ipaddress
class InventoryModule(BaseInventoryPlugin, Constructable):
NAME = 'ping_addr'
def scan_network(self, network):
network_addresses = list(ipaddress.ip_network(network).hosts())
VALID_HOSTS = []
for address in network_addresses:
response = os.system("ping -c 1 " + str(address) + " > /dev/null 2>&1")
if response == 0:
VALID_HOSTS.append(str(address))
return VALID_HOSTS
def _fail(self, msg):
raise AnsibleError(msg)
def _populate(self):
raw_params = dict(
network=self.get_option('network'),
debug=None,
)
online_nodes = self.scan_network(raw_params['network'])
try:
self.nodes = online_nodes
for self.node in self.nodes:
self.inventory.add_host(self.node)
except Exception as e:
raise AnsibleError('holy shit you broke it m8: %s' %
to_native(e))
def verify_file(self, path):
"""Return the possibly of a file being consumable by this plugin."""
return (
super(InventoryModule, self).verify_file(path) and
path.endswith((self.NAME + '.yaml', self.NAME + '.yml')))
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path, cache)
self._read_config_data(path)
self._populate()