Merge "Adding environment variables for salt minion"
diff --git a/.kitchen.yml b/.kitchen.yml
index fe8de71..888f83f 100644
--- a/.kitchen.yml
+++ b/.kitchen.yml
@@ -86,6 +86,7 @@
         minion_local_pillar.sls: tests/pillar/minion_local_pillar.sls
         minion_local_reclass.sls: tests/pillar/minion_local_reclass.sls
         minion_backend_urllib.sls: tests/pillar/minion_backend_urllib.sls
+        minion_env_vars.sls: tests/pillar/minion_env_vars.sls
 
   - name: master-default
     provisioner:
diff --git a/README.rst b/README.rst
index 705efd4..60c5c7b 100644
--- a/README.rst
+++ b/README.rst
@@ -565,6 +565,15 @@
       minion:
         backend: urllib2
 
+Salt minion using environment variables defined in /etc/default/salt-minion for
+upstart based Ubuntu and in /etc/environment for Ubuntu with systemd.
+
+.. code-block:: yaml
+
+    salt:
+      minion:
+        env_vars:
+          engine: file
 
 Salt minion with PKI certificate authority (CA)
 
diff --git a/salt/files/etc_default_salt-minion b/salt/files/etc_default_salt-minion
new file mode 100644
index 0000000..bbd8199
--- /dev/null
+++ b/salt/files/etc_default_salt-minion
@@ -0,0 +1,7 @@
+{%- from "salt/map.jinja" import minion with context -%}
+{%- if minion.env_vars.variables is defined -%}
+{%- for envvar_name, envvar_value in minion.env_vars.get('variables', {}).iteritems() %}
+export {{ envvar_name }}={% if envvar_value is string -%}{{ envvar_value }}{%- else -%}{{ envvar_value|join(',') }}
+{%- endif %}
+{%- endfor %}
+{%- endif %}
diff --git a/salt/files/systemd_minion_override.conf b/salt/files/systemd_minion_override.conf
new file mode 100644
index 0000000..cf52581
--- /dev/null
+++ b/salt/files/systemd_minion_override.conf
@@ -0,0 +1,2 @@
+[Service]
+EnvironmentFile=/etc/environment
diff --git a/salt/map.jinja b/salt/map.jinja
index e238877..68a7cf2 100644
--- a/salt/map.jinja
+++ b/salt/map.jinja
@@ -148,6 +148,17 @@
 }, merge=pillar.salt.minion.get('proxy_minion', {})) %}
 {%- endif %}
 
+{%- if pillar.salt.minion.env_vars is defined %}
+{% set env_vars = salt['grains.filter_by']({
+    'Debian': {
+        'engine': 'file'
+    },
+    'RedHat': {
+        'engine': 'file'
+    },
+}, merge=pillar.salt.minion.get('env_vars', {})) %}
+{%- endif %}
+
 {%- endif %}
 
 
diff --git a/salt/minion/env_vars.sls b/salt/minion/env_vars.sls
new file mode 100644
index 0000000..e4d2680
--- /dev/null
+++ b/salt/minion/env_vars.sls
@@ -0,0 +1,32 @@
+{%- from "salt/map.jinja" import minion,env_vars with context %}
+
+{%- if env_vars.engine is defined %}
+{%- if env_vars.engine == 'file' %}
+
+/etc/default/salt-minion:
+  file.managed:
+  - source: salt://salt/files/etc_default_salt-minion
+  - user: root
+  - group: root
+  - template: jinja
+  - require:
+    - {{ minion.install_state }}
+  - onchanges_in:
+    - cmd: salt_minion_service_restart
+
+{%- if grains.get('init', None) == 'systemd' %}
+/etc/systemd/system/salt-minion.service.d/override.conf:
+  file.managed:
+  - source: salt://salt/files/systemd_minion_override.conf
+  - user: root
+  - makedirs: True
+  - group: root
+  - template: jinja
+  - require:
+    - {{ minion.install_state }}
+  - onchanges_in:
+    - cmd: salt_minion_service_restart
+
+{%- endif %}
+{%- endif %}
+{%- endif %}
diff --git a/salt/minion/init.sls b/salt/minion/init.sls
index 3439ce1..0575952 100644
--- a/salt/minion/init.sls
+++ b/salt/minion/init.sls
@@ -11,3 +11,6 @@
 {%- if pillar.salt.minion.proxy_minion is defined %}
 - salt.minion.proxy
 {%- endif %}
+{%- if pillar.salt.minion.env_vars is defined %}
+- salt.minion.env_vars
+{%- endif %}
diff --git a/tests/pillar/minion_env_vars.sls b/tests/pillar/minion_env_vars.sls
new file mode 100644
index 0000000..0578e96
--- /dev/null
+++ b/tests/pillar/minion_env_vars.sls
@@ -0,0 +1,5 @@
+salt:
+  minion:
+    enabled: true
+    env_vars:
+      engine: file