Shell script in .sls file

295 views
Skip to first unread message

Nicholas Geovanis

unread,
Oct 11, 2017, 12:10:32 PM10/11/17
to Salt-users
On numerous occasions I've seen salt .sls files which "are" or "begin with" shell scripts. For example:

echo '12.14.16.18,xxx.yyy.zzz.int' > /tmp/anistage.in:
  cmd.run:
    - creates:
      - /tmp/anistage.in

...which writes the expected contents into /tmp/anistage.in on a minion.

But despite many hours in the salt doc, I have never seen this salt feature discussed there. Specifically no
mention of precisely when and where this construct can be used in salt.
Can someone point me to such doc?
Thanks.....Nick

Nicholas Capo

unread,
Oct 11, 2017, 1:15:13 PM10/11/17
to Salt-users
What you are seeing is just short hand.

In general the syntax is as follows:

```
State ID:
  module.function:
    - name: name
    - argument1: value1
```

But you can also do:

```
name:
  module.function:
    - argument1: value1
```

And the ID will be reused as the name.

So:

```
echo '12.14.16.18,xxx.yyy.zzz.int' > /tmp/anistage.in:
  cmd.run:
    - creates:
      - /tmp/anistage.in
```

Is the just saying run a command (cmd.run), and grab the command to run from the state ID. So you could also do:

```
set anistage:
  cmd.run:
    - name: echo '12.14.16.18,xxx.yyy.zzz.int' > /tmp/anistage.in
    - creates:
      - /tmp/anistage.in
```

I would actually recommend this pattern since you have given the state a useful ID that can be more easily used in requires, even if you change the exact details of the command.

For example something like this:

```
foo:
  cmd.run:
    - require:
      - cmd: set anistage
```

is better than this:

```
foo:
  cmd.run:
    - require:
      - cmd: echo '12.14.16.18,xxx.yyy.zzz.int' > /tmp/anistage.in
```

(Though as it happens you can actually refer to a state by its ID or name, having the ID different just seems cleaner to me.)

Hope that helps,

Nicholas

P.S. For this sort of state I would actually recommend using file.managed instead of cmd.run:

```
  file.manged:
    - contents: '12.14.16.18,xxx.yyy.zzz.int'
```

But I also see where this might be just an example. :-)

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/b3c8234e-4714-434f-824f-a230f5602826%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages