2018-02-24 00:10:47 +01:00
.. _playbooks_environment:
2013-02-23 20:34:29 +01:00
Setting the Environment (and Working With Proxies)
2013-09-29 22:47:34 +02:00
==================================================
2013-02-23 20:34:29 +01:00
2013-08-23 18:33:57 +02:00
.. versionadded :: 1.1
2013-02-23 20:34:29 +01:00
It is quite possible that you may need to get package updates through a proxy, or even get some package
2019-06-26 23:07:27 +02:00
updates through a proxy and access other packages not through a proxy. Or maybe a script you might wish to
2013-10-05 19:50:53 +02:00
call may also need certain environment variables set to run properly.
2013-10-03 04:02:11 +02:00
2019-01-23 20:54:05 +01:00
Ansible makes it easy for you to configure the remote execution environment by using the 'environment' keyword. Here is an example::
2013-02-23 20:34:29 +01:00
2013-02-23 21:17:50 +01:00
- hosts: all
2013-09-07 23:19:23 +02:00
remote_user: root
2013-02-23 20:34:29 +01:00
2013-02-23 21:17:50 +01:00
tasks:
2013-02-23 20:34:29 +01:00
2019-03-11 18:13:52 +01:00
- name: Install cobbler
package:
name: cobbler
state: present
2013-02-23 21:17:50 +01:00
environment:
http_proxy: http://proxy.example.com:8080
2013-02-23 20:34:29 +01:00
The environment can also be stored in a variable, and accessed like so::
2013-02-23 21:17:50 +01:00
- hosts: all
2013-09-07 23:19:23 +02:00
remote_user: root
2013-02-23 20:34:29 +01:00
2014-04-11 17:48:51 +02:00
# here we make a variable named "proxy_env" that is a dictionary
2013-02-23 21:17:50 +01:00
vars:
proxy_env:
2013-02-28 00:39:08 +01:00
http_proxy: http://proxy.example.com:8080
2013-02-23 20:34:29 +01:00
2013-02-23 21:17:50 +01:00
tasks:
2013-02-23 22:47:47 +01:00
2019-03-11 18:13:52 +01:00
- name: Install cobbler
package:
name: cobbler
state: present
2015-12-11 15:32:19 +01:00
environment: "{{proxy_env}}"
2013-02-23 20:34:29 +01:00
2016-09-28 16:21:11 +02:00
You can also use it at a play level::
2015-10-22 13:07:54 +02:00
- hosts: testhost
roles:
- php
- nginx
environment:
http_proxy: http://proxy.example.com:8080
2013-02-23 21:17:50 +01:00
While just proxy settings were shown above, any number of settings can be supplied. The most logical place
to define an environment hash might be a group_vars file, like so::
2013-02-23 20:34:29 +01:00
2013-05-31 10:54:13 +02:00
---
2013-02-23 21:17:50 +01:00
# file: group_vars/boston
ntp_server: ntp.bos.example.com
backup: bak.bos.example.com
proxy_env:
2013-02-28 00:39:08 +01:00
http_proxy: http://proxy.bos.example.com:8080
https_proxy: http://proxy.bos.example.com:8080
2013-02-23 20:34:29 +01:00
2016-10-24 04:39:05 +02:00
Working With Language-Specific Version Managers
===============================================
Some language-specific version managers (such as rbenv and nvm) require environment variables be set while these tools are in use. When using these tools manually, they usually require sourcing some environment variables via a script or lines added to your shell configuration file. In Ansible, you can instead use the environment directive::
---
### A playbook demonstrating a common npm workflow:
# - Check for package.json in the application directory
# - If package.json exists:
# * Run npm prune
# * Run npm install
- hosts: application
become: false
vars:
node_app_dir: /var/local/my_node_app
environment:
NVM_DIR: /var/local/nvm
2017-04-10 18:16:47 +02:00
PATH: /var/local/nvm/versions/node/v4.2.1/bin:{{ ansible_env.PATH }}
2016-10-24 04:39:05 +02:00
tasks:
- name: check for package.json
stat:
path: '{{ node_app_dir }}/package.json'
register: packagejson
- name: npm prune
command: npm prune
args:
chdir: '{{ node_app_dir }}'
when: packagejson.stat.exists
- name: npm install
npm:
path: '{{ node_app_dir }}'
when: packagejson.stat.exists
You might also want to simply specify the environment for a single task::
---
- name: install ruby 2.3.1
command: rbenv install {{ rbenv_ruby_version }}
args:
creates: '{{ rbenv_root }}/versions/{{ rbenv_ruby_version }}/bin/ruby'
vars:
rbenv_root: /usr/local/rbenv
rbenv_ruby_version: 2.3.1
environment:
CONFIGURE_OPTS: '--disable-install-doc'
RBENV_ROOT: '{{ rbenv_root }}'
PATH: '{{ rbenv_root }}/bin:{{ rbenv_root }}/shims:{{ rbenv_plugins }}/ruby-build/bin:{{ ansible_env.PATH }}'
2016-07-06 17:18:20 +02:00
.. note ::
2019-01-23 20:54:05 +01:00
`` environment: `` does not affect Ansible itself, ONLY the context of the specific task action and this does not include Ansible's own configuration settings.
2016-07-06 17:18:20 +02:00
2013-10-05 18:31:16 +02:00
.. seealso ::
2019-06-26 23:07:27 +02:00
:ref: `playbooks_intro`
2013-10-05 18:31:16 +02:00
An introduction to playbooks
2018-07-21 15:48:47 +02:00
`User Mailing List <https://groups.google.com/group/ansible-devel> `_
2013-10-05 18:31:16 +02:00
Have a question? Stop by the google group!
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel