title: SaltStack macros and environment variables
body: <p>I'm trying to create a formula to manage MySQL accounts on multiple database servers. I'm trying to re-use a macro from a <code>macro.sls</code> file, and use environment variables to specify connection settings. However, putting environment files in the state files for each database server doesn't work; it only works when I put them in the <code>macro.sls</code> file. This is not desired since I want to be able to update users on multiple database servers at once. See below:</p>
  
  <pre><code># Example of "db_user" macro in mysql_user-mgmt/macro.sls
  {%- macro db_user(user, password_hash, require_state="None") %}
  {{ user }}:
    mysql_user.present:
      - connection_charset: {{ db_charset }}
      - connection_host: {{ db_host }}
      - connection_pass: {{ db_pass }}
      - connection_port: {{ db_port }}
      - connection_user: {{ db_user }}
      - host: "%"
      - password_hash: "{{ password_hash }}"
    {%- if require_state != "None" %}
      - require:
        - {{ require_state }}
    {%- endif %}
  {%- endmacro %}
  </code></pre>
  
  <p>Example db-server1.sls state file using macro</p>
  
  <pre><code># My mysql_user-mgmt/db-server1.sls file
  {%- import "mysql_user-mgmt/macro.sls" as macro -%}
  {%- set db_charset = salt['environ.get']('SERVER1_DB_CHAR') -%}
  {%- set db_host    = salt['environ.get']('SERVER1_DB_HOST') -%}
  {%- set db_pass    = salt['environ.get']('SERVER1_DB_PASS') -%}
  {%- set db_port    = salt['environ.get']('SERVER1_DB_PORT') -%}
  {%- set db_user    = salt['environ.get']('SERVER1_DB_USER') -%}
  
  {{ macro.db_user("someUser", "&lt;MYSQL HASHED PASSWORD&gt;") }}
  </code></pre>
  
  <p>...and the output then I try to run the <code>salt-call state.sls mysql_user-mgmt.db-server1</code> command</p>
  
  <pre><code># Output of running `salt-call state.sls mysql_user-mgmt.db-server1`
  local:
      - Rendering SLS 'base:mysql_user-mgmt.db-server1' failed: Jinja variable 'db_charset' is undefined
        /var/cache/salt/minion/files/base/mysql_user-mgmt/macro.sls(4):
        ---
        {%- macro db_user(user, password_hash, require_state="None") %}
        {{ user }}:
          mysql_user.present:
            - connection_charset: {{ db_charset }}    &lt;======================
            - connection_host: {{ db_host }}
            - connection_pass: {{ db_pass }}
            - connection_port: {{ db_port }}
            - connection_user: {{ db_user }}
            - host: "%"
        [...]
        ---
  </code></pre>