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 }}'"
Related
I'm currently coding a shell script for my GitHub Workflow and I want to test it locally.
However, it depends on an env variable provided by GitHub that consists of multiple nested segments, like a Javascript object for example.
my-bash-file.sh
PAYLOAD=${github.event.client_payload}
echo "$PAYLOAD"
How would I declare and inject such a kind of env variable locally when calling my script?
While you can define environment variables containing dots you won't be able to reference them from bash as identifiers can consist only of alphanumeric characters and underscores.
However, you can access them using other languages, like python. Call it my-python for example:
#! /usr/bin/env python3
import os
payload=os.environ['github.event.client_payload']
print(f'payload={payload}')
invoking it as
env github.event.client_payload=hello ./my-python
produces
payload=hello
I think I understand what you're trying to do. You're running a shell script as part of a GitHub action that looks something like this:
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: Install pre-commit
run: |
PAYLOAD=${github.event.client_payload}
echo "$PAYLOAD"
The best solution is to not reference github template variables directly in your shell script. Write it like this instead:
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: Install pre-commit
env:
PAYLOAD: "${github.event.client_payload}"
run: |
echo "$PAYLOAD"
This keeps the github template variable out of your script and instead uses it to set the PAYLOAD environment variable before the script runs. If you want to run the same script locally, you would just need to set the same PAYLOAD environment variable.
The examples here show a script embedded in the GitHub workflow; that's because there's no way to use a template variable embedded in a script file as you've shown in your question. If your workflow tried to run a script my-bash-file.sh that contained:
#!/bin/sh
PAYLOAD=${github.event.client_payload}
echo "$PAYLOAD"
It would fail with:
my-bash-file.sh: line 2: ${github.event.client_payload}: bad substitution
I am very new to ansible, i am writing a set of code where the result of a shell command goes to a Register variable, and in that variable, I need to find the keyword "happy". If the keyword is found, execute ANOTHER shell script. Shell command apparently outputs multiple different values, looks like a dictionary, it is not a straight forward string, so how do i find ONE word in the entire shell result.
My attempt:
- name: execute first task
shell: /tmp/somescript
register: myResult
- name: another shell script
shell: /tmp/anotherscript
when: "happy" in myResult.
This returns saying "condition was false", when the output DOES have happy in it per the output result.
I have linked a variable group to my build pipeline. The variable group has a variable in it. I am trying to use the variable in a bash script in my pipeline.
This document describes how to reference my variable from my variable group:
[https://learn.microsoft.com/en-us/vsts/pipelines/process/variables?view=vsts&tabs=yaml%2Cbatch][2]
However, $TESTING_YOLO does not work.
I have spent 2 hours trying to reference this variable from a bash script. Literally no idea how to do it.
I have figured out that variables in my variable group that come from key vault are not visible in my env variables when running the bash script.
Further, if I create a variable group that is not linked to key vault i.e. a variable group with key: value, yolo1: yolo1, those variables are visible in my env variables when running the bash script.
To summarise this update, variables in a variable group that come from key vault don't work as expected.
"However, secret variables (encrypted variables and key vault variables) cannot be accessed directly in scripts - instead they must be passed as arguments to a task". Quoted from here: https://learn.microsoft.com/en-us/vsts/pipelines/library/variable-groups?view=vsts
So we had to figure out how to pass arguments to our task. Here I am passing my secret yolo3 as an argument to my bash script task
Then I can reference the secret yolo3 as an argument in my bash script i.e. $1.
Hopefully, this will help someone else :).
The format to use the variables from variable group which links Azure key value as below:
$(VariableName)
Such as you can use the format $(yolo). But since the variable yolo is secret, the value will be marked as *** from the build logs.
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
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.