1""" 2Display return data in YAML format 3================================== 4 5This outputter defaults to printing in YAML block mode for better readability. 6 7CLI Example: 8 9.. code-block:: bash 10 11 salt '*' foo.bar --out=yaml 12 13Example output: 14 15CLI Example: 16 17.. code-block:: python 18 19 saltmine: 20 foo: 21 bar: baz 22 dictionary: 23 abc: 123 24 def: 456 25 list: 26 - Hello 27 - World 28""" 29 30import logging 31 32import salt.utils.yaml 33 34# Define the module's virtual name 35__virtualname__ = "yaml" 36 37log = logging.getLogger(__name__) 38 39 40def __virtual__(): 41 return __virtualname__ 42 43 44def output(data, **kwargs): # pylint: disable=unused-argument 45 """ 46 Print out YAML using the block mode 47 """ 48 49 params = {} 50 if "output_indent" not in __opts__: 51 # default indentation 52 params.update(default_flow_style=False) 53 elif __opts__["output_indent"] >= 0: 54 # custom indent 55 params.update(default_flow_style=False, indent=__opts__["output_indent"]) 56 else: # no indentation 57 params.update(default_flow_style=True, indent=0) 58 try: 59 return salt.utils.yaml.safe_dump(data, **params) 60 except Exception as exc: # pylint: disable=broad-except 61 import pprint 62 63 log.exception( 64 "Exception %s encountered when trying to serialize %s", 65 exc, 66 pprint.pformat(data), 67 ) 68