-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
|
||
# This playbook example contains simple use cases that illustrate the basics functionality of join filter. | ||
|
||
- name: Examples | ||
gather_facts: false | ||
hosts: localhost | ||
vars: | ||
path: /tmp | ||
tree: | ||
- path: foo | ||
- path: foo/bar | ||
content: | | ||
Bar | ||
- path: foo/baz | ||
template: template.j2 | ||
- path: qux | ||
state: absent | ||
tasks: | ||
|
||
- name: Tree with simple path | ||
manala.path.path: | ||
path: "{{ item.path }}" | ||
state: "{{ item.state | default(omit) }}" | ||
content: "{{ item.content | default(omit) }}" | ||
template: "{{ item.template | default(omit) }}" | ||
loop: "{{ tree | manala.path.join(path) }}" | ||
|
||
- name: Tree with multiple paths | ||
manala.path.path: | ||
path: "{{ item.path }}" | ||
state: "{{ item.state | default(omit) }}" | ||
content: "{{ item.content | default(omit) }}" | ||
template: "{{ item.template | default(omit) }}" | ||
loop: "{{ tree | manala.path.join(path, 'service') }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
import os.path | ||
|
||
from ansible.errors import AnsibleFilterError | ||
|
||
|
||
def join(elements, *paths): | ||
if not isinstance(elements, list): | ||
raise AnsibleFilterError('join first parameter expects a list of dictionary but was given a %s' % type(elements)) | ||
|
||
print(paths) | ||
|
||
for element in elements: | ||
element['path'] = os.path.join(*(*paths, element['path'])) | ||
|
||
print(elements) | ||
|
||
return elements | ||
|
||
|
||
class FilterModule(object): | ||
''' Manala join jinja2 filters ''' | ||
|
||
def filters(self): | ||
filters = { | ||
'join': join, | ||
} | ||
|
||
return filters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
import unittest | ||
from plugins.filter.join import join | ||
|
||
from ansible.errors import AnsibleFilterError | ||
|
||
|
||
class TestJoin(unittest.TestCase): | ||
|
||
def test_uncorrect_first_parameter(self): | ||
with self.assertRaises(AnsibleFilterError) as error: | ||
join("NotGood", "/tmp") | ||
self.assertEqual("join first parameter expects a list of dictionary but was given a <class 'str'>", str(error.exception)) | ||
|
||
def test_path(self): | ||
self.assertEqual([ | ||
{"path": "/tmp/foo"}, | ||
{"path": "/tmp/foo/bar"} | ||
], join([ | ||
{"path": "foo"}, | ||
{"path": "foo/bar"} | ||
], "/tmp")) | ||
|
||
def test_paths(self): | ||
self.assertEqual([ | ||
{"path": "/tmp/service/foo"}, | ||
{"path": "/tmp/service/foo/bar"} | ||
], join([ | ||
{"path": "foo"}, | ||
{"path": "foo/bar"} | ||
], "/tmp", "service")) |