summaryrefslogtreecommitdiff
path: root/docs/docsite/rst/user_guide/playbooks_environment.rst
blob: a15a46dc73cbaa57ab034c0d11f389c34b8050af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
.. _playbooks_environment:

Setting the Environment (and Working With Proxies)
==================================================

.. versionadded:: 1.1

The ``environment`` keyword allows you to set an environment varaible for the action to be taken on the remote target.
For example, it is quite possible that you may need to set a proxy for a task that does http requests.
Or maybe a utility or script that are called may also need certain environment variables set to run properly.

Here is an example::

    - hosts: all
      remote_user: root

      tasks:

        - name: Install cobbler
          package:
            name: cobbler
            state: present
          environment:
            http_proxy: http://proxy.example.com:8080

.. note::
   ``environment:`` does not affect Ansible itself, ONLY the context of the specific task action and this does not include
    Ansible's own configuration settings nor the execution of any other plugins, including lookups, filters, and so on.

The environment can also be stored in a variable, and accessed like so::

    - hosts: all
      remote_user: root

      # here we make a variable named "proxy_env" that is a dictionary
      vars:
        proxy_env:
          http_proxy: http://proxy.example.com:8080

      tasks:

        - name: Install cobbler
          package:
            name: cobbler
            state: present
          environment: "{{ proxy_env }}"

You can also use it at a play level::

    - hosts: testhost

      roles:
         - php
         - nginx

      environment:
        http_proxy: http://proxy.example.com:8080

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::

    ---
    # file: group_vars/boston

    ntp_server: ntp.bos.example.com
    backup: bak.bos.example.com
    proxy_env:
      http_proxy: http://proxy.bos.example.com:8080
      https_proxy: http://proxy.bos.example.com:8080


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
        PATH: /var/local/nvm/versions/node/v4.2.1/bin:{{ ansible_env.PATH }}

      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

.. note::
   ``ansible_env:`` is normally populated by fact gathering (M(gather_facts)) and the value of the variables depends on the user
   that did the gathering action. If you change remote_user/become_user you might end up using the wrong values for those variables.

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 }}'

.. seealso::

   :ref:`playbooks_intro`
       An introduction to playbooks
   `User Mailing List <https://groups.google.com/group/ansible-devel>`_
       Have a question?  Stop by the google group!
   `irc.libera.chat <https://libera.chat/>`_
       #ansible IRC chat channel