3

I've got a salt script that is being executed with state.sls like this:

salt '*' state.sls foo.bar

In my script I've got this:

foo-bar:
  cmd.run:
    - php foo.php bar --delete
    - cwd: /srv/foo

The --delete flag will cause the script to truncate a database first. My problem is that it asks a question and expects an input.

root@host:/srv/foo# php foo.php bar --delete
This is going to remove all data in the database. Are you sure? [y/n]:

Because salt doesn't know how to answer that it will time out and abort and do things that I did not intend.

The salt docs on cmd.run did not say anything about doing this and I am not sure what to google for exactly. I know that in a Perl implementation I would use Expect to do this.

How do I tell salt to please answer with y?

2
  • Of course I could just add another command line option to foo.php to override the check, but let's assume there is no such option.
    – simbabque
    Feb 5, 2015 at 17:23
  • well, why not use expect here in your cmd.run state?
    – figtrap
    Sep 28, 2016 at 21:16

1 Answer 1

4

In recent versions of salt you can provide 'stdin' to cmd.run:

A string of standard input can be specified for the command to be run using
the ``stdin`` parameter. This can be useful in cases where sensitive
information must be read from standard input.:

    salt '*' cmd.run "grep f" stdin='one\ntwo\nthree\nfour\nfive\n'

You can create a script to do it and run it via cmd.script or even create a custom python module.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .