-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
executable file
·36 lines (29 loc) · 1.09 KB
/
environment.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
import os
import sys
from ansible.plugins.inventory import BaseInventoryPlugin
class InventoryModule(BaseInventoryPlugin):
NAME = 'benfiola.dotfiles.environment'
TYPE = "generator"
def verify_file(self, path):
return any([
"LOCAL" in os.environ,
"REMOTE_IP" in os.environ
])
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path, cache=cache)
self.parse_from_environment()
def parse_from_environment(self):
host_vars = {}
is_local = os.environ.get("LOCAL")
remote_ip = os.environ.get("REMOTE_IP")
if is_local:
host = "localhost"
host_vars["ansible_connection"] = "local"
host_vars["ansible_python_interpreter"] = sys.executable
elif remote_ip:
host = remote_ip
else:
raise ValueError(f"host undefined")
self.inventory.add_host(host)
for key, value in host_vars.items():
self.inventory.set_variable(host, key, value)