从Ansible运行powershell脚本

时间:2019-06-19 20:41:36

标签: ansible powershell-3.0

我有一个Powershell脚本,该脚本接受文件ex.-“ C:\ temp \ foo.ps1”的多个用户输入,然后运行。我正在尝试使用win_shell模块将该脚本与Ansible集成。如何传递Powershell脚本的用户输入:-

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

脚本(powershell):-

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

所以这需要用户输入,我如何使用vars_prompt变量对此进行编辑?

1 个答案:

答案 0 :(得分:1)

要将test3.csv的内容作为脚本的输入传递,这是一种方法:

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

要将提示的vars内容从剧本传递给用户,您可以这样做:

#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 }}"