jugal
jugal

Reputation: 279

Run a batch file in windows using ansible

I want to run a run.bat file present on my remote windows node using my control node (CentOS) . Is it possible? If not is there any alternative to do so?

Upvotes: 6

Views: 24453

Answers (4)

blamb
blamb

Reputation: 4299

I use Call, and I took it a step further incase your trying to run it from a UNC path, which then we use the Become privs for that.

Keep in mind if your concatenating commands using win_shell, i found they need to be on the same line using the &&, i couldnt get it to work other ways. Ans 2.14.2 Python 3.11.2

   - name: Run batch file
     win_shell:  |
       pushd \\UNC-path\Labos\Extern\nomadconffiles && Call "run.bat"
     args:
       executable: cmd
     vars:
       ansible_become: yes
       ansible_runas_user:       "{{ win_user }}"
       ansible_become_user:      "{{ win_user }}"
       ansible_become_password:  "{{ win_pass }}"
     register: res_run_batch                                                                                                                    
   - debug:
       var:  res_run_batch

Upvotes: 0

blamb
blamb

Reputation: 4299

I use Call, and I took it a step further incase your trying to run it from a UNC path, which then you may also need the Become privs for that.

Keep in mind if your concatenating commands using win_shell, i found they need to be on the same line using the &&, i couldnt get it to work other ways. Ans 2.14.2 Python 3.11.2

   - name: Run batch file
     win_shell:  |
       pushd \\UNC-path\scripts && Call "run.bat"
     args:
       executable: cmd
     vars:
       ansible_become: yes
       ansible_runas_user:       "{{ win_service_user }}"
       ansible_become_user:      "{{ win_service_user }}"
       ansible_become_password:  "{{ win_service_pass }}"
     register: res_run_batch                                                                                                                    
   - debug:
       var:  res_run_batch

Upvotes: 0

Shivendra Gupta
Shivendra Gupta

Reputation: 1

Save the script (the batch file) on the control server. Ansible copies the script to the remote host and then execute it there. See example below — it is assumed that run.bat file is saved in /tmp (not a best practice) directory on your control server.

- name: execute batch
  script: /tmp/run.bat

Upvotes: 0

Vor
Vor

Reputation: 35149

Take a look at this section Windows Support

So what you want to do is create a task that looks similar to this:

- name: run simple script
  script: run.bat

Upvotes: 9

Related Questions