Answers: <p>This is easy if you reference the module in Ansible correctly.</p>
  <pre><code>- hosts: &quot;TimeMachines&quot;
    tasks:
      - name: Calibrate Flux Capacitors
        script: &quot;scripts/calibrate_flux_cap.ps1 -some_arg 'some_value'&quot;
  </code></pre>
  <p>This works, along with the arg and value. In this example, the arg is consumed by the powershell script using the params() ability, which is a powershell feature.</p>
  <p>I think the Ansible docs should be updated with an example that indicates this type of use is possible. Weird that their doc's examples give that full name and that Ansible blows up if you actually use it.</p>
  <p>Update:
  I added an example for the Ansible docs that is currently in review in GitHub, here: <a href="https://github.com/ansible/ansible/pull/76980" rel="nofollow noreferrer">https://github.com/ansible/ansible/pull/76980</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2022-02-07T23:58:23.140Z
Answers: 2022-02-08T00:35:38.443Z
Answers: 2022-02-08T00:35:38.443Z
Answers: James T Snell
Answers: <p>I've found a half-solution to this. You can load a file in to a Ansible variable and use that. So instead of having an inline script, you could store your local script file in to a variable string and then pass it to a shell module like win_shell.</p>
  <p>Example:</p>
  <pre><code>- hosts: &quot;DocsTimeMachines&quot;
    tasks:
      - name: Calibrate Flux Capacitors
        win_shell: &quot;{{ lookup('file', 'scripts/calibrate_flux_capacitor.ps1') }}&quot;
  
  </code></pre>
  <p>In the above example, under the directory with my playbook, there's a script &quot;scripts&quot; directory with the referenced script file.</p>
  <p>How this kinda sucks still is I want to be able to call the script and use named arguments, for instance, the script call might look like:</p>
  <pre><code>scripts/calibrate_flux_capacitor.ps1 --mrfusion_enabled=false --hover_systems_enabled=false --retro_1950s_transistor_hack_enabled=true
  </code></pre>
  <p>I don't think I can call it like this with my string variable solution here. I'll leave this open for other answers and accept one that can actually call the script, if it comes up.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-02-07T23:31:53.557Z
Answers: 2022-02-07T23:31:53.557Z
Answers: James T Snell
Body: <p>I'm trying to write a play that will take a locally stored powershell script from my Ansible server and run it on Windows hosts. I have been using the &quot;win_shell&quot; module to run scripts placed inside my playbook files, but for testability I want to have the scripts being executed stored outside of my playbook yaml files.</p>
  <p>Here's what I'm trying right now:</p>
  <pre><code>- hosts: &quot;AnsibleDevTestingHosts&quot;
    tasks:
      - name: &quot;showing playbook_dir&quot;
        debug:
          msg: &quot;{{ playbook_dir }}&quot;
  
      - name: Testing Ansible
        ansible.builtin.script: &quot;{{ playbook_dir }}/scripts/calibrate_flux_capacitor.ps1&quot;
  </code></pre>
  <p>When I run this, I'm currently getting back an error message:</p>
  <pre><code>TASK [Testing Ansible] *********************************************************
  An exception occurred during task execution. To see the full traceback, use -vvv. The error was: IndexError: list index out of range
  fatal: [delorian-car-1.local]: FAILED! =&gt; {&quot;msg&quot;: &quot;Unexpected failure during module execution.&quot;, &quot;stdout&quot;: &quot;&quot;}
  
  </code></pre>
  <p>I've been looking at the <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html" rel="nofollow noreferrer">Ansible docs for ansible.builtin.script</a> and it seems pretty simple to use (and supports Windows). So, I'm unsure what the problem here is.</p>
  
Tags: <ansible><powershell>
Title: Ansible: How to run local powershell script on remote hosts
UserDisplayName: James T Snell
ThreadText: # Ansible: How to run local powershell script on remote hosts
  
  (Asked by: username_0 on 2022-02-07)
  
  I'm trying to write a play that will take a locally stored powershell script from my Ansible server and run it on Windows hosts. I have been using the "win\_shell" module to run scripts placed inside my playbook files, but for testability I want to have the scripts being executed stored outside of my playbook yaml files.
  
  Here's what I'm trying right now:
  
  ```
  - hosts: "AnsibleDevTestingHosts"
    tasks:
      - name: "showing playbook_dir"
        debug:
          msg: "{{ playbook_dir }}"
  
      - name: Testing Ansible
        ansible.builtin.script: "{{ playbook_dir }}/scripts/calibrate_flux_capacitor.ps1"
  
  ```
  
  When I run this, I'm currently getting back an error message:
  
  ```
  TASK [Testing Ansible] *********************************************************
  An exception occurred during task execution. To see the full traceback, use -vvv. The error was: IndexError: list index out of range
  fatal: [delorian-car-1.local]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}
  
  ```
  
  I've been looking at the [Ansible docs for ansible.builtin.script](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html) and it seems pretty simple to use (and supports Windows). So, I'm unsure what the problem here is.
  
  **Tags:** ansible, powershell
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2022-02-07. Score: 1 | Accepted answer
  
  This is easy if you reference the module in Ansible correctly.
  
  ```
  - hosts: "TimeMachines"
    tasks:
      - name: Calibrate Flux Capacitors
        script: "scripts/calibrate_flux_cap.ps1 -some_arg 'some_value'"
  
  ```
  
  This works, along with the arg and value. In this example, the arg is consumed by the powershell script using the params() ability, which is a powershell feature.
  
  I think the Ansible docs should be updated with an example that indicates this type of use is possible. Weird that their doc's examples give that full name and that Ansible blows up if you actually use it.
  
  Update:
  I added an example for the Ansible docs that is currently in review in GitHub, here: <https://github.com/ansible/ansible/pull/76980>
  
  
  ---
  
  ### Answer by username_0 on 2022-02-07. Score: 1
  
  I've found a half-solution to this. You can load a file in to a Ansible variable and use that. So instead of having an inline script, you could store your local script file in to a variable string and then pass it to a shell module like win\_shell.
  
  Example:
  
  ```
  - hosts: "DocsTimeMachines"
    tasks:
      - name: Calibrate Flux Capacitors
        win_shell: "{{ lookup('file', 'scripts/calibrate_flux_capacitor.ps1') }}"
  
  ```
  
  In the above example, under the directory with my playbook, there's a script "scripts" directory with the referenced script file.
  
  How this kinda sucks still is I want to be able to call the script and use named arguments, for instance, the script call might look like:
  
  ```
  scripts/calibrate_flux_capacitor.ps1 --mrfusion_enabled=false --hover_systems_enabled=false --retro_1950s_transistor_hack_enabled=true
  
  ```
  
  I don't think I can call it like this with my string variable solution here. I'll leave this open for other answers and accept one that can actually call the script, if it comes up.
  
  
  ---
  
  