+

Search Tips   |   Advanced Search

Setting the remote environment

You can use the environment keyword at the play, block, or task level to set an environment variable for an action on a remote host. With this keyword, you can enable using a proxy for a task that does http requests, set the required environment variables for language-specific version managers, and more.

When you set a value with environment: at the play or block level, it is available only to tasks within the play or block that are executed by the same user. The environment: keyword does not affect Ansible itself, Ansible configuration settings, the environment for other users, or the execution of other plugins like lookups and filters. Variables set with environment: do not automatically become Ansible facts, even when you set them at the play level. You must include an explicit gather_facts task in your playbook and set the environment keyword on that task to turn these values into Ansible facts.


Setting the remote environment in a task

You can set the environment directly at the task level:

You can re-use environment settings by defining them as variables in your play and accessing them in a task as you would access any stored Ansible variable:

You can store environment settings for re-use in multiple playbooks by defining them in a group_vars file:

You can set the remote environment at the play level:

These examples show proxy settings, but you can provide any number of settings this way.


Working with language-specific version managers

Some language-specific version managers (such as rbenv and nvm) require you to set environment variables while these tools are in use. When using these tools manually, you usually source some environment variables from a script or from lines added to your shell configuration file. In Ansible, you can do this with the environment keyword at the play level:

The example above uses ansible_env as part of the PATH. Basing variables on ansible_env is risky. Ansible populates ansible_env values by gathering facts, so the value of the variables depends on the remote_user or become_user Ansible used when gathering those facts. If you change remote_user/become_user the values in ansible-env may not be the ones you expect.

Warning

Environment variables are normally passed in clear text (shell plugin dependent) so they are not a recommended way of passing secrets to the module being executed.

You can also specify the environment at the task level:


Next Previous