cant execute bashfile using ansible

Hi Experts,

I am automating IBM CLM application and i would need to run the application default .bat file for starting the server. Eg: (server.startup.bat). I tried with win_command, script and win_shell modules and its not running the bash file.

Could you please provide any solution.

.bat extension is for Windows batch file. Bash files typically have .sh or .bash extension. Could you show the tasks you have tried and their respective output ?

Hi ,

Yes. It’s .bat file and it’s for stating a ibm liberty server “server.startup.bat”.

Hi Tony,

Any suggestions here please

Hello Experts,

Any suggestion here… ?

The win_command module can be used to execute batch files without any issues. If it is returning without any errors it sounds like the batch file is failing silently and not informing you of what has gone wrong. You can add some echo statements or create files to verify whether it is truly running but I did some tests myself just now and it had no issues.

Hello Jordan,

Thanks for your advise.

But same batch file u can execute in the server and it’s working. And also through powershell it’s getting executed.

Running through Ansible is different from running locally because the tasks are being run under a WinRM logon. Windows treats network (WinRM) and interactive (local) logons differently and can have different behaviour and access permissions depending on the type of logon. My recommendations still stand;

  • Split the batch file into smaller steps and run each one manually to see if it breaks
  • Add some logging to your batch file to log each step into a file to verify that is is actually running
  • Don’t use a batch file and convert to a PowerShell script instead
  • Use become on the task to replicate how it would run on an interactive logon
  • Use native PowerShell with Invoke-Command to see if that works, Ansible uses a similar mechanism to execute tasks

One thing I am curious about, the batch file is server.startup.bat which indicates this is trying to startup an executable. Unless you are using this to startup a scheduled task, service, or a process that is explicitly defined to breakaway from a job, you will find that once the initial WinRM process spawned from Ansible is completed, Windows will kill all of it’s child processes.

If this is the case, you really should be using a proper scheduler like Windows SCM or Scheduled tasks to run this and not try and spawn it directly from Ansible.

Thanks

Jordan

Hello Jordan.

Yes you are correct. Actually through ansible I already installed IBM liberty server using installer. And here the server.startup. bat file is the default IBM package getting deployed during the liberty server installation. So inorder to start the liberty server, I want to execute that batch file. Usually we are starting it through command line manually by running the batch file.

So here I tried to automate it through ansible playbook and my play was like as given below in this mail chain.

I am using server local admin user for winrm connection and installation process. Splitting the batch file will be a difficult task for us as it is a default IBM provided package and I am not well aware of that.

Is there anyways to overcome this. I used win_shell, scrit, win_command modules and it’s not happening.
Please advise me here.

I am very new to ansible and apologies for any repeated query.

Hi Jordan,
Any suggestions further to guide me here in this scenario…??

You never really said what the batch file actually does. Does it start a process that runs in the background or is it meant to run a short living process?

Hello Jordan,

the attached are the batch file, which will start and stop IBM Liberty servers and deploy the war files on this. Once it got started, a process will be running in windows for liberty server.

server.startup.bat

2 things;

  1. That’s a bash script and not a batch file, simply changing the extension from .sh to .bat will not work
  2. Processes spawned in WinRM are killed once the parent process exist

1 is either you copied the wrong script or are using the wrong file, I can’t help you there. 2 can be overcome in multiple ways;

  1. Use async to run the process in the background that is escaped from the WinRM job, this must have a timeout and cannot be run indefinitely
  2. Use the proper way of putting this in a scheduled task or Windows service and run it that way

You really should be using a scheduled task or Windows server to run this as it has fine controls over error actions, scheduling and the like. This isn’t designed to be run directly from Ansible except maybe for debugging purposes.

Thanks

Jordan

Hello Jordan,

ya, its my bad that i mistakenly pasted the wrong script. The batch script is as given below., Also, in my playbook, starting the liberty server using this batch file is a one task only and there are too many other tasks in continuous to this after the server got started.

could you please explain that how to use async for calling this startup batch file to start the server.

Batch file- server.startup.bat

Please find my current yaml file to execute the startup batch fille and could you please advise me what modification needed to do for using async here in this yaml.

Have a look at the async docs https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html. You want to use the async keyword and set ‘poll: 0’ as task directives.

As I said though, async is not the right tool for what you are looking for. You should be using a scheduled task or Windows service to run this. Using async will have lots of issues as;

* You can not run an async task indefinitely, you must set a max timeout value for each async task that Ansible spawns
* If the process ends abruptly, you need to use Ansible to start the task again, scheduled tasks and services can have more thourough error handling and can even restart the service if you want
* adding to the above, Ansible is on a separate host if you need to restart the process a service/task can be controlled using PowerShell on that same host
* You have limited visibility over the process apart from seeing if it is still running by checking all the currently running processes

Do this the right way now or else you will regret it in the future.