Ansible Tower: Writing job stdout to files - ansible-awx

Is there any way in Ansible Tower to create log files for the each job stdout? I am aware that the below API will show me the output.
https://<tower_ip>/api/v2/jobs/<job id>/stdout/
But we also want these output into separate files for each job. Could you please help me with this?

According the Tower API Reference Guide Jobs it is possible to get via REST API call a formatted downloadable plain text, in example
curl --silent -u ${TOWER_USER}:${TOWER_PASSWORD} --location https://${TOWER_URL}/api/v2/jobs/${JobID}/stdout?format=txt_download --output job_${JobID}.log
and write it into a file like job_${JobID}.log.
Thanks to
man curl

Related

Trying to issue a curl POST command in PowerShell

I have been able to use curl to issue a query and return a result or series of named parameters.
I want to issue a POST command on these series of named parameters, but the "multiple" is tripping me up.
I can issue the POST command on one of the parameters, but not on the entire series of them.
The command I'm using within powershell is
curl.exe -u username:password -X POST http://site.url:8042/modalities/MCTEST/store -d '["6a3eb7c4-a9d83950-24d36e94-5c20d248-0b5ce989","8c93b430-757278ab-21ab643c-c98aa03d-da14148e","b8f648fa-175de243-de76b1c0-2dc7551a-928b86a5","c865b966-f0c7d2c2-0a1114e0-80531305-31cd104e","aac7f73f-fcb922ef-b950c4c1-ee1d512e-e2aeb5ae"]'
If I issue the command on only one, it processes fine...obviously, I don't need quotes or brackets, which I think is what is tripping me up.
curl.exe -u username:password -X POST http://site.url:8042/modalities/MCTEST/store -d 6a3eb7c4-a9d83950-24d36e94-5c20d248-0b5ce989
I've tried subbing the double quotes with a backslash, tried adding a backslash, I've tried everything i can think of...I'm missing something.
I get a response of "must provide a json value"
I'm not sure what my error is in my syntax.
Any ideas?
external commands have to be called/quoted properly
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Execution of External Commands in PowerShell Done Right
Using Windows PowerShell to run old command-line tools (and their
weirdest parameters)
See also about Redirection
Powershell: Pipe external command output to another external command
How to use the curl command in PowerShell?
Stackoverflow example by Ansgar Wiechers
$CurlArgument = '-u', 'xxx#gmail.com:yyyy',
'-X', 'POST',
'https://xxx.bitbucket.org/1.0/repositories/abcd/efg/pull-requests/2229/comments',
'--data', 'content=success'
$CURLEXE = 'C:\Program Files\Git\mingw64\bin\curl.exe'
& $CURLEXE #CurlArgument

Multiple (github) PR checks from single azure pipeline yaml

Maybe i'm missing something obvious, but...
Can i have several (github) PR checks from a single azure pipelines yaml?
For example, on this screenshot i have CI connected to azure pipelines where build & running tests happen within the same check:
Can i somehow separate them so i have 2 checks: Build and running tests and see them pass|fail separately?
if its possible to have N checks in a single yaml and have their
statuses posted separately
For this issue, the answer is yes, you can achieve this with script approach.
Here is an issue about Multiple GitHub checks, in this issue, someone has the same problem as you, and got a solution and the exact config is given in it.
Since the build environment is a shell, for example, you could wrap your lint commands in a shell script that traps the exit code and sends the status to GitHub:
#!/bin/sh
npm run lint
EXIT_CODE=$?
if [[ $EXIT_CODE == 0 ]]
then
export STATUS="success"
else
export STATUS="failure"
fi
GITHUB_TOKEN=<your api token>
curl "https://api.github.com/repos/$CI_REPO/statuses/$CI_COMMIT?access_token=$GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "{\"state\": \"$STATUS\", \"description\": \"eslint\", \"target_url\": \"$CI_BUILD_URL\"}"
exit $EXIT_CODE

Jenkins create Folder with REST API in groovy script

i am trying to create a Folder in Jenkins using a groovy script. For that i use an REST API call.
My starting point is was basically a curl command:
curl -XPOST "http://userName:YourApiToken#JenkinsURL:Port/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder" -H "Content-Type:application/x-www-form-urlencoded"
I got this from https://gist.github.com/marshyski/abaa1ccbcee5b15db92c and found later out that it also works if you put the user into the URL. Using this line in the Windows cmd works perfectly and generates me a new Folder in my Jenkins.
Now i try to do the same thing but from a groovy script. For that i use the following code:
import groovyx.net.http.*
def post = new URL("http://userName:YourApiToken#JenkinsURL:Port/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}
I got this basically from Groovy built-in REST/HTTP client? (i have heard apache Http is deprecated)
Long story short i did not manage to create a folder with the given groovy script. I always get an 403 response (I run the script from windows cmd and inside jenkins with the script console).
I also played around with Jenkins-crumb but that seems not necessary because the above cmd command already worked.
So the question is does anyone know what i am missing? why is the groovy script not working but the curl command is?
Or has anyone a different approach on how to generate a Folder in Jenkins from a groovy script?
Edit:
When i try to run the curl command in the groovy script with .execute() i get an "The system cannot find the file specified" Error.
String command = "curl -XPOST "+ '"http://user.name:ApiToken#JenkinsURL:8080/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder"'+ ' -H "Content-Type:application/x-www-form-urlencoded"'
println(command)
command.execute()
curl command not present on windows platform - so you have
"The system cannot find the file specified".
403 - forbidden. something wrong with auth I guess.
better to move user&pass to header:
post.setRequestProperty("Authorization", "Basic ${"username:password".bytes.encodeBase64().toString()}")

How to execute Powershell script on the fly

If I have a Powershell script accessible through HTTP, how can I download it and execute it on the fly like what we can do in the bash?
curl --user user:pass https://fake.url | bash
First of all, you shouldn't do that. I mean, downloading random code from the web and running it without taking a look at it? Yikes!
Yes, it's convenient, but it's also a horribly stupid idea, just as curl | bash, or even worse, curl | sudo bash is.
In most cases you can probably use WebClient.DownloadString and pass it to Invoke-Expression: Invoke-Expression (New-Object Net.WebClient).DownloadString(«url»). I'm not sure whether that still opens a script: scope for variables, though, so if that scope is used in the script it might not work. In that case you can download it as a file and execute the script. Then you also have to take care of the execution policy, though.
Try
curl --user user:pass https://fake.url | powershell -Command -
If the value of Command is "-", the command text is read from standard input.

Jenkins CLI get test result

I am trying to find out if there is a command in the Jenkin's CLI, that provides you with an xml, txt, html or some file with test results from a job that was just executed. Does any one know of such command or way to do this?
The reason I'm asking is that I'm creating a job from an xml file via Jenkin's CLI. Then, I'm executing the job I just created and once the job is complete, I'd like to use a command and get the test results.
Thank you in advanced
David
You can just save the results of your test as artifact, and download it, as the url is known to be something like http://jenkins.yourcompany.com/job/yourjob/buildId/artifact/testResults.xml