1

I have a powershell script which takes multiple user inputs for file ex.- "C:\temp\foo.ps1", then it runs. I am trying to integrate that script with Ansible using win_shell module. How can I pass the user inputs for the powershell script :-

  - name: windows test command
    win_shell: C:\temp\Snapshots.ps1
    args:
     stdin: C:\temp\test3.csv

Script(powershell):-

$CONumber = Read-Host "Enter the CO Number"

SO this takes input from user, how can I edit this using vars_prompt variable?

1 Answer 1

1

To pass the content of your test3.csv as an input for your script, here is a way to do that :

- name: windows test command
  win_shell: C:\temp\Snapshots.ps1
  args:
    stdin: "{{ lookup('file', C:\temp\test3.csv) }}"

To pass the content of vars prompted to the user from the playbook, you can do like this :

#playbook_snapshot.yml
- hosts: "my_host"
  gather_facts: no
  vars_prompt:
    # co_number will be set to user input, and available in your role
    - name: co_number
      prompt: "Enter the CO Number"
    - name: other_value
      prompt: "Enter what is needed"
  role:
    - { role: wintest }

#role wintest
- name: windows test command
  win_shell: C:\temp\Snapshots.ps1
  args:
    stdin: "{{ co_number}}\n
      {{ other_value }}"
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks for replying, I am looking to feed answers for user inputs in the powershell script, how can I do that?
don't you already have the user inputs in test3.csv ? if not, you want your playbook to ask questions (look at vars_prompt) and then use the answers in your script. by the way in this case, use jinja vars in your script, and use module template to replace those vars with user inputs, then upload it, and run it
Hey, I added the script snippet in the question, which asks the user input, how can I replace it by vars_prompt variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.