How can I execute multiple commands using Salt Stack?

sorin picture sorin · Oct 28, 2013 · Viewed 35.1k times · Source

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.

Answer

Dan Garthwaite picture Dan Garthwaite · Oct 29, 2013

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