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
e8381959
Commit
e8381959
authored
Mar 01, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2234 from akvadrako/nice-yaml-and-json
add to_nice_yaml|json filters
parents
38ab9b50
fca1167a
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 @
e8381959
...
@@ -18,14 +18,27 @@
...
@@ -18,14 +18,27 @@
import
json
import
json
import
yaml
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
):
class
FilterModule
(
object
):
''' Ansible core jinja2 filters '''
''' Ansible core jinja2 filters '''
def
filters
(
self
):
def
filters
(
self
):
return
{
return
{
# json
'to_json'
:
json
.
dumps
,
'to_json'
:
json
.
dumps
,
'to_nice_json'
:
to_nice_json
,
'from_json'
:
json
.
loads
,
'from_json'
:
json
.
loads
,
# yaml
'to_yaml'
:
yaml
.
safe_dump
,
'to_yaml'
:
yaml
.
safe_dump
,
'to_nice_yaml'
:
to_nice_yaml
,
'from_yaml'
:
yaml
.
safe_load
,
'from_yaml'
:
yaml
.
safe_load
,
}
}
test/TestFilters.py
0 → 100644
View file @
e8381959
'''
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