如何使用ansible读取powershell脚本返回的值 [英] How to read the value returned by a powershell script with ansible

查看:40
本文介绍了如何使用ansible读取powershell脚本返回的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下 Powershell 脚本:

I am trying the following Powershell script:

Function jello-code {
    return "jello"
}

jello-code

在 Ansible playbook 中,我有一个任务定义为

In Ansible playbook I have a task defined as

- name : run jello script.
  win_shell: C:Projects\scripts\jello.ps1
  args:
    chdir:  C:Projects\scripts
  register : script_return
- debug : msg = {{script_return.stdout}}

当前调试任务显示的值为

Currently the value shown by debug task is

ok: [clientmachine.com] => {
    "msg": "Hello world!"
}

我总是收到相同的Hello world!" 消息.

I always get the same "Hello world!" message.

我的目标是根据 Powershell 脚本返回的值执行其他任务.

My goal is to do other tasks based on the value returned by the Powershell script.

推荐答案

嗯,这是因为 debug 模块的默认消息是 Hello world! 你可以参考到 参数 msg.

Well this happens because, the default message of the debug module is Hello world! you can refer to the documentation default of the parameter msg.

并且因为您的 YAML 语法是伪造的,所以您的 debug 任务会打印其默认值.

And because your YAML syntax is bogus, then your debug task prints its default.

一个好的建议是避免将 YAML 语法和旧的 key=value 混合在一起.
一个工作示例是:

A good recommendation would be to avoid mixing the YAML syntax and the older key=value one.
A working example of this would be:

- name: run jello script.
  win_shell: C:Projects\scripts\jello.ps1
  args:
    chdir:  C:Projects\scripts
  register: script_return
- debug: 
    msg: "{{ script_return.stdout }}"

如果您坚持使用其他语法,请注意您的问题来自您在等号周围放置的空格.

If you insist on having the other syntax, though, note that your issue is coming from the space that you put around your equal sign.

以下是带空格和不带空格的不同行为:

Here are the different behaviours with and without spaces:

- debug: msg={{script_return.stdout}}

是正确的并打印

"msg": "jello"

  • - debug: msg = {{script_return.stdout}}
    

    是假的和印刷品

    "msg": "Hello world!"
    

  • - debug: msg ={{script_return.stdout}}
    

    是假的和印刷品

    "msg": "Hello world!"
    

  • - debug: msg= {{script_return.stdout}}
    

    是假的和印刷品

    "msg": ""
    

  • 因此,简而言之,在此语法中,不要在等号周围添加空格.

    So, in short, in this syntax, do not add spaces around the equal sign.

    这篇关于如何使用ansible读取powershell脚本返回的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