diff --git a/changelogs/fragments/error-on-undefined-environment-ansible_env.yml b/changelogs/fragments/error-on-undefined-environment-ansible_env.yml new file mode 100644 index 00000000000..bbfd42b6f04 --- /dev/null +++ b/changelogs/fragments/error-on-undefined-environment-ansible_env.yml @@ -0,0 +1,5 @@ +bugfixes: +- >- + ``environment`` keyword - env vars defined with ``ansible_env`` are no longer + silently ignored when ``ansible_env`` is undefined for actions other than + setup and gather_facts. diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 0876c0fa0d6..2a7dd0aad50 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -420,7 +420,7 @@ class Task(Base, Conditional, Taggable, CollectionSearch, Notifiable, Delegatabl return except AnsibleUndefinedVariable as e: error = to_native(e) - if self.action in C._ACTION_FACT_GATHERING and 'ansible_facts.env' in error or 'ansible_env' in error: + if self.action in C._ACTION_FACT_GATHERING and ('ansible_facts.env' in error or 'ansible_env' in error): # ignore as fact gathering is required for 'env' facts return raise diff --git a/test/integration/targets/environment/test_environment.yml b/test/integration/targets/environment/test_environment.yml index 47141e4751a..72b36fb2f21 100644 --- a/test/integration/targets/environment/test_environment.yml +++ b/test/integration/targets/environment/test_environment.yml @@ -187,3 +187,19 @@ assert: that: - test_foo.results[0].stdout == 'outer' + +- hosts: localhost + gather_facts: no + tasks: + - shell: echo $PATH + environment: + PATH: "/usr/local/bin/:{{ ansible_env.PATH }}" + ignore_errors: true + register: fatal + + - assert: + that: + - fatal is failed + - fatal.msg is search(expected) + vars: + expected: "Error processing keyword 'environment': 'ansible_env' is undefined"