Ansible 显示实时 shell 标准输出

Vee*_*anu 5 stdout ansible

我想查看实时shell标准输出,而不是register在变量中输入,然后在完成后显示。

示例剧本 - test.yml

- name: Testing RUN Shell Command
  hosts: localhost
  connection: local

  tasks:
  - name: Runnig Update
    shell: apt update
Run Code Online (Sandbox Code Playgroud)

默认输出

$ ansible-playbook  test.yml 

PLAY [Testing RUN Shell Command] ******************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [localhost]

TASK [Runnig Update] ******************************************************************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

Run Code Online (Sandbox Code Playgroud)

但是我想看看当我们运行apt update终端时它在做什么

$ sudo apt update
Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://dl.google.com/linux/chrome/deb stable Release                                                                            
Hit:3 http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu bionic InRelease                                                              
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                                                           
Hit:6 http://in.archive.ubuntu.com/ubuntu bionic InRelease                                                                            
Hit:7 http://ppa.launchpad.net/canonical-chromium-builds/stage/ubuntu bionic InRelease                                                
------OUTPUT REMOVED----------
Run Code Online (Sandbox Code Playgroud)

我看到已经在github 问题上进行了讨论,看起来这是不可能的。

有什么技巧可以通过帮助ansible Callback获得实时标准输出吗?

Joh*_*ald 6

“实时”或“流式”输出尚未合并到 Ansible。将原来的问题是在2014年关闭的不可行。一个更近的建议有自2018年2月没有大的更新。

这对 Ansible 用户不可用。它需要对命令和回调插件进行认真的黑客攻击,更重要的是测试,才能按建议工作。


奖励剧本审查:您的示例命令可以由 apt 模块完成。这比自己调用 apt 命令行具有更多功能。

- name: Only run "apt-get update" if the last one is more than 3600 seconds ago
  apt:
    update_cache: yes
    cache_valid_time: 3600
Run Code Online (Sandbox Code Playgroud)