Skip to content

Commit

Permalink
Add local connection option
Browse files Browse the repository at this point in the history
  • Loading branch information
WStechura committed Jul 5, 2024
1 parent 07674ae commit 2571062
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
Empty file added roles/__init__.py
Empty file.
Empty file added roles/oneagent/__init__.py
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import subprocess
from typing import List

from util.test_data_types import CommandResult


def _getCommandPrefix(address: str, user: str, password: str) -> List[str]:
if address == "localhost":
return []
else:
return ["sshpass", "-p", f"{password}", "ssh", "-o", "StrictHostKeyChecking=no", f"{user}@{address}"]

class UnixCommandExecutor:
def __init__(self, user: str, password: str):
self.user = user
self.password = password

def execute(self, address: str, command: str, *args: str) -> CommandResult:
cmd = [
"sshpass",
"-p",
f"{self.password}",
"ssh",
"-o StrictHostKeyChecking=no",
f"{self.user}@{address}",
command,
*args,
]
prefix = _getCommandPrefix(address, self.user, self.password)
cmd = prefix + [command, *args]
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=False)
return CommandResult(out.returncode, out.stdout, out.stderr)


Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
PLAYBOOK_TEMPLATE_FILE_NAME,
HOSTS_TEMPLATE_FILE_NAME,
ANSIBLE_USER_KEY,
ANSIBLE_CONNECTION_KEY,
ANSIBLE_RESOURCE_DIR,
COLLECTION_DIR,
COLLECTION_NAME,
Expand Down Expand Up @@ -32,6 +33,9 @@ def _prepare_inventory_file(user: str, platforms: PlatformCollection) -> None:
for platform, hosts in platforms.items():
group_data = data["all"]["children"][platform.family()]["children"][platform.value]
group_data["hosts"] = {k: None for k in hosts}
# TODO: Replace so that both local and ssh connections can be used
if "localhost" in hosts:
group_data["vars"][ANSIBLE_CONNECTION_KEY] = "local"
group_data["vars"][ANSIBLE_USER_KEY] = user
write_yaml_file(INVENTORY_FILE, data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

ANSIBLE_USER_KEY = "ansible_user"
ANSIBLE_PASS_KEY = "ansible_password"
ANSIBLE_CONNECTION_KEY = "ansible_connection"

HOSTS_TEMPLATE_FILE_NAME = "hosts.yml"
CREDENTIALS_FILE_NAME = "credentials.yml"
Expand Down

0 comments on commit 2571062

Please sign in to comment.