Hi,
I am trying to run bash script from inside local-exec. but I have noticed that terraform will stuck forever in the loop, and if I add if statement to check the status it will always return true!
Here is an example of what I am trying to achieve :
,,,,,
resource "null_resource" "runjob" {
provisioner "local-exec" {
#command = "chmod +x ${path.module}/runjob.sh ; ${path.module}/runjob.sh"
command = <<EOT
#!/bin/bash
set -ev
## Code removed #########
while [ $RUN_STATE=="Run" -o $RUN_STATE=="Pending" ]
do
sleep 30
RUN_STATE=$(bedo get job status| jq -r ".state.life_cycle_state")
echo "Waiting for Job to complete... Current state is: $RUN_STATE"
done
# Check if job succeeded
RESULT_STATE=$(bedo get job status | jq -r ".state.result_state")
(echo "Result state of Job: $RESULT_STATE")
if [ $RESULT_STATE=="SUCCESS" -o $RESULT_STATE=="TERMINATED" ]; then
(echo "Job succeeded!"); exit 0;
else
(echo "Job failed!"); exit 0;
fi
EOT
}
}
so basically I want to run while loop & if statement
thank you in advance for your support.
Thanks @maxb for your reply, i tried to run it from .sh file, but it gave me the same result
also using spaces between the operator will lead to this error “[: Pending unexpected operator” hence I have removed the spaces
so, is there any specific syntax for the while loop & IF statement
I thought of another thing that could trip you up, as well…
As previously mentioned, the local-exec provisioner’s command argument is built to take a single command, not a script.
As a result, your line:
has no function, being read as just a simple comment. In particular, it does not cause the use of bash - instead you get whatever the system default /bin/sh is, which might be bash, or it might not.
The use of the == operator with [ is a bash extension, so may be read as an error. (The correct standard equality operator in [ syntax is just =.)
You may also experience other problems, if you have bash-specific code in the part of your script that you did not post.
# While job is not finished keep checking run state
while :
do
RUN_STATE=$(bedo get job status | jq -r ".state.life_cycle_state")
echo "in loop"
if [ $RUN_STATE="RUNNING" -o $RUN_STATE="PENDING" ]; then
echo "Waiting for Job to complete... Current state is: $RUN_STATE"
else
echo 'Done!'
break
fi
sleep 20
done
Output - it will not exit from the loop
meanwhile, if I add space like this
while :
do
RUN_STATE=$(bedo get job status | jq -r ".state.life_cycle_state")
echo "in loop"
if [ $RUN_STATE == "RUNNING" -o $RUN_STATE == "PENDING" ]; then
echo "Waiting for Job to complete... Current state is: $RUN_STATE"
else
echo 'Done!'
break
fi
sleep 20
done
Change the == for =. As per my previous post, it seems you are indeed on a system where /bin/sh is not bash - the observed behaviour matches that from the shell dash commonly found on Debian/Ubuntu and similar.