Hmm, are these variables being determined dynamically in the task, if not you might try setting them at the Flow Level rather than the task level and see if that works better or you could set them on the agent running the flow in you local execution environment
https://docs-v1.prefect.io/orchestration/getting-started/flow-configs.html#configure-environment-variables
To be perfectly transparent I'm not 100% sure on why the environment variables would work sometimes and not others, instinctually I think it's tied to the fact that the task run context for the different tasks are technically separate but it seems odd it would work sometimes in that case, either way though I think defining these at the flow level or ideally prior to the flow actually running in your execution environment would be more stable.