`{{ ansible_env.HOME }}` resolves to `/root`, and files land in the wrong home directory
IntermediateTroubleshootingVariables, Facts and Precedence Problems
Answer
Likely causes → Facts were gathered with become: true at play level, so the environment facts describe root. ansible_env is a snapshot from fact gathering, not from the current task's user.
Fix → Look up the home directory of the user you actually mean:
- ansible.builtin.getent:
database: passwd
key: deploy
- ansible.builtin.set_fact:
deploy_home: "{{ ansible_facts.getent_passwd['deploy'][4] }}"
Or use "~deploy" paths where the module expands ~.
Prevent → Do not depend on ansible_env for paths. Make the user's home an explicit role variable.
More on Variables, Facts and Precedence Problems
Variables in `group_vars/webservers.yml` are silently ignoredBeginnerYou set `nginx_port: 8443` in inventory `group_vars`, but the role still uses `80`Intermediate`set_fact: release_tag=v2` runs, but later tasks still see `v1`Intermediate`'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'` when templating `hostvars['db01']`Intermediate`'ansible_eth0' is undefined` on new cloud instancesBeginnerFacts like `ansible_hostname` became undefined after someone changed `ansible.cfg`Intermediate