gpt4 book ai didi

powershell - 从PowerShell到Ansible的输出,以用于Ansible条件/控制播放执行

转载 作者:行者123 更新时间:2023-12-03 01:11:30 27 4
gpt4 key购买 nike

我通过调用PowerShell脚本在Windows主机上使用Ansible。效果很好,但我的Playbook要求只根据条件播放剧本(使用Ansible时间)。

我面临的挑战是如何从PowerShell输出到Ansible。作为一个用例,假设我有两个角色。第一个播放调用脚本以检查PowerShell版本号,第二个播放仅在PowerShell版本为5.0时运行。

如果可能的话,我如何将第一部剧本的PowerShell脚本中的变量输出回Ansible,该变量可以在第二部剧本的“何时”使用,以允许或阻止执行?

最佳答案

对于这种特定情况,您可能只想使用ansible_powershell_version事实,例如:

- name: do PS5-only thing
raw: Run-SomePS5Thing
when: ansible_powershell_version >= 5

但是通常,要捕获var中的内容并在以后使用,您可以仅在命令任务上将 register:关键字与下游任务上的过滤器结合使用,以提取所需的值,例如:
  # Filters can deal with text too, but since it's Powershell, 
# let's use structured data- ConvertTo-Json gives us something Ansible
# can read with the from_json filter
- raw: Get-NetRoute 0.0.0.0/0 | ConvertTo-Json -depth 1
register: routeout

# use from_json to read the stdout from raw and convert to a dictionary
# we can pull values from
- debug: msg="Default gateway is {{ (routeout.stdout | from_json).NextHop }}"

# or do something conditionally:
- debug: msg="Default gateway isn't what we expected!"
when: (routeout.stdout | from_json).NextHop != "192.168.1.1"

关于powershell - 从PowerShell到Ansible的输出,以用于Ansible条件/控制播放执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38923475/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com