1

I have a simple powershell script(debug.ps1) which takes user input from read-host and displays it

$var = read-host "Enter text"
write-host $var

I want to run this from ansible but when I try through stdin, the job executes indefinitely. How do I pass input to read-host from ansible?

  tasks:
    - name: Executing script
      win_shell: .\debug.ps1
      
      register: output
      args:
        chdir: C:\Scripts\
        stdin: 'hello'

    - debug: var=output.stdout_lines
1
  • Try the expect module. Note the requirements on the documentation. From a very quick search, it looks like pexpect is available for windows. Commented Mar 25, 2021 at 14:43

1 Answer 1

1

Stay away from Read-Host as it's intended for interactive logon sessions, not automation. Convert to a script that accepts parameters and feed a parameter as an argument. For more advanced cases (which is literally everything other than 'helloworld') check into parameter options.

<#
.SYNOPSIS
  Do this instead
#>
Param(
  [string]$Text
)

$text

Write-Host used to be really bad because it only wrote to the console, not to one of the out streams (Powershell has 6 last I checked). However in a recent update, Write-Host now out's to the Information stream. What you need is data out on the Output stream for it to be collected properly.

So in short, stay away from Read-Host and Write-Host, use parameterization and out as object or datatype. In this case, a [string] is a datatype.

Sign up to request clarification or add additional context in comments.

Comments

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.