Search code examples
salt-projectorchestration

How to run local commands on the salt orchestrator from the orchestrate runner


I'm trying to execute the redis-trib.rb utility (used to configure redis clusters) on the salt orchestrator, after executing salt states on multiple minions to bring up redis processes.

Reading the salt documentation it looks like the the orchestrate runner does what I want to execute the minion states.

Indeed this snippet functions perfectly when executed with sudo salt-run state.orchestrate orch.redis_cluster:

redis_cluster_instances_create:
  salt.state:
    - tgt: '*redis*'
    - highstate: True

The problem is with the next step, which requires me to call redis-trib.rb on the orchestrator. Reading through the documentation it looks like I need to use the salt.runner state (executes another runner), to call the salt.cmd runner (executes a salt state locally), which in turn calls the cmd.run state to actually execute the command.

What I have looks like this:

redis_cluster_setup_masters_{{ cluster }}:
  salt.runner:
    - name: salt.cmd
    - fun: cmd.run
    - args:
      - srv/salt/orch/redis_cluster/usr/bin/redis-trib.rb create {% for shard in shards %}{{ shard['master'] }} {% endfor %}
    - kwargs:
      unless: srv/salt/orch/redis_cluster/usr/bin/redis-trib.rb info {{ shards[0]['master'] }} | grep 'cluster_state:ok'
    - require:
      - salt: redis_cluster_instances_create

But it doesn't work, and salt errors out with:

lab-orchestrator_master:
----------
          ID: redis_cluster_instances_create
    Function: salt.state
      Result: True
     Comment: States ran successfully. No changes made to lab-redis04, lab-redis01, lab-redis02, lab-redis03.
     Started: 09:54:57.811313
    Duration: 14223.204 ms
     Changes:   
----------
          ID: redis_cluster_setup_masters_pdnocg
    Function: salt.runner
        Name: salt.cmd
      Result: False
     Comment: Exception occurred in runner salt.cmd: Traceback (most recent call last):
                File "/usr/lib/python2.7/site-packages/salt/client/mixins.py", line 392, in _low
                  data['return'] = self.functions[fun](*args, **kwargs)
              TypeError: cmd() takes at least 1 argument (0 given)
     Started: 09:55:12.034716
    Duration: 1668.345 ms
     Changes:   

Can anyone suggest what i'm doing wrong? Or an alternative way of executing commands locally on the orchestrator?


Solution

  • The problem is, that you are passing fun et. al. to the runner instead of the execution module. Also note that you have to pass the arguments via arg and not args:

    redis_cluster_setup_masters_{{ cluster }}:
      salt.runner:
        - name: salt.cmd
        - arg:
          - fun=cmd.run
          - cmd='srv/salt/orch/redis_cluster/usr/bin/redis-trib.rb create {% for shard in shards %}{{ shard['master'] }} {% endfor %}'
        - unless: srv/salt/orch/redis_cluster/usr/bin/redis-trib.rb info {{ shards[0]['master'] }} | grep 'cluster_state:ok'
        - require:
          - salt: redis_cluster_instances_create
    

    That should do the trick, though I haven't tested it with the kwargs parameter.