Skip to content

Commit 2e29353

Browse files
committed
FEAT : join filter
1 parent ac4e10b commit 2e29353

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

examples/join-filter-example.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
3+
# This playbook example contains simple use cases that illustrate the basics functionality of join filter.
4+
5+
- name: Examples
6+
gather_facts: false
7+
hosts: localhost
8+
vars:
9+
path: /tmp
10+
tree:
11+
- path: foo
12+
- path: foo/bar
13+
content: |
14+
Bar
15+
- path: foo/baz
16+
template: template.j2
17+
- path: qux
18+
state: absent
19+
tasks:
20+
21+
- name: Tree with simple path
22+
manala.path.path:
23+
path: "{{ item.path }}"
24+
state: "{{ item.state | default(omit) }}"
25+
content: "{{ item.content | default(omit) }}"
26+
template: "{{ item.template | default(omit) }}"
27+
loop: "{{ tree | manala.path.join(path) }}"
28+
29+
- name: Tree with multiple paths
30+
manala.path.path:
31+
path: "{{ item.path }}"
32+
state: "{{ item.state | default(omit) }}"
33+
content: "{{ item.content | default(omit) }}"
34+
template: "{{ item.template | default(omit) }}"
35+
loop: "{{ tree | manala.path.join(path, 'service') }}"

plugins/filter/join.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from __future__ import absolute_import, division, print_function
2+
__metaclass__ = type
3+
4+
import os.path
5+
6+
from ansible.errors import AnsibleFilterError
7+
8+
9+
def join(elements, *paths):
10+
if not isinstance(elements, list):
11+
raise AnsibleFilterError('join first parameter expects a list of dictionary but was given a %s' % type(elements))
12+
13+
print(paths)
14+
15+
for element in elements:
16+
element['path'] = os.path.join(*(*paths, element['path']))
17+
18+
print(elements)
19+
20+
return elements
21+
22+
23+
class FilterModule(object):
24+
''' Manala join jinja2 filters '''
25+
26+
def filters(self):
27+
filters = {
28+
'join': join,
29+
}
30+
31+
return filters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import (absolute_import, division, print_function)
2+
__metaclass__ = type
3+
4+
import unittest
5+
from plugins.filter.join import join
6+
7+
from ansible.errors import AnsibleFilterError
8+
9+
10+
class TestJoin(unittest.TestCase):
11+
12+
def test_uncorrect_first_parameter(self):
13+
with self.assertRaises(AnsibleFilterError) as error:
14+
join("NotGood", "/tmp")
15+
self.assertEqual("join first parameter expects a list of dictionary but was given a <class 'str'>", str(error.exception))
16+
17+
def test_path(self):
18+
self.assertEqual([
19+
{"path": "/tmp/foo"},
20+
{"path": "/tmp/foo/bar"}
21+
], join([
22+
{"path": "foo"},
23+
{"path": "foo/bar"}
24+
], "/tmp"))
25+
26+
def test_paths(self):
27+
self.assertEqual([
28+
{"path": "/tmp/service/foo"},
29+
{"path": "/tmp/service/foo/bar"}
30+
], join([
31+
{"path": "foo"},
32+
{"path": "foo/bar"}
33+
], "/tmp", "service"))

0 commit comments

Comments
 (0)