Skip to content

Commit 1a9d396

Browse files
committed
Automatically define ANSIBLE_HOME to ensure isolation from user env
1 parent 8fa5859 commit 1a9d396

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"source.fixAll": "explicit",
88
"source.organizeImports": "explicit"
99
},
10-
"editor.defaultFormatter": "ms-python.black-formatter",
10+
"editor.defaultFormatter": "charliermarsh.ruff",
1111
"editor.formatOnSave": true
1212
},
1313
"flake8.importStrategy": "fromEnvironment",

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ For more information about communication, see the [Ansible communication guide](
1616
## Features
1717

1818
- Promotes an "ephemeral" development approach
19-
- Ensures the current development environment is isolated
19+
- Ensures the current development environment is isolated by using python virtual
20+
environments and redefining ANSIBLE_HOME to point to inside them.
2021
- Install all collection python requirements
2122
- Install all collection test requirements
2223
- Checks for missing system packages

src/ansible_dev_environment/cli.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,26 @@ def ensure_isolated(self) -> None:
109109
self.output.hint(hint)
110110
errored = True
111111

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

120-
usr_coll = Path("/usr/share/ansible/collections")
121-
if usr_coll.exists() and tuple(usr_coll.iterdir()):
122-
err = f"Collections found in {usr_coll}"
123-
self.output.error(err)
124-
hint = "Run `sudo rm -rf /usr/share/ansible/collections` to remove them."
125-
self.output.hint(hint)
126-
errored = True
125+
if "ANSIBLE_COLLECTIONS_SCAN_SYS_PATH" not in os.environ:
126+
usr_coll = Path("/usr/share/ansible/collections")
127+
if usr_coll.exists() and tuple(usr_coll.iterdir()):
128+
err = f"Collections found in {usr_coll}, declaring ANSIBLE_COLLECTIONS_SCAN_SYS_PATH=False to ensure isolation."
129+
self.output.error(err)
130+
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."
131+
self.output.hint(hint)
127132

128133
if errored:
129134
err = "The development environment is not isolated, please resolve the above errors."

tests/unit/test_cli.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,10 @@ def test_collections_in_home(
163163
monkeypatch.setenv("HOME", str(tmp_path))
164164
collection_root = tmp_path / ".ansible" / "collections" / "ansible_collections"
165165
(collection_root / "ansible" / "utils").mkdir(parents=True)
166-
with pytest.raises(SystemExit):
167-
main(dry=True)
166+
main(dry=True)
168167
captured = capsys.readouterr()
169-
msg = f"Collections found in {collection_root}"
170-
assert msg in captured.err
168+
msg = "Declaring ANSIBLE_HOME="
169+
assert msg in captured.out
171170

172171

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

0 commit comments

Comments
 (0)