skx / marionette

Something like puppet, for the localhost only.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Escape single quotes in commands?

httpete opened this issue · comments

No matter how I try to escape, I can't do:

shell { name    => "Adding datadir, adding new",
        command => ["sed '17 i This is Line 17' ${HOME}/.my.cnf" ]
}

This is a bug, I'll handle an update shortly.

I did install from master, saw the new version come in, and it doesn't fly:

shell { name    => "Adding datadir, adding new",
        command => ["sed -i \'s/user/#user/g\' ${HOME}/.my.cnf"], 
}

sed: -e expression #1, char 18: unterminated address regex
Error:error running shell-module rule 'Adding datadir, adding new' error running command 'sed -i \'s/user/#user/g\' /home/ps893826/.my.cnf' exit status 1

Looking at this more carefully I think there's some confusion.

This is my sample input:

shell {
      command => "echo \"user is bob\" > test.conf",
}
shell {
      command => ["sed -i s/user/#user/g test.conf"],
}
  • The first block writes to test.conf
    • user is bob
  • The second block runs sed
    • Replacing user with #user

That works 100%. No quoting involved.

Adding quotes around the sed-input causes failure:

  • command => "sed -i 's/user/#user/g' test.conf",
  • command => "sed -i \"s/user/#user/g\" test.conf",

In both cases the error is from sed, which looks like this:

  • sed: -e expression #1, char 1: unknown command: `''
  • sed: -e expression #1, char 1: unknown command: `"'

i.e. The issue here is that the command is being taken with the quotes literally, because we're not runnign with a shell.

So I think the immediate problem you have is solved by dropping the quotes.

The second problem is caused by not running with bash. I think that can be solved by adding a needless redirect:

shell {
      command => ["sed -i \"s/user/#user/g\" test.conf >/dev/null"],
}

That's because the way that I run shell commands depends on whether there is a redirection, or similar "complex" thing present.

I hope that one/other of those solves things for you. But I guess I'll leave this open and see if I can add:

 shell => "true"

To force the use of the shell, rather than the hack with the redirection character.

OK I see it work as you say, and this is nice I don't want to deal with escaping.
Thank you, I am enjoying marionette, I see it filling a lane that is quite missing out there. I am using it to provision a development environment. Some additions that would be good

  1. a --version flag
  2. a mysql module, where I can execute sql. Right now I am shelling out, but that defeats the purpose
  3. A find and replacer, like I am doing with sed, the file module seems close but I need more fine grained control.
        command => "sed -i 's/user/#user/g' ${HOME}/.my.cnf > /dev/null", 

Good comments. I'll create issues for the missing features you suggest.

For editing I've done a hack which is to use the edit-module twice;

  • Once to remove lines matching a pattern.
  • Once to add the intended thing.

Doesn't work generally, such as changing the port of sshd, but it can work in emergencies.

I can see this working, and being easy to add:

edit {
target => "path/to/file",
  search => "old",
  replace => "new",
}