Search code examples
windowsansiblecygwinansible-2.x

How to run a bat script on remote windows system after successful ssh using ansible


I am trying to run a batch file with an argument on a remote windows machine from my local Cygwin console using Ansible. I have successfully configured ssh to a remote windows machine using OpenSSH. If I run this ansible command, I get this output.

ansible -i inventory.yml -m win_ping windows_server1 -vvv

windows_server1 | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": "pong"
        }
    },
    "ping": "pong"
}

My inventory file is as below.

---
all:
  hosts:
    windows_server1:
      ansible_connection: ssh
      ansible_shell_type: cmd
      ansible_host: "host1"
      ansible_user: "user1@DOMAIN"

Now when I add a playbook to execute a batch file with arguments, I get an error. Tried script, win_command, win_shell, win_psexec modules with no luck. Here is the code in my playbook.

---
- hosts: windows_server1
  become: yes
  become_method: runas
  become_user: user1
  tasks:
    - name: Invoke App stop script
      win_command:
        cmd: '"D:\Temp\appstop.bat" "argument 1" -stop'

Here is the ansible command I used.

ansible-playbook -i inventory.yml playbook-01.yml -vvv

Here is the error I am getting now.

fatal: [windows_server1]: FAILED! => { "changed": false, "msg": "Get-AnsibleParam: Missing required argument: _raw_params" }

Appreciate any hints to proceed with this. Thanks


Solution

  • Try the following code snippet

    - name: Run a bat script on remote windows system after successful ssh using ansible
      hosts: windows
      tasks:
        - name: Run a bat script on remote windows system after successful ssh using ansible
          win_shell: C:\Users\test\test.bat
          register: result
          ignore_errors: yes
        - name: Display output
          debug:
            var: result.stdout_lines