2015-06-01 21:41:52 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import yaml
|
2015-10-16 00:55:23 +00:00
|
|
|
from ansible.compat.six import PY3
|
2015-06-01 21:41:52 +00:00
|
|
|
|
2015-12-07 06:12:48 +00:00
|
|
|
from ansible.parsing.yaml.objects import AnsibleUnicode, AnsibleSequence, AnsibleMapping
|
2016-08-24 00:03:11 +00:00
|
|
|
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
|
2015-10-18 14:07:20 +00:00
|
|
|
from ansible.vars.hostvars import HostVars
|
2017-01-19 01:35:24 +00:00
|
|
|
from ansible.vars.unsafe_proxy import AnsibleUnsafeText
|
2015-06-01 21:41:52 +00:00
|
|
|
|
2016-08-24 00:03:11 +00:00
|
|
|
|
2015-06-01 21:41:52 +00:00
|
|
|
class AnsibleDumper(yaml.SafeDumper):
|
|
|
|
'''
|
|
|
|
A simple stub class that allows us to add representers
|
|
|
|
for our overridden object types.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2015-10-18 14:07:20 +00:00
|
|
|
def represent_hostvars(self, data):
|
|
|
|
return self.represent_dict(dict(data))
|
|
|
|
|
2016-08-24 00:03:11 +00:00
|
|
|
# Note: only want to represent the encrypted data
|
|
|
|
def represent_vault_encrypted_unicode(self, data):
|
2017-02-03 20:28:50 +00:00
|
|
|
return self.represent_scalar(u'!vault', data._ciphertext.decode(), style='|')
|
2016-08-24 00:03:11 +00:00
|
|
|
|
2015-09-10 05:57:53 +00:00
|
|
|
if PY3:
|
|
|
|
represent_unicode = yaml.representer.SafeRepresenter.represent_str
|
|
|
|
else:
|
|
|
|
represent_unicode = yaml.representer.SafeRepresenter.represent_unicode
|
|
|
|
|
2015-06-01 21:41:52 +00:00
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
AnsibleUnicode,
|
2015-09-10 05:57:53 +00:00
|
|
|
represent_unicode,
|
2015-06-01 21:41:52 +00:00
|
|
|
)
|
|
|
|
|
2017-01-19 01:35:24 +00:00
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
AnsibleUnsafeText,
|
|
|
|
represent_unicode,
|
|
|
|
)
|
|
|
|
|
2015-10-18 14:07:20 +00:00
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
HostVars,
|
|
|
|
represent_hostvars,
|
|
|
|
)
|
|
|
|
|
2015-12-07 06:12:48 +00:00
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
AnsibleSequence,
|
|
|
|
yaml.representer.SafeRepresenter.represent_list,
|
|
|
|
)
|
|
|
|
|
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
AnsibleMapping,
|
|
|
|
yaml.representer.SafeRepresenter.represent_dict,
|
|
|
|
)
|
|
|
|
|
2016-08-24 00:03:11 +00:00
|
|
|
AnsibleDumper.add_representer(
|
|
|
|
AnsibleVaultEncryptedUnicode,
|
|
|
|
represent_vault_encrypted_unicode,
|
|
|
|
)
|