Linux Shell Scritping - Variable inside variable with commands

8/21/2016

I'm trying create a script that automate the setup and install the processess of a Kubernetes master node. I can't figure out whats wrong with my code below, tha goal is too simple setup the worker nodes information.

So let me share my snippet code.

function create_worker()
{

  echo "Generate the Kubernetes Worker Keypairs ...";

  WORKER_NUMBER=3
  WORKERS_FQDN=("worker1" "worker2" "worker3")
  WORKERS_IP=("192.168.0.10" "192.168.0.11" "192.168.0.12")
  MASTER_IP=("192.168.0.20")
  K8S_SERVICE_IP=10.3.0.1

  for ((i=0; i <= $WORKER_NUMBER; i++));
  do
     openssl genrsa -out ${WORKERS_FQDN[i]}-worker-key.pem 2048
     WORKER_IP="${WORKERS_IP[i]}" openssl req -new -key "${WORKERS_FQDN[i]}"-worker-key.pem -out "${WORKERS_FQDN[i]}"-worker.csr -subj "/CN="${WORKERS_FQDN[i]}"" -config worker-openssl.cnf
     WORKER_IP="${WORKERS_IP[i]}" openssl x509 -req -in "${WORKERS_FQDN[i]}"-worker.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out "${WORKERS_FQDN[i]}"-worker.pem -days 365 -extensions v3_req -extfile worker-openssl.cnf
    echo ${WORKERS_IP[i]}  ${WORKERS_FQDN[i]}
    sleep 2;
  done

  echo "Done ...";

  create_adminkey

}

And here is the output:

+ WORKER_IP=
+ openssl req -new -key -worker-key.pem -out -worker.csr -subj /CN= -config worker-openssl.cnf
Error Loading request extension section v3_req
139752372545168:error:220A4076:X509 V3 routines:a2i_GENERAL_NAME:bad ip address:v3_alt.c:476:value=
139752372545168:error:22098080:X509 V3 routines:X509V3_EXT_nconf:error in extension:v3_conf.c:95:name=subjectAltName, value=@alt_names
+ WORKER_IP=
+ openssl x509 -req -in -worker.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out -worker.pem -days 365 -extensions v3_req -extfile worker-openssl.cnf
Error Loading extension section v3_req
140642872743568:error:220A4076:X509 V3 routines:a2i_GENERAL_NAME:bad ip address:v3_alt.c:476:value=
140642872743568:error:22098080:X509 V3 routines:X509V3_EXT_nconf:error in extension:v3_conf.c:95:name=subjectAltName, value=@alt_names

I can't read the value of a variable inside other variable and execute commands within that variable.

So any idea to help me?

Thanks

-- Rodrigo Andrade
kubernetes
linux
shell

2 Answers

8/30/2016

I find the issue and below following my workaround.

  function create_worker()
  {

    echo "Generate the Kubernetes Worker Keypairs ...";

    WORKER_NUMBER=2
    WORKERS_FQDN=("worker1" "worker2" "worker3")
    WORKERS_IP=("192.168.0.10" "192.168.0.11" "192.168.0.12")
    MASTER_IP=("192.168.0.20")
    K8S_SERVICE_IP=10.3.0.1

    for ((i=0; i <= "$WORKER_NUMBER"; i++));
    do
      echo "${WORKERS_IP[i]}" --- "${WORKERS_FQDN[i]}";
      openssl genrsa -out "${WORKERS_FQDN[i]}"-worker-key.pem 2048
      WORKER_IP="${WORKERS_IP[i]}" openssl req -new -key "${WORKERS_FQDN[i]}"-worker-key.pem -out "${WORKERS_FQDN[i]}"-worker.csr -subj "/CN="${WORKERS_FQDN[i]}"" -config worker-openssl.cnf
      WORKER_IP="${WORKERS_IP[i]}" openssl x509 -req -in "${WORKERS_FQDN[i]}"-worker.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out "${WORKERS_FQDN[i]}"-worker.pem -days 365 -extensions v3_req -extfile worker-openssl.cnf
      sleep 2;
    done

Now everything it's working fine.

Thanks you all.

Kind regards,

-- Rodrigo Andrade
Source: StackOverflow

8/22/2016

You don't say whether you are seeing that error on the first iteration of your loop or the last iteration. I'm assuming the latter, based on what I see of your shell script.

If I simplify your script to:

echo "Generate the Kubernetes Worker Keypairs ..."

WORKER_NUMBER=3
WORKERS_FQDN=("worker1" "worker2" "worker3")
WORKERS_IP=("192.168.0.10" "192.168.0.11" "192.168.0.12")
MASTER_IP=("192.168.0.20")
K8S_SERVICE_IP=10.3.0.1

for ((i=0; i <= $WORKER_NUMBER; i++)); do
    echo WORKER_IP="${WORKERS_IP[i]}"
done

echo "Done ...";

I see as output:

Generate the Kubernetes Worker Keypairs ...
WORKER_IP=192.168.0.10
WORKER_IP=192.168.0.11
WORKER_IP=192.168.0.12
WORKER_IP=
Done ...

That last iteration, in which WORKER_IP= shows no value, is due to an erroneous comparision in your loop. You have:

for ((i=0; i <= $WORKER_NUMBER; i++)); do

This will iterate over the values 0, 1, 2, and 3. Your list only has three values, which are ${WORKERS_IP[0]} through ${WORKERS_IP[2]}. There is no ${WORKERS_IP[3]}. Change your loop to:

for ((i=0; i < $WORKER_NUMBER; i++)); do

...and things should work as intended.

Now, having said this, you can probably make things more manageable by doing this:

for WORKER_IP in "${WORKERS_IP[@]}"; do
    echo WORKER_IP="$WORKER_IP"
done

This doesn't require keeping track of the length of $WORKERS_IP.

-- larsks
Source: StackOverflow