Add zuul legacy vars filter

Change-Id: I087bd0de3356a8606fe40d4762e620e4d465a719
diff --git a/tests/fixtures/config/ansible/git/common-config/playbooks/check-vars.yaml b/tests/fixtures/config/ansible/git/common-config/playbooks/check-vars.yaml
index 9bfeb0e..e6bd5ef 100644
--- a/tests/fixtures/config/ansible/git/common-config/playbooks/check-vars.yaml
+++ b/tests/fixtures/config/ansible/git/common-config/playbooks/check-vars.yaml
@@ -24,6 +24,12 @@
           - zuul.project.canonical_name == 'review.example.com/org/project'
           - zuul.project.src_dir == 'src/review.example.com/org/project'
 
+    - name: Assert legacy zuul vars are valid
+      assert:
+        that:
+          - zuul.project.name == '{{ (zuul | zuul_legacy_vars).ZUUL_PROJECT }}'
+          - zuul.branch == '{{ (zuul | zuul_legacy_vars).ZUUL_BRANCH }}'
+
     - debug:
         msg: "vartest secret {{ vartest_secret }}"
 
diff --git a/zuul/ansible/filter/zuul_filters.py b/zuul/ansible/filter/zuul_filters.py
new file mode 100644
index 0000000..4304d51
--- /dev/null
+++ b/zuul/ansible/filter/zuul_filters.py
@@ -0,0 +1,63 @@
+# Copyright 2017 Red Hat, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+def zuul_legacy_vars(zuul):
+    # omitted:
+    # ZUUL_URL
+    # ZUUL_REF
+    # ZUUL_COMMIT
+
+    short_name = zuul['project']['name'].split('/')[-1]
+    params = dict(ZUUL_UUID=zuul['build'],
+                  ZUUL_PROJECT=zuul['project']['name'],
+                  ZUUL_SHORT_PROJECT_NAME=short_name,
+                  ZUUL_PIPELINE=zuul['pipeline'],
+                  ZUUL_VOTING=zuul['voting'],
+                  WORKSPACE='/home/zuul')
+    if 'branch' in zuul:
+        params['ZUUL_BRANCH'] = zuul['branch']
+
+    if 'change' in zuul:
+        changes_str = '^'.join(
+            ['%s:%s:refs/changes/%s/%s/%s' % (
+                i['project']['name'],
+                i['branch'],
+                str(i['change'])[:-2:],
+                i['change'],
+                i['patchset'])
+             for i in zuul['items']])
+        params['ZUUL_CHANGES'] = changes_str
+
+        change_ids = ' '.join(['%s,%s' % (i['change'], i['patchset'])
+                               for i in zuul['items']])
+        params['ZUUL_CHANGE_IDS'] = change_ids
+        params['ZUUL_CHANGE'] = str(zuul['change'])
+        params['ZUUL_PATCHSET'] = str(zuul['patchset'])
+
+    if 'newrev' in zuul or 'oldrev' in zuul:
+        params['ZUUL_REFNAME'] = zuul['ref']
+        params['ZUUL_OLDREV'] = zuul.get('oldrev', '0' * 40)
+        params['ZUUL_NEWREV'] = zuul.get('newrev', '0' * 40)
+
+    params['TOX_TESTENV_PASSENV'] = ' '.join(params.keys())
+    return params
+
+
+class FilterModule(object):
+
+    def filters(self):
+        return {
+            'zuul_legacy_vars': zuul_legacy_vars,
+        }
diff --git a/zuul/executor/server.py b/zuul/executor/server.py
index 0e56656..27fd85f 100644
--- a/zuul/executor/server.py
+++ b/zuul/executor/server.py
@@ -567,6 +567,7 @@
         self.action_dir = os.path.join(plugin_dir, 'action')
         self.callback_dir = os.path.join(plugin_dir, 'callback')
         self.lookup_dir = os.path.join(plugin_dir, 'lookup')
+        self.filter_dir = os.path.join(plugin_dir, 'filter')
 
         _copy_ansible_files(zuul.ansible, plugin_dir)
 
@@ -1446,6 +1447,8 @@
             config.write('command_warnings = False\n')
             config.write('callback_plugins = %s\n' % callback_path)
             config.write('stdout_callback = zuul_stream\n')
+            config.write('filter_plugins = %s\n'
+                         % self.executor_server.filter_dir)
             # bump the timeout because busy nodes may take more than
             # 10s to respond
             config.write('timeout = 30\n')