0

I try from my terminal, to execute on remote server a openstack command to a docker. The purpose is to get the id of openstack project.

when condition is true, I want to get id, but the script below failed to get id. I don't know if I can execute if condition to EOF statement

ret="$(ssh -qT root@server << EOF
  docker exec openstack bash -c ". adminrc &&
  if [ false ]; then  openstack project create p_ops &>/dev/null
  else :
  fi
  id_u=$(openstack user show u_ops | grep " id" | cut -d "|" -f3 | xargs)
  openstack role add SwiftOperator --project p_ops --user $id_u
  id_p=$(openstack project show p_ops | grep " id" | cut -d "|" -f3|xargs)
  echo "$id_p""
EOF
)"

I get the output :

Missing value auth-url required for auth plugin password
Missing value auth-url required for auth plugin password
usage: openstack role add [-h]
                          [--system <system> | --domain <domain> | --project <project>]
                          [--user <user> | --group <group>]
                          [--group-domain <group-domain>]
                          [--project-domain <project-domain>]
                          [--user-domain <user-domain>] [--inherited]
                          [--role-domain <role-domain>]
                          <role>
openstack role add: error: argument --user: expected one argument

I desired the id of project :

echo $id_p
faafe2044c4235ac648faaceae5d1a3bf2a8f7a8ca8a765f5a9621a5e53d9162
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
poiubb22
  • 13
  • 3
  • `$()` is executed on your local machine before the SSH connection is established. – jordanm Mar 11 '22 at 15:42
  • 1
    Same with the environment variables `$id_u` and `$id_p`. I'd suggest breaking this into two or three different pieces – make the script be a script, `scp` it to the remote machine, then run it – rather than trying to escape and fit everything into a single command. This seems like a little more of a shell-scripting or system-administration question than programming-related, though. – David Maze Mar 11 '22 at 17:43
  • Your quotes are messed up, you should backslash `"` inside other `"`. You can check your syntax at https://www.shellcheck.net/ – Nic3500 Mar 12 '22 at 03:34

1 Answers1

0

You can avoid the use of nested quotes which are giving you problems.

Instead of the complex sentence:

openstack user show u_ops | grep " id" | cut -d "|" -f3 | xargs

you can just write:

openstack user show -f value -c id
ret=$(ssh -qT root@server << EOF
  docker exec openstack bash -c ". adminrc &&
  if [ false ]; then openstack project create p_ops &>/dev/null
  else :
  fi
  id_u=$(openstack user show -f value -c id u_ops)
  openstack role add SwiftOperator --project p_ops --user $id_u
  id_p=$(openstack project show -f value -c id p_ops)
  echo $id_p"
EOF
)
Rockcat
  • 3,002
  • 2
  • 14
  • 28