Ansible — Tips

Case

Get the HOME environment variable of the executing user during sudo with ansible.

When sudo: yes using with ansible, it {{ ansible_env.HOME }}becomes root’s. This is a problem when you want to place files under the home directory of the execution user, such as when building a development environment. In ansible, the execution user name is {{ ansible_ssh_user }}set to, so you can use this to get the HOME environment variable of the execution user and use it.

By pre_taskexecuting the above task with , {{ ansible_home }}the HOME environment variable of the executing user is set, so you can use it from other tasks.

By the way, {{ ansible_ssh_user }}it is connection: local available without a problem because it is set even when.

Reference

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store