I have the following YAML file to store all the variables I use for the shell script. How do I pass one of the variable to the shell script? Please provide an example if possible.
testme:
Numberofservers: 3
EC2InstanceType: m4.2xlarge
DetailedMonitoring: 'true'
TerminationProtection: 'true'
DataEBSVolumeSize: 200
you can use this bash script https://gist.github.com/pkuczynski/8665367
to read your yaml and access the yaml property as bash variable
Related
I am new to shell and groovy and I have already googled and read solutions below, but none of it seems to be working
Groovy parameter to shell script
Pass groovy variable to shell script
read file to variable shell script
How to pass a groovy variable to a shell block jenkins
How to pass groovy variable to shell script in Jenkinsfile?
Issue:
I want to read value from the groovy variable and pass it to shell script but either I get blank value or the literal variable name
Below is my code:
def Tenant_Instances = ''
//some more declarations and code
if(tenant_created)
{
Tenant_Instances = "author.${ciTenant}.rally-dev.com,publish.${ciTenant}.rally-dev.com"
println("tenant instance are -"+Tenant_Instances) //value coming fine here
//some code here
println("tenant instance again are -"+Tenant_Instances) //values coming fine here too
sh '''#!/bin/bash
echo "[MNR] - Beginning deployment of bundle(s) to AEM Package Manager"
IFS="," read -ra INSTANCES <<< $Tenant_Instances
echo "demo ${INSTANCES}" -->here value does not come properly
I have tried replacing Tenant_Instances with 'Tenant_Instances' or "${Tenant_Instances}" or $Tenant_Instances or '$Tenant_Instances'... but none of it seem to work.
I've this task :
- name: script
script: "scripts/script.sh"
And in this script I would like to use ansible var like {{myvar}}
Is that possible ?
THKS
As far as I am aware there isn't a way of directly using an Ansible variable within a script, as you describe.
There are a number of different ways of achieving the behavior I think you're after.
The first option would be to write your script in such a way that the desired variable value can be passed in as an argument. As described in the script module docs:
The script module takes the script name followed by a list of space-delimited arguments.
As such, you could run a script which takes the variable as an argument like:
- name: script
script: scripts/script.sh {{myvar}}
Another option would be template out the script and use the Ansible template module to substitute the variable value directly into your script. You could then use either the command or shell Ansible modules to execute the script, configured from the template, on the remote node.
If you have a long string with newlines or spaces, prefer use single quotas to pass it as one sting
Also you might find it useful endings of stdout or stdout_lines
- name: script
script: "{{ role_path }}/scripts/script.sh '{{ myvar.stdout }}'"
I am not able to get output of the Environment-Variable BUILD_URL through Shell command.
Configured Manage Jenkins-->Configure System-->Jenkins Location-->Jenkins URL as http://xxx.xxx.xxx.xxx:8080/
Using below script within Shell command.
#!/bin/bash
echo ${BUILD_URL}
Do i have to set jenkins_url within the script?
In a Shell you can't access environment variables like that. you will need to read here: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps.
What you can do is in the Jenkins's job session you can send a parameter to your script to gain access to the env parameter. in the Shell section you will have access to the environment parameter, in the script itself it'll work differently.
Good luck
I want to use the parameters that we define in the Jenkins job as arguments to the shell commands in the same job.
I have created a parameterized build with the following parameters:
high.version: 234
low.version: 220
I want to use these variables as arguments for the build's shell script:
/bin/bash /hai/mycode/scripts/run_script.sh high.version
How do I these parameters in the same job?
Jenkins will create environment variables with the parameters' names.
The caveat here is that Jenkins will also do that for parameters that do not represent valid variable names -- those are difficult to access in bash. This is the case in your example, as bash variable names must not contain the . character.
The easiest solution is that you
rename your parameters, e.g. to high_version and low_version (which are valid bash variable names)
then use the corresponding variable names when calling your script
Example:
/bin/bash /hai/mycode/scripts/run_script.sh "$high_version"
If you cannot rename parameters to represent valid bash variable names (e.g., for usability reasons: Jenkins presents variable names to end users in the Web form for starting a build): you can still access such parameters by grepping for the parameter name in the output of the env command.
What really helped me was Hudson: How to pass parameters to shell script
Solution: the variables are UPPERCASE even you define them in lowercase!
Use following syntax to pass jenkins parameter to shell script -
eg. YourScript.sh %JENKINS_PARAMETER%
after that in your script,you can use that parameter like normal shell script command line parameter.
eg. myParam = $1;
Have you try this?
echo "function hello() { " > gg.sh
echo "echo \$1">> gg.sh
echo "}" >> gg.sh
echo "hello \$1" >> gg.sh
chmod 777 gg.sh
./gg.sh $hello_version
Be careful of the variable name, dot is not that well supported, for detail, you can ref this.
https://issues.jenkins-ci.org/browse/JENKINS-7180
It is not a good practice to have dot(.) in your parameters. You should either choose highVersion OR high_version as your param names.
As per your question, it seems that you're working with a Freestyle job but many devs coming here would also be interested in the Pipeline syntax as well, so I'm giving a solution to use params in Jenkins pipeline DSL.
There are two ways you can use Jenkins parameters in the Jenkins Pipeline shell script -
As a Shell parameter
stage('Test'){
sh "/bin/bash /hai/mycode/scripts/run_script.sh $highVersion"
}
As a Groovy parameter
stage('Test'){
sh "/bin/bash /hai/mycode/scripts/run_script.sh ${params.highVersion}"
}
I would recommend to use a second method, as we're using groovy as a pipeline DSL.
I have many scripts where I make use variables within the scripts. While the script is running, can I issue a command within the script to dump all of the variables and their values?
The command env on its own should display all the variables.
The builtin command set on its own will also dump the variables.
Note that inherited variables will also be dumped.
If you are after a way of helping you debug your script, you could put
set -x
At the beginning of your script that will display command traces including how your variables are used.