Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
fca1167a
Commit
fca1167a
authored
Feb 28, 2013
by
Devin Bayer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add to_nice_yaml|json filters
parent
23bcb647
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
0 deletions
+94
-0
lib/ansible/runner/filter_plugins/core.py
+13
-0
test/TestFilters.py
+81
-0
No files found.
lib/ansible/runner/filter_plugins/core.py
View file @
fca1167a
...
...
@@ -18,14 +18,27 @@
import
json
import
yaml
def
to_nice_yaml
(
*
a
,
**
kw
):
'''Make verbose, human readable yaml'''
return
yaml
.
safe_dump
(
*
a
,
indent
=
4
,
allow_unicode
=
True
,
default_flow_style
=
False
,
**
kw
)
def
to_nice_json
(
*
a
,
**
kw
):
'''Make verbose, human readable JSON'''
return
json
.
dumps
(
*
a
,
indent
=
4
,
sort_keys
=
True
,
**
kw
)
class
FilterModule
(
object
):
''' Ansible core jinja2 filters '''
def
filters
(
self
):
return
{
# json
'to_json'
:
json
.
dumps
,
'to_nice_json'
:
to_nice_json
,
'from_json'
:
json
.
loads
,
# yaml
'to_yaml'
:
yaml
.
safe_dump
,
'to_nice_yaml'
:
to_nice_yaml
,
'from_yaml'
:
yaml
.
safe_load
,
}
test/TestFilters.py
0 → 100644
View file @
fca1167a
'''
Test bundled filters
'''
import
unittest
,
tempfile
,
shutil
from
ansible
import
playbook
,
inventory
,
callbacks
INVENTORY
=
inventory
.
Inventory
([
'localhost'
])
BOOK
=
'''
- hosts: localhost
vars:
var: { a: [1,2,3] }
tasks:
- template: src=
%
s dest=
%
s
'''
SRC
=
'''
-
{{ var|to_json }}
-
{{ var|to_nice_json }}
-
{{ var|to_yaml }}
-
{{ var|to_nice_yaml }}
'''
DEST
=
'''
-
{"a": [1, 2, 3]}
-
{
"a": [
1,
2,
3
]
}
-
a: [1, 2, 3]
-
a:
- 1
- 2
- 3
'''
class
TestFilters
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
tmpdir
=
tempfile
.
mkdtemp
(
dir
=
'/tmp'
)
def
tearDown
(
self
):
shutil
.
rmtree
(
self
.
tmpdir
)
def
temp
(
self
,
name
,
data
=
''
):
'''write a temporary file and return the name'''
name
=
self
.
tmpdir
+
'/'
+
name
with
open
(
name
,
'w'
)
as
f
:
f
.
write
(
data
)
return
name
def
test_filters
(
self
):
src
=
self
.
temp
(
'src.j2'
,
SRC
)
dest
=
self
.
temp
(
'dest.txt'
)
book
=
self
.
temp
(
'book'
,
BOOK
%
(
src
,
dest
))
playbook
.
PlayBook
(
playbook
=
book
,
inventory
=
INVENTORY
,
transport
=
'local'
,
callbacks
=
callbacks
.
PlaybookCallbacks
(),
runner_callbacks
=
callbacks
.
DefaultRunnerCallbacks
(),
stats
=
callbacks
.
AggregateStats
(),
)
.
run
()
out
=
open
(
dest
)
.
read
()
self
.
assertEqual
(
DEST
,
out
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment