module.run documentation example causes YAML parsing error

880 views
Skip to first unread message

Chris Ruscio

unread,
Sep 15, 2017, 11:50:51 PM9/15/17
to Salt-users

Salt Master 17.7.1 (Ubuntu 16.04)
Salt Minion 17.7.1 (Windows 2012r2, python 3.5.3, amd64)

There is an example of module.run on https://docs.saltstack.com/en/latest/ref/states/all/salt.states.module.html

eventsviewer:
  module.run:
    task.create_task:
      - name: events-viewer
      - user_name: System
      - action_type: Execute
      - cmd: 'c:\netops\scripts\events_viewer.bat'
      - trigger_type: 'Daily'
      - start_date: '2017-1-20'
      - start_time: '11:59PM'

When applying a state with that example, the following error occurs:

Data failed to compile:
----------
State 'eventsviewer' in SLS 'telegraf' is not formed as a list


---

1. What is the correct syntax for running a module function in a state file?
2. Should I open an issue on github to correct the documentation in module.py?

Thomas Phipps

unread,
Sep 16, 2017, 12:12:07 AM9/16/17
to Salt-users
I've been looking into this. there are a couple of things going on here. an error in the documentation. AND a setting that is shown at the bottom of the documentation that is needed to use the new module.run which is not enabled by default. 

both topics are brought up in this issue report


you need to add this to your minion config in order to use the new module.run. and you also need to use a hyphen in front of the module being run. in this case. - task.create_task
use_superseded:
  - module.run

On Fri, Sep 15, 2017 at 8:50 PM Chris Ruscio <cru...@gmail.com> wrote:

Chris Ruscio

unread,
Sep 16, 2017, 11:33:39 AM9/16/17
to Salt-users
I just did both things:
  • Updated the minion config (and restarted)
  • Added a hyphen in the state file.
It created the task, but it also threw a python error.

[DEBUG   ] Rendered data from file: c:\salt\var\cache\salt\minion\files\base\telegraf\init.sls:
eventsviewer
:
 
module.run:
   
- task.create_task:

     
- name: events-viewer
     
- user_name: System
     
- action_type: Execute
     
- cmd: 'c:\netops\scripts\events_viewer.bat'
     
- trigger_type: 'Daily'
     
- start_date: '2017-1-20'
     
- start_time: '11:59PM'
[DEBUG   ] LazyLoaded config.get
[DEBUG   ] Results of YAML rendering:
OrderedDict([('eventsviewer', OrderedDict([('module.run', [OrderedDict([('task.create_task', [OrderedDict([('name', 'events-viewer')]), OrderedDict([(
'user_name', 'System')]), OrderedDict([('action_type', 'Execute')]), OrderedDict([('cmd', 'c:\\netops\\scripts\\events_viewer.bat')]), OrderedDict([('
trigger_type'
, 'Daily')]), OrderedDict([('start_date', '2017-1-20')]), OrderedDict([('start_time', '11:59PM')])])])])]))])
[PROFILE ] Time (in seconds) to render 'c:\salt\var\cache\salt\minion\files\base\telegraf\init.sls' using 'yaml' renderer: 0.015630245208740234
[DEBUG   ] LazyLoaded module.run
[INFO    ] Running state [eventsviewer] at time 11:27:19.406231
[INFO    ] Executing state module.run for [eventsviewer]
[DEBUG   ] LazyLoaded task.create_task
[INFO    ] {'task.create_task': 'events-viewer already exists'}
[INFO    ] Completed state [eventsviewer] at time 11:27:19.437482 duration_in_ms=31.251
[DEBUG   ] File c:\salt\var\cache\salt\minion\accumulator\421414487712 does not exist, no need to cleanup.
[DEBUG   ] LazyLoaded config.option
[DEBUG   ] LazyLoaded cmd.run
[INFO    ] Executing command ['attrib', '-R', 'c:\\salt\\var\\cache\\salt\\minion\\sls.p'] in directory 'C:\Users\cruscio'
[DEBUG   ] output:
[WARNING ] Passed invalid arguments to state.apply: can't serialize dict_keys(['task.create_task'])

    .. versionadded:: 2015.5.0

    This function will call :mod:`state.highstate
    <salt.modules.state.highstate>` or :mod:`state.sls
    <salt.modules.state.sls>` based on the arguments passed to this function.
    It exists as a more intuitive way of applying states.

    .. rubric:: APPLYING ALL STATES CONFIGURED IN TOP.SLS (A.K.A. :ref:`HIGHSTATE <running-highstate>`)

    To apply all configured states, simply run ``state.apply``:

    .. code-block:: bash

        salt '
*' state.apply

    The following additional arguments are also accepted when applying all
    states configured in top.sls:

    test
        Run states in test-only (dry-run) mode

    pillar
        Custom Pillar values, passed as a dictionary of key-value pairs

        .. code-block:: bash

            salt '
