Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute multiple commands using Salt Stack?

Tags:

salt-stack

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.

like image 803
sorin Avatar asked Oct 28 '13 17:10

sorin


People also ask

How do you run multiple commands?

Running Multiple Commands as a Single Job We can start multiple commands as a single job through three steps: Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.

How do you list salt in minions?

"salt-key -L" will list all minions that whose public keys you've accepted on your master.

How does SaltStack work?

SaltStack, also known as Salt, is a configuration management and orchestration tool. It uses a central repository to provision new servers and other IT infrastructure, to make changes to existing ones, and to install software in IT environments, including physical and virtual servers, as well as the cloud.


2 Answers

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
like image 176
Dan Garthwaite Avatar answered Oct 19 '22 16:10

Dan Garthwaite


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.

like image 39
Steve Bennett Avatar answered Oct 19 '22 17:10

Steve Bennett