Skip to content

Commit

Permalink
FEAT : join filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ethinot committed Sep 13, 2024
1 parent 25c5c34 commit 022c6fb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/join-filter-example.yaml
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') }}"
31 changes: 31 additions & 0 deletions plugins/filter/join.py
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
33 changes: 33 additions & 0 deletions tests/unit/plugins/filter/test_join_filter.py
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"))

0 comments on commit 022c6fb

Please sign in to comment.