win_shell completing command but not ending process - powershell

I'm learning Ansible and am running into something I haven't seen a good reference for anywhere -
I have a .ps1 on my target machine that I want to run via Ansible, while passing it a variable as well. My playbook for it is pretty simple:
---
- name: run stuff
hosts: all
gather_facts: no
vars:
force_all_hosts: false
tasks:
- name: run mounter script
win_shell: C:\folder\file.ps1 -password (ConvertTo-SecureString "{{vault_pw}}" -AsPlainText -Force)
The contents of the .ps1 work when I run them manually local to the Windows machine, so I know the actual commands work. When I run the playbook it works as well - the Powershell script runs succesfully and the changes are made - but the actual task never completes so the playbook can't finish.
I added a "< NUL" to the end of the win_shell line to force a message that said the return code wasn't 0 - the RC was 1 - but then I updated Ansible to 2.8 and now the "<" character is reserved so I can't recreate the return code failure anymore.
Is there a way to force the return code, and filter if it's <= 2 or something? Or better yet, is there another way to ensure the job ends other than using async/poll since that causes the play to report that it failed?

Can you try the async option in ansible?
https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html

Related

GitLab CI: allow_failure:exit_codes does not behave as expected with powershell

Using a shell executor with powershell, allow_failure:exit_codes do not behave as documented
https://docs.gitlab.com/ee/ci/yaml/#allow_failureexit_codes
With the following very simple job:
build:
script:
- exit 4
allow_failure:
exit_codes:
- 4
The job fails instead of passing with a warning sign.
It works perfectly with bash.
it works perfectly with allow_failure: true.
Am I missing something ? Is there a work around ?
This is an aspect of how Powershell works, in general. See: Returning an exit code from a PowerShell script
To workaround this for a shell executor running powershell, use the following:
script:
- $host.SetShouldExit(4); exit 4
# ...
Note, however this workaround may not work for custom executors, like the beta shared-windows autoscaling executors on gitlab.com -- there is no workaround in that case at this time of writing.

Execute powershell script with gitlab-runner on local windows machine

I do have following setup:
a win PC with gitlab-runner installed (working)
a powershell script running on the same PC is starting an application
a gitlab server to connect this local PC and starting the powershell script
Now when starting the powershell script directly from the local PC, the application starts and terminates after done - working as expected. When starting the same powershell script with the gitlab server (yml-file) then I can see that the application has been started (new process in taskmanager) but it is not running as well it never terminates.
When manually end the task I see that gitlab terminates again.
Question:
what could be the root cause?
is it possible to run the powershell script with gitlab-runner? I think there is a way with the command "exec". How does the command looks like when calling the powershell script?
is it possible to run the application not in the background in order to see whats going on?
others?
thanks in advance
I think there is a bug with the gitlab runner on windows.
No matter which shell you configure in the config.toml the runner
will always use cmd.exe for an exec local run.
Specify the --shell argument to override the default cmd.exe shell:
> gitlab-runner exec shell your_job --shell pwsh
If you run this locally in your project, it outputs to .builds/, so add this to your .gitignore because git will see it and think you might want to add a submodule.

Can Ansible keep a process ruining even after the playbook has ended?

i have an open-source program i want to run from ansible basically ansible will go into the node and run "./Program.Name" which will start the program but when ansible-playbook is done the program closes is there a way I can start the program and keep it running even after ansible is done? I was told there is the async module but how can I write the playbook so it will keep the program running for as long as the node is up. Please try to provide the yml code if possible where I can replace the name of the program and it should do the job
I have tried to run the porcess with "./Program.Name &" but it does not stay running.
The following methods may be useful to you, and the async and poll parameters are necessary,more info see ansible doc
- name: run
shell: "( tail > /dev/null 2>&1 &)"
async: 10
poll: 0
Note: If you want to run the program with a daemon, I think supervisord may be more appropriate.
async poll = 0
just never check back in on the task
https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html#concurrent-tasks-poll-0

Bamboo Powershell Task fails after first run

I'm completely new to Bamboo, so thank you in advance for the help.
I'm trying to create a Bamboo Run that zips files from a git repo and uploads it to Artifactory. Currently my build contains 2 tasks - source code checkout and a simple powershell script. The first time I run it it builds perfectly fine, but without any modifications any consecutive runs fail.
The error I'm getting in the log is the following:
Failing task since return code of [powershell -ExecutionPolicy bypass -Command /bin/sh /opt/bamboo/agent/temp/OR-J8U-JOB1-4-ScriptBuildTask-539645121146088515.ps1] was -1 while expected 0
Replacing the powershell script with empty space does not resolve the issue - only removing the script completely allows the build to succeed, but I cannot reinsert a new script or it will fail. I read other online questions suggesting that I "merge the user-level PATH environment information in to the system-level PATH" but I cannot find the user-level environment information, my environmental variables section is completely empty.
Like Vlad, I found that it was more efficient to implement my powershell script with batch.

PowerShell script called from within the Jenkins PowerShell build step hangs indefinitely

I have a Jenkins (1.493) project that uses the Jenkins PowerShell build step to execute a PowerShell script. Inside that script I want to invoke another script that is stored inside a file. I have now reduced it to the following:
Script inside Jenkins PowerShell build step:
& "\\stemmer.local\sidevelopment\cvdev\devbase\jenkins\PowerShell\Test.ps1"
Content of Test.ps1:
write-host 'Hello world!'
Whenever this Jenkins project executes, the PowerShell build step hangs indefinitely.
Things I have tried/verified so far:
Adding some output before the invocation of Test.ps1 shows me that the Jenkins PowerShell script is being execute normally up to the point where Test.ps1 is called.
The file Test.ps1 exists and is reachable from the build slave that executes the script. If I alter the file's name, I get the expected error message from PowerShell...
Exchanging the " for ' in the 1st script does not change anything. Also, using dot-sourcing rather than & does not help.
The file Test.ps1 can be executed properly from the powershell itself using the same command line that is being used in the Jenkins PowerShell script.
The execution policy for PowerShell has been set to unrestricted on my development host as well as on the Jenkins build slave.
I've tried replacing the PowerShell build step with a Windows batch command build step that looks like this:powershell.exe -InputFormat None -File "\\stemmer.local\sidevelopment\cvdev\devbase\jenkins\PowerShell\Test.ps1"and played around a little with the parameters of powershell.exe, but the results were - in those cases that were syntactically and otherwise correct as far as I can tell - always the same.
I only found few references to problems that sounded similar, but none of the approaches mentioned elsewhere did help me fix this. I am absolutely puzzled, and wondering whether someone encountered this issue before (and maybe even got a scenario like the one I have in mind running).
Thanks a lot for any input!
Volker
have you tried to set execution policy to bypass ?
Copy the script file locally, then invoke it from within the Jenkins PowerShell plugin - that way it works as expected.