Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT : join filter #2

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/tree.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: Tree
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') }}"
27 changes: 27 additions & 0 deletions plugins/filter/join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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))

for element in elements:
element['path'] = os.path.join(*(*paths, element['path']))

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.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"))