33 lines
763 B
Python
Executable File
33 lines
763 B
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import subprocess
|
|
|
|
def get_hosts():
|
|
# Use nmap to discover hosts
|
|
result = subprocess.run(['nmap', '-sn', '192.168.1.0/24'], stdout=subprocess.PIPE)
|
|
output = result.stdout.decode()
|
|
|
|
hosts = []
|
|
for line in output.split('\n'):
|
|
if 'Nmap scan report for' in line:
|
|
hosts.append(line.split(' ')[-1])
|
|
return hosts
|
|
|
|
def main():
|
|
hosts = get_hosts()
|
|
inventory = {
|
|
'all': {
|
|
'hosts': hosts,
|
|
'vars': {
|
|
'ansible_connection': 'ssh',
|
|
'ansible_user': 'alex',
|
|
'ansible_ssh_private_key_file': '~/.ssh/id_ed25519'
|
|
}
|
|
}
|
|
}
|
|
print(json.dumps(inventory))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|