Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape single quotes in commands? #89

Closed
httpete opened this issue Feb 9, 2022 · 5 comments
Closed

Escape single quotes in commands? #89

httpete opened this issue Feb 9, 2022 · 5 comments

Comments

@httpete
Copy link

httpete commented Feb 9, 2022

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" ]
}
@skx
Copy link
Owner

skx commented Feb 10, 2022

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

@skx skx closed this as completed in 2bb0b08 Feb 10, 2022
@httpete
Copy link
Author

httpete commented Feb 10, 2022

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

@skx
Copy link
Owner

skx commented Feb 10, 2022

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:

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.

@httpete
Copy link
Author

httpete commented Feb 10, 2022

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", 

@skx
Copy link
Owner

skx commented Feb 10, 2022

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",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants