Re: [ansible-project] Powershell script runs locally but when run via ansible playbook doesn't produce the expected results.

14 views
Skip to first unread message

Avinash Jadhav

unread,
Jul 29, 2020, 10:29:16 AM7/29/20
to ansible...@googlegroups.com
Hello Darshan,

Could you please try to below 

---
-
  become: true
  become_user: ansible
  gather_facts: true
  hosts: CF-DEV-B
  ignore_errors: true
  tasks:
    -
      name: restart service
      script: C:\Users\Administrator\Documents\service.ps1 cdu
      register: out
    -
      debug: var=out.stdout_lines
  

Please let know if it is working or not 

Regards 
Avinash Jadhav


On Wed, 29 Jul 2020 at 19:23, Darshan Yamakanamardi <dars...@gmail.com> wrote:
Hello Ansible Experts,

 I have a PowerShell script that actually takes an argument and stops/starts a service based on the argument passed.

[CmdletBinding()]
param (
   [string] $SITE
)
$ServiceName =  Get-Website | findstr 80 | findstr $SITE | %{ $_.Split(' ')[0]; } | findstr Production
Write-Host -Object "The service to stop and start is  ColdFusion 9 - $ServiceName"
Stop-Service -Name "ColdFusion 9 - $ServiceName"
Start-Service -Name "ColdFusion 9 - $ServiceName"



When I run the above script locally on the windows machine, it stops/starts the service.

Screen Shot 2020-07-29 at 11.00.47 PM.png






When I attempt to run the same script via the Ansible playbook below bypassing the argument actually the stopping/starting service doesn't work. 

- hosts: CF-DEV-B
  gather_facts: yes
    tasks:
      - name: restart service
        win_shell: C:\Users\Administrator\Documents\service.ps1 cdu
        become: yes
        become_user: Administrator
        register: output
      - debug: var=output



Posting the debug section output here for your reference.


TASK [debug] ********************************************************************************************************************************************************************************************************************************

ok: [10.0.0.96] => {

    "output": {

        "changed": true, 

        "cmd": "C:\\Users\\Administrator\\Documents\\restart_service.ps1 cdu", 

        "delta": "0:00:01.099647", 

        "end": "2020-07-29 01:46:22.326932", 

        "failed": false, 

        "rc": 0, 

        "start": "2020-07-29 01:46:21.227285", 

        "stderr": "Stop-Service : Cannot find any service with service name 'ColdFusion 9 - '.\r\nAt C:\\Users\\Administrator\\Documents\\restart_service.ps1:8 char:1\r\n+ Stop-Service -Name \"ColdFusion 9 - $ServiceName\"\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (ColdFusion 9 - :String) [Stop-S \r\n   ervice], ServiceCommandException\r\n    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell. \r\n   Commands.StopServiceCommand\r\n \r\nStart-Service : Cannot find any service with service name 'ColdFusion 9 - '.\r\nAt C:\\Users\\Administrator\\Documents\\restart_service.ps1:9 char:1\r\n+ Start-Service -Name \"ColdFusion 9 - $ServiceName\"\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (ColdFusion 9 - :String) [Start- \r\n   Service], ServiceCommandException\r\n    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.


When executed via ansible-playbook the PowerShell fails to find the service name. 

While I know we have win_servive module to stop and start a service but that module doesn't help me in solving the problem which we have. Actually, the arguments will be passed to ansible-playbook by using "-e" extra argument options and the arguments are dynamically identified and passed based on which site is down

Any suggestions to fix this would be much appreciable. Thanks in advance for looking into it. 

Regards,
Darshan


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/beeccf92-8322-4f34-bf22-b6c288c7873eo%40googlegroups.com.

Darshan Yamakanamardi

unread,
Jul 29, 2020, 7:57:32 PM7/29/20
to Ansible Project



Hello Avinash,


Thanks for your response. And sorry for this duplicate post. (I missed the response from other community members) The original post is here: https://groups.google.com/d/msg/ansible-project/LGtMnY3b32E/XN5bRkBwCQAJ


The script module never worked for me. 


fatal: [10.0.0.96]: FAILED! => {"changed": false, "msg": "Could not find or access 'C:UsersAdministratorDocumentsservice.ps1'\nSearched in:\n\t/home/ec2-user/site-warmup/gs-int/files/C:UsersAdministratorDocumentsservice.ps1\n\t/home/ec2-user/site-warmup/gs-int/C:UsersAdministratorDocumentsservice.ps1\n\t/home/ec2-user/site-warmup/gs-int/files/C:UsersAdministratorDocumentsservice.ps1\n\t/home/ec2-user/site-warmup/gs-int/C:UsersAdministratorDocumentsservice.ps1 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}


I tried to copy the script using the win_copy module and set the remote_src to true.  It kept failing with the weird message. I didn't spend much time on it as I thought of using win_shell instead. 

Can we discuss this on the original post please?

Regards,
Darshan

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages