- 1 year ago
How to run a bat script on remote windows system after successful ssh using ansible
To run a batch script on a remote Windows system after a successful SSH connection using Ansible, you can utilize the win_shell
module. Here's an example playbook to demonstrate this:
yaml---
- name: Execute Batch Script on Remote Windows System
hosts: windows_hosts
gather_facts: false
tasks:
- name: Copy Batch Script to Remote System
win_copy:
src: /path/to/local/batch_script.bat
dest: C:\path\on\remote\batch_script.bat
remote_src: yes
- name: Execute Batch Script
win_shell: |
C:\path\on\remote\batch_script.bat
Here's a breakdown of the playbook:
The
hosts
line specifies the target hosts or host group where the batch script will be executed. Ensure you replacewindows_hosts
with the appropriate group or hostname defined in your inventory.The
win_copy
task copies the batch script from the local system to the remote Windows system. Modify thesrc
anddest
paths to match the paths on your local and remote systems, respectively.The
win_shell
task executes the batch script on the remote Windows system using thewin_shell
module. Adjust the path in the script accordingly to match the location of the batch script on the remote system.
Ensure you have SSH connectivity established between the Ansible control machine and the target Windows hosts using the appropriate SSH configuration for Windows. Additionally, make sure Ansible is properly configured to manage Windows hosts by installing the required dependencies and enabling WinRM.
Execute the playbook using the ansible-playbook
command, providing the path to the playbook file.
shellansible-playbook playbook.yml
Replace playbook.yml
with the actual name of your playbook file.
This playbook will copy the batch script to the remote Windows system and then execute it using the win_shell
module. Adjust the paths and commands to match your specific requirements.