*' state.apply test pillar='{"foo": "bar"}'

        .. note::
            Values passed this way will override Pillar values set via
            ``pillar_roots`` or an external Pillar source.

    queue : False
        Instead of failing immediately when another state run is in progress,
        queue the new state run to begin running once the other has finished.

        This option starts a new thread for each queued state run, so use this
        option sparingly.

    localconfig
        Optionally, instead of using the minion config, load minion opts from
        the file specified by this argument, and then merge them with the
        options from the minion config. This functionality allows for specific
        states to be run with their own custom minion configuration, including
        different pillars, file_roots, etc.

        .. code-block:: bash

            salt '
*' state.apply localconfig=/path/to/minion.yml


    .. rubric:: APPLYING INDIVIDUAL SLS FILES (A.K.A. :py:func:`STATE.SLS <salt.modules.state.sls>`)

    To apply individual SLS files, pass them as a comma-separated list:

    .. code-block:: bash

        # Run the states configured in salt://test.sls (or salt://test/init.sls)
        salt '
*' state.apply test
        # Run the states configured in salt://test.sls (or salt://test/init.sls)
        # and salt://pkgs.sls (or salt://pkgs/init.sls).
        salt '
*' state.apply test,pkgs

    The following additional arguments are also accepted when applying
    individual SLS files:

    test
        Run states in test-only (dry-run) mode

    pillar
        Custom Pillar values, passed as a dictionary of key-value pairs

        .. code-block:: bash

            salt '
*' state.apply test pillar='{"foo": "bar"}'

        .. note::
            Values passed this way will override Pillar values set via
            ``pillar_roots`` or an external Pillar source.

    queue : False
        Instead of failing immediately when another state run is in progress,
        queue the new state run to begin running once the other has finished.

        This option starts a new thread for each queued state run, so use this
        option sparingly.

    concurrent : False
        Execute state runs concurrently instead of serially

        .. warning::

            This flag is potentially dangerous. It is designed for use when
            multiple state runs can safely be run at the same time. Do *not*
            use this flag for performance optimization.

    saltenv
        Specify a salt fileserver environment to be used when applying states

        .. versionchanged:: 0.17.0
            Argument name changed from ``env`` to ``saltenv``

        .. versionchanged:: 2014.7.0
            If no saltenv is specified, the minion config will be checked for an
            ``environment`` parameter and if found, it will be used. If none is
            found, ``base`` will be used. In prior releases, the minion config
            was not checked and ``base`` would always be assumed when the
            saltenv was not explicitly set.

    pillarenv
        Specify a Pillar environment to be used when applying states. This
        can also be set in the minion config file using the
        :conf_minion:`pillarenv` option. When neither the
        :conf_minion:`pillarenv` minion config option nor this CLI argument is
        used, all Pillar environments will be merged together.

    localconfig
        Optionally, instead of using the minion config, load minion opts from
        the file specified by this argument, and then merge them with the
        options from the minion config. This functionality allows for specific
        states to be run with their own custom minion configuration, including
        different pillars, file_roots, etc.

        .. code-block:: bash

            salt '
*' state.apply test localconfig=/path/to/minion.yml

Traceback (most recent call last):
  File "c:\salt\bin\lib\site-packages\salt\minion.py", line 1468, in _thread_return
    return_data = executor.execute()
  File "c:\salt\bin\lib\site-packages\salt\executors\direct_call.py", line 28, in execute
    return self.func(*self.args, **self.kwargs)
  File "c:\salt\bin\lib\site-packages\salt\modules\state.py", line 592, in apply_
    return sls(mods, **kwargs)
  File "c:\salt\bin\lib\site-packages\salt\modules\state.py", line 1121, in sls
    serial.dump(ret, fp_)
  File "c:\salt\bin\lib\site-packages\salt\payload.py", line 304, in dump
    fn_.write(self.dumps(msg, use_bin_type=True))
  File "c:\salt\bin\lib\site-packages\salt\payload.py", line 181, in dumps
    return msgpack.dumps(msg, use_bin_type=use_bin_type)
  File "c:\salt\bin\lib\site-packages\msgpack\__init__.py", line 47, in packb
    return Packer(**kwargs).pack(o)
  File "msgpack\_packer.pyx", line 231, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:3661)
  File "msgpack\_packer.pyx", line 233, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:3503)
  File "msgpack\_packer.pyx", line 192, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:2657)
  File "msgpack\_packer.pyx", line 192, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:2657)
  File "msgpack\_packer.pyx", line 228, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:3382)
TypeError: can'
t serialize dict_keys(['task.create_task'])
[DEBUG   ] Minion return retry timer set to 7 seconds (randomized)
[INFO    ] Returning information for job: 20170916152711886991

Thomas Phipps

unread,
Sep 17, 2017, 1:27:41 AM9/17/17
to Salt-users
humm, it should not have thrown the error. everything else does sound good though. 

humm the error looks doesn't make a lot of sense it looks like it is saying you passed ['task.create_task'] as an argument to state.apply. which should not be the case.

Chris Ruscio

unread,
Sep 18, 2017, 8:54:07 AM9/18/17
to Salt-users
Think I should open an issue on Github? This seems sufficiently different from the one you linked.

Thomas Phipps

unread,
Sep 18, 2017, 12:31:33 PM9/18/17
to Salt-users
I would say yes. not sure where it is coming from even. when i tried with a different command i didn't see any kind of error. 
Reply all
Reply to author
Forward
0 new messages