mirror of
https://github.com/geerlingguy/ansible-for-devops.git
synced 2024-05-19 06:50:03 +00:00
70 lines
2.0 KiB
Python
Executable File
70 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
'''
|
|
Example custom dynamic inventory script for Ansible, in Python.
|
|
'''
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import json
|
|
|
|
class ExampleInventory(object):
|
|
|
|
def __init__(self):
|
|
self.inventory = {}
|
|
self.read_cli_args()
|
|
|
|
# Called with `--list`.
|
|
if self.args.list:
|
|
self.inventory = self.example_inventory()
|
|
# Called with `--host [hostname]`.
|
|
elif self.args.host:
|
|
# Not implemented, since we return _meta info `--list`.
|
|
self.inventory = self.empty_inventory()
|
|
# If no groups or vars are present, return empty inventory.
|
|
else:
|
|
self.inventory = self.empty_inventory()
|
|
|
|
print(json.dumps(self.inventory));
|
|
|
|
# Example inventory for testing.
|
|
def example_inventory(self):
|
|
return {
|
|
'group': {
|
|
'hosts': ['192.168.56.71', '192.168.56.72'],
|
|
'vars': {
|
|
'ansible_user': 'vagrant',
|
|
'ansible_ssh_private_key_file':
|
|
'~/.vagrant.d/insecure_private_key',
|
|
'ansible_python_interpreter':
|
|
'/usr/bin/python3',
|
|
'example_variable': 'value'
|
|
}
|
|
},
|
|
'_meta': {
|
|
'hostvars': {
|
|
'192.168.56.71': {
|
|
'host_specific_var': 'foo'
|
|
},
|
|
'192.168.56.72': {
|
|
'host_specific_var': 'bar'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Empty inventory for testing.
|
|
def empty_inventory(self):
|
|
return {'_meta': {'hostvars': {}}}
|
|
|
|
# Read the command line args passed to the script.
|
|
def read_cli_args(self):
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--list', action = 'store_true')
|
|
parser.add_argument('--host', action = 'store')
|
|
self.args = parser.parse_args()
|
|
|
|
# Get the inventory.
|
|
ExampleInventory()
|