Packer shell-local post-processor permissions - shell

I am using Packer to create a custom Virtual Machine. I would like to use the post-processor after the build is finished. I am trying to keep it simple, using inline commands. However, some of them require sudo. This is the problem I am facing - I can't seem to make the execute_command work properly.
Currently what I have is:
"post-processors": [
{
"type":"shell-local",
"execute_command": "echo 'password' | sudo",
"inline": ["command 1", "command 2", "sudo command 3"]
}
]
This results in:
==> vmware-iso: Running post-processor: shell-local
==> vmware-iso (shell-local): Post processing with local shell script: /tmp/packer-shell867480064
vmware-iso (shell-local): usage: sudo -h | -K | -k | -V
vmware-iso (shell-local): usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
vmware-iso (shell-local): usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user]
vmware-iso (shell-local): [command]
vmware-iso (shell-local): usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
vmware-iso (shell-local): prompt] [-u user] [VAR=value] [-i|-s] [<command>]
vmware-iso (shell-local): usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
vmware-iso (shell-local): prompt] [-u user] file ...
Does anyone have an idea how I should implement the execute_command to make it work?

You shouldn't pipe your PASSWORD to sudo!
Instead if you need to run passwordless (NOPASSWD) sudo configure sudo to allow it. Also verify if you need !requiretty. See sudoers.conf.

Related

knife bootstrap --sudo not working

I'm trying to bootstrap an amazon aws instance with the command:
knife bootstrap 50.60.71.109 -N {node_name} -d {distro} -E {environment} -x ubuntu -i ~/.ssh/my-key.pem --sudo
but the output I get suggests that it's not using sudo correctly:
52.60.71.109 usage: sudo -h | -K | -k | -V
52.60.71.109 usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
52.60.71.109 usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user]
52.60.71.109 [command]
52.60.71.109 usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
52.60.71.109 prompt] [-u user] [VAR=value] [-i|-s] [<command>]
52.60.71.109 usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
52.60.71.109 prompt] [-u user] file ...
52.60.71.109 E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
...
The server is an ec2 ubuntu 14.04 instance.
My version of knife is 11.16.4

zsh run script without sudo

I made a bash script helpme that contains help configurations tasks that I use daily.
I would like to use it everywhere (even in other machines) with simple running:
$ helpme
I made this for that:(MAC OS)
$ sudo chmod +x helpme
$ sudo mv helpme /usr/local/bin/helpme
If I use bash terminal works correctly but now I'm using oh-my-zsh terminal and it shows me this message:
usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ...
If I run with sudo works, but I don't want to use sudo
$ sudo helpme
Edited:
This is an example:
#!/bin/bash
main(){
echo invalid option
}
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
main "$#"
fi
Any idea?

Why is the command not gettng the input with pipe in bash?

I am trying to do something simple in bash but I cannot get it to work. I have defined the variable:
export devcluster2="10.122.22.22"
in my .bashrc. When I type:
echo $devcluster2 I get as expected:
10.122.22.22
now I want to do:
echo '$devcluster2' | ssh
but I get
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user#]hostname [command]
I want to understand why the ssh doesn't get the output of echo as an input? and how can I get it to work?
To ssh to a host, you use:
ssh 10.122.22.22
If you want to ssh to a host in a variable, you therefore use:
ssh "$devcluster2"
Meanwhile, echo text | command is like running command and then typing in "text". If you to run ssh by itself and wait for an opportunity to enter text, it fails just like it does in your script:
$ ssh
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
...

sudo -u postgres not working in mac os x

I have recently installed postgresql 9.1 after uninstalling postgres 9.3 as per the requirement of my project but I am not able to run basic sudo command with postgres user.
sudo -u postgres -h localhost createdb template_postgis
This gives me no error but shows the usage of sudo:
usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u
user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] file ...
Though sudo works for other projects and commands but not in this. It was working earlier perfectly. I will be really thankful if someone helps.
In the manual page for sudo (which you can view by running man sudo in the terminal), you can see that this is exactly what the -h option does:
-h The -h (help) option causes sudo to print a usage message and exit.
You should be able to run the command without that option:
sudo -u postgres createdb template_postgis

OSX Terminal.app SSH port

I feel like a complete dunce - but I cannot seem to connect with Terminal.app and SSH to a non-standard SSH port. I'm on Mountain Lion.
I've tried all of the following:
$ ssh myuser#mysite.com -p 42586
$ ssh myuser#mysite.com -p42586
$ ssh -p 42586 myuser#mysite.com
$ ssh -p42586 myuser#mysite.com
I cannot seem to get the syntax right, all of the above produce an error. What is the correct order of flags? I have checked the man page and it shows it as preceding the [user]#[domain] section, however, this throws an error as well.
Edit:
More information and raw terminal output:
$ ssh mysite.com -p42586
--hangs--
$ ssh mysite.com -p 42586
--hangs--
$ ssh -p42586 mysite.com
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user#]hostname [command]
$ ssh -p 42586 mysite.com
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
/usr/bin/ssh: option requires an argument -- p
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user#]hostname [command]
Examples 1 and 2 hang until timeout. 3 and 4 produce a grep usage echo as well as an ssh usage echo.
Edit 2:
Thanks for the suggestion #topguncoder my original attempt was the colon notation - but that produces a different error:
$ ssh myuser#mysite.com:42586
ssh: Could not resolve hostname mysite:42586: nodename nor servname provided, or not known
Try
"ssh" -p 42586 mysite.com
to ignore your alias settings. It looks like you might have ssh aliased to something else. If this works, you should check your .profile for any ssh aliases.
I believe that on your system, the ssh command somehow was redefined as the grep command. Try this:
alias | /usr/bin/grep ssh
What do you see? Also, try this:
/usr/bin/ssh -p 42586 myuser#mysite.com
Do you have a success connection? If ssh was aliased into something else. Check the following files:
~/.bash_profile
~/.bashrc
~/.profile
You can then remove the alias, or use the absolute path (e.g /usr/bin/ssh)

Resources