How can I execute multiple commands using Salt Stack?

跟風遠走 提交于 2019-12-04 07:48:07

问题


I tried to add:

 mypack:
   pkg:
     - installed
     - pkgs:
       - mercurial
       - git
   cmd.run:
       - name: 'mkdir -p /opt/mypack'
   cmd.run: 'hg pull -u -R /opt/mypack || hg clone -R /opt https://...'
   cmd.run: 'ln -s /opt/mypack/etc/init.d/xxx /etc/init.d/xxx'

But for some reason this the state seems to execute/install but the commands are not executed, or at least not all of them.

I need a solution to run multiple commands and to fail the deployment if any of these fails.

I know that I could write a bash script and include this bash script, but I was looking for a solution that would work with only the YAML file.


回答1:


You want this:

cmd-test:
  cmd.run:
    - name: |
        mkdir /tmp/foo
        chown dan /tmp/foo
        chgrp www-data /tmp/foo
        chmod 2751 /tmp/foo
        touch /tmp/foo/bar

Or this, which I would prefer, where the script is downloaded from the master:

cmd-test:
  cmd.script:
    - source: salt://foo/bar.sh
    - cwd: /where/to/run
    - user: fred



回答2:


In addition to the above (better) suggestions, you can do this:

cmd-test:
  cmd.run:
    - names: 
      - mkdir -p /opt/mypack
      - hg pull -u -R /opt/mypack || hg clone -R /opt https://...
      - ln -s /opt/mypack/etc/init.d/xxx /etc/init.d/xxx

For reasons I don't understand yet (I'm a Salt novice), the names are iterated in reverse order, so the commands are executed backwards.




回答3:


You can do as Dan pointed out, using the pipe or a cmd.script state. But it should be noted that you have some syntax problems in your original post. Each new state needs a name arg, you can't just put the command after the colon:

mypack:
  pkg:
    - installed
    - pkgs:
      - mercurial
      - git
  cmd.run:
    - name: 'my first command'
  cmd.run:
    - name: 'my second command'

However, that actually may fail as well, because I don't think you can put multiple of the same state underneath a single ID. So you may have to split them out like this:

first:
  cmd.run:
    - name: 'my first command'

second:
  cmd.run:
    - name: 'my second command'


来源:https://stackoverflow.com/questions/19640829/how-can-i-execute-multiple-commands-using-salt-stack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!