Skip to content

Commit 8530b32

Browse files
committed
Pass env into facts from the current operation.
This means facts will correctly use any `env` provided inside the current operation executing. There is also a follow up here to relook at the whole operation kwarg/fact interaction to dedupe a lot of the logic currently duplicated in facts/operations APIs.
1 parent d761cef commit 8530b32

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pyinfra/api/facts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def get_facts(
158158
ignore_errors = state.config.IGNORE_ERRORS
159159
shell_executable = state.config.SHELL
160160
use_sudo_password = state.config.USE_SUDO_PASSWORD
161+
env = state.config.ENV
161162

162163
# Facts can override the shell (winrm powershell vs cmd support)
163164
if fact.shell_executable:
@@ -175,10 +176,11 @@ def get_facts(
175176
su_user = current_global_kwargs['su_user']
176177
ignore_errors = current_global_kwargs['ignore_errors']
177178
timeout = current_global_kwargs['timeout']
179+
env = current_global_kwargs['env']
178180

179181
# Make a hash which keeps facts unique - but usable cross-deploy/threads.
180182
# Locks are used to maintain order.
181-
fact_hash = make_hash((name, kwargs, sudo, sudo_user, su_user, ignore_errors))
183+
fact_hash = make_hash((name, kwargs, sudo, sudo_user, su_user, ignore_errors, env))
182184

183185
# Already got this fact? Unlock and return them
184186
current_facts = state.facts.get(fact_hash, {})
@@ -223,6 +225,7 @@ def get_facts(
223225
use_sudo_password=use_sudo_password,
224226
su_user=su_user,
225227
timeout=timeout,
228+
env=env,
226229
shell_executable=shell_executable,
227230
print_output=state.print_fact_output,
228231
print_input=state.print_fact_input,

tests/test_api/test_api_facts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_get_fact(self):
3737
sudo=False,
3838
sudo_user=None,
3939
timeout=None,
40+
env={},
4041
use_sudo_password=False,
4142
return_combined_output=True,
4243
)
@@ -55,6 +56,7 @@ def test_get_fact_current_op_meta(self):
5556
'su_user': 'someuser',
5657
'ignore_errors': False,
5758
'timeout': 10,
59+
'env': {'HELLO': 'WORLD'},
5860
}
5961

6062
with patch('pyinfra.api.connectors.ssh.run_shell_command') as fake_run_command:
@@ -74,6 +76,7 @@ def test_get_fact_current_op_meta(self):
7476
sudo=True,
7577
sudo_user='someuser',
7678
timeout=10,
79+
env={'HELLO': 'WORLD'},
7780
use_sudo_password=True,
7881
return_combined_output=True,
7982
)
@@ -105,6 +108,7 @@ def test_get_fact_error(self):
105108
sudo=False,
106109
sudo_user=None,
107110
timeout=None,
111+
env={},
108112
use_sudo_password=False,
109113
return_combined_output=True,
110114
)
@@ -123,6 +127,7 @@ def test_get_fact_error_ignore(self):
123127
'su_user': None,
124128
'ignore_errors': True,
125129
'timeout': None,
130+
'env': {},
126131
}
127132

128133
with patch('pyinfra.api.connectors.ssh.run_shell_command') as fake_run_command:
@@ -142,6 +147,7 @@ def test_get_fact_error_ignore(self):
142147
sudo=False,
143148
sudo_user=None,
144149
timeout=None,
150+
env={},
145151
use_sudo_password=False,
146152
return_combined_output=True,
147153
)

0 commit comments

Comments
 (0)