Ansible Script 模块如何为脚本中的每个命令返回执行结果? [英] How Ansible Script module return execution result for every command in the script?

查看:44
本文介绍了Ansible Script 模块如何为脚本中的每个命令返回执行结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ansible 脚本模块在远程主机中执行 shell 脚本.我的 shell 脚本 test.sh

I execute a shell script in remote hosts using ansible script module. My shell script test.sh is

pwd
echo "hello"
ls -l

我的 ansible 剧本是

My ansible playbook is

- name: Transfer and execute a script.
  hosts: server
  tasks:
     - name: Copy and Execute the script 
       script: /root/test.sh

另外,我想查看脚本中每个命令的执行结果.但我只看结果.

Besides, I want to view the execution result of every command in the script. But i only watched result.

changed: [172.18.96.134]
changed: [172.18.96.244]
changed: [172.18.96.245]

我怎样才能看到这样的结果:

How can i watch result like this:

/root
hello
a.txt  test.txt

推荐答案

你不能.在 Ansible 中无法在线跟踪模块.您可以从脚本中编写自己的日志.

You can't. It's not possible to trace a module on-line in Ansible. You can write your own log from the script.

处理此类要求的 Ansible 标准方法是 异步操作和轮询,以有效处理潜在大量主机的输出.

Ansible standard way to handle such requirements is Asynchronous Actions and Polling to efficiently handle the output of the potentially large number of hosts.

可以注册并打印模块的结果.

It's possible to register and print the module's result.

    - name: Copy and Execute the script 
      script: /root/test.sh
      register: result
    - debug:
        var: result.stdout

这篇关于Ansible Script 模块如何为脚本中的每个命令返回执行结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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