Skip to content

Commit

Permalink
Automatically define ANSIBLE_HOME to ensure isolation from user env
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored and shatakshiiii committed Jan 22, 2025
1 parent fa3b556 commit 269b9e4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true
},
"flake8.importStrategy": "fromEnvironment",
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ For more information about communication, see the [Ansible communication guide](
## Features

- Promotes an "ephemeral" development approach
- Ensures the current development environment is isolated
- Ensures the current development environment is isolated by using python virtual
environments and redefining ANSIBLE_HOME to point to inside them.
- Install all collection python requirements
- Install all collection test requirements
- Checks for missing system packages
Expand Down
33 changes: 19 additions & 14 deletions src/ansible_dev_environment/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,26 @@ def ensure_isolated(self) -> None:
self.output.hint(hint)
errored = True

home_coll = Path.home() / ".ansible/collections/ansible_collections"
if home_coll.exists() and tuple(home_coll.iterdir()):
err = f"Collections found in {home_coll}"
self.output.error(err)
hint = "Run `rm -rf ~/.ansible/collections` to remove them."
self.output.hint(hint)
errored = True
ansible_home: Path
if "ANSIBLE_HOME" not in os.environ:
ansible_home = Path(os.environ.get("VIRTUAL_ENV", "~/.ansible"))
self.output.warning(
f"Declaring ANSIBLE_HOME={ansible_home} to ensure isolation from user environment.",
)
os.environ["ANSIBLE_HOME"] = str(ansible_home)
else:
ansible_home = Path(os.environ.get("ANSIBLE_HOME", "~/.ansible"))
self.output.info(
f"Using ANSIBLE_HOME={ansible_home} to ensure isolation from user environment.",
)

usr_coll = Path("/usr/share/ansible/collections")
if usr_coll.exists() and tuple(usr_coll.iterdir()):
err = f"Collections found in {usr_coll}"
self.output.error(err)
hint = "Run `sudo rm -rf /usr/share/ansible/collections` to remove them."
self.output.hint(hint)
errored = True
if "ANSIBLE_COLLECTIONS_SCAN_SYS_PATH" not in os.environ:
usr_coll = Path("/usr/share/ansible/collections")
if usr_coll.exists() and tuple(usr_coll.iterdir()):
err = f"Collections found in {usr_coll}, declaring ANSIBLE_COLLECTIONS_SCAN_SYS_PATH=False to ensure isolation."
self.output.error(err)
hint = "You can also `sudo rm -rf /usr/share/ansible/collections` to remove them or explicitly define ANSIBLE_COLLECTIONS_SCAN_SYS_PATH variable to avoid this message."
self.output.hint(hint)

if errored:
err = "The development environment is not isolated, please resolve the above errors."
Expand Down
10 changes: 4 additions & 6 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,10 @@ def test_collections_in_home(
monkeypatch.setenv("HOME", str(tmp_path))
collection_root = tmp_path / ".ansible" / "collections" / "ansible_collections"
(collection_root / "ansible" / "utils").mkdir(parents=True)
with pytest.raises(SystemExit):
main(dry=True)
main(dry=True)
captured = capsys.readouterr()
msg = f"Collections found in {collection_root}"
assert msg in captured.err
msg = "Declaring ANSIBLE_HOME="
assert msg in captured.out


def test_collections_in_user(
Expand Down Expand Up @@ -219,8 +218,7 @@ def _iterdir(path: Path) -> list[Path] | Generator[Path, None, None]:
"sys.argv",
["ansible-dev-environment", "install", "--venv", "venv"],
)
with pytest.raises(SystemExit):
main(dry=True)
main(dry=True)
captured = capsys.readouterr()
msg = f"Collections found in {usr_path}"
assert msg in captured.err
Expand Down

0 comments on commit 269b9e4

Please sign in to comment.