2014-09-18 634 views
14

我想知道在模塊執行時是否有辦法打印信息 - 主要是作爲證明過程正在運行並且沒有掛起的手段。具體來說,我試圖在執行cloudformation模塊期間得到反饋。我試圖修改(Python)的源代碼,包括以下內容:Ansible - 我可以在模塊執行期間打印信息嗎?

def debug(msg): 
    print json.dumps({ 
     "DEBUG" : msg 
    }) 

... 

debug("The stack operation is still working...") 

這是什麼做的,當然,是存儲所有這些輸出,僅打印出來後,所有的模塊都執行完畢。所以對於特別大的雲信息模板,這意味着我等待大約5分鐘左右,然後突然在屏幕上看到大量文字出現在最後。我期待的是每隔x秒打印一次「堆疊操作仍在工作......」。

看來Asynchronous Actions and Polling是我正在尋找的......但是這也沒有奏效。整個任務「爲{{stackname}}啓動CloudFormation」完全被忽略。請參閱下面從我的劇本的相關(YAML)片段:

- name: Launch CloudFormation for {{ stackname }} 
     cloudformation: > 
     stack_name="{{ stackname }}" state=present 
     region="{{ region }}" disable_rollback=true 
     template="{{ template }}" 
     register: cloud 
     args: 
     template_parameters: 
      KeyName: "{{ keyName }}" 
      Region: "{{ region }}" 
      SecurityGroup: "{{ securityGroup }}" 
      BootStrapper: "{{ bootStrapper }}" 
      BootStrapCommand: "powershell.exe -executionpolicy unrestricted -File C:\\{{ bootStrapper }} {{ region }}" 
      S3Bucket: "{{ s3Bucket }}" 
     async: 3600 
     poll: 30 

這告訴我,異步是爲典型的shell命令,而不是複雜的模塊,如cloudformation。 - 我可能做錯了什麼。

任何人都可以對這種情況有所瞭解嗎?再次,對於需要一段時間的大型雲計算任務,我希望定期指示任務仍在運行,而不是掛起。我感謝幫助!

回答

6

答案很簡單 - 沒有。 Ansible是一個連續系統,旨在處理在一堆服務器上運行的能力,並顯示實時標準輸出結果可能非常不方便。

但我想你可以使用一些技巧,如果你的目標系統可以支持在後臺執行。我看你的系統是windows,所以你必須在例如安裝cygwin到它運行後臺能力命令,如「睡眠20 &」的

您可以ansible-playbook -vv background.yml 運行這個劇本可以看出,標準輸出變化。 echo Test---- >> /tmp/test && tail /tmp/test是一個演示命令。您應該將數據輸出到某個文件並將其拖尾以查看進度。或者您可以查看標準輸出文件的文件大小並顯示它。使用想象力)))

# @file background.yml 

- hosts: 127.0.0.1 
    connection: local 
    gather_facts: no 

    tasks: 
    - name: Background operation 
    shell: "sleep 20 & \ PPID=$! \ echo $PPID" 
    register: bcktsk 

    - name: Check PPID 
    shell: "kill -0 {{ bcktsk.stdout | int + 2 }}" 
    register: checkppid 
    ignore_errors: true 

    - name: Check if process still active 
    shell: "echo Test---- >> /tmp/test && tail /tmp/test && kill -0 {{ bcktsk.stdout | int + 2 }}" 
    register: test 
    when: checkppid.rc == 0 
    until: test.rc ==1 
    delay: 2 
    retries: 10000 
    ignore_errors: true 
+1

謝謝。我至少可以做一些這樣的事情,以30秒的間隔報告一個簡單的提示「操作仍在進行......」。 Ansible並不支持真正的異步過程,這真是一種遺憾。 – Brian 2014-09-18 20:04:10

+1

雖然在實踐中,這似乎不適用於CloudFormation模塊。同樣,看起來只有簡單的shell命令可以異步執行(或作爲後臺進程)。**所以答案仍然是一個具體的NO。** – Brian 2014-09-19 14:55:42

0

有一種解決方法,它會顯示實時輸出,但是你必須在每一行處理注入的前綴。它的混亂/ hackish的,但對於調試/監控目的,幹得不錯:

- name: Run some command and echo it's realtime stdout 
    shell: ":" 
    with_lines: "/usr/bin/some_long_running_command with arguments and such here" 

命令:只是一個空操作,你可以方便地使用command: /usr/bin/true如果這是更容易。在任何情況下,這類似於將輸出線:

changed: [localhost] => (item=first output line) 
changed: [localhost] => (item=second output line) 
changed: [localhost] => (item=third output line) 
1

我的做法對本地主機模塊:

... 
module.log(msg='test!!!!!!!!!!!!!!!!!') 
... 

然後在另一個窗口:

$ tail -f /var/log/messages 

Nov 29 22:32:44 nfvi-ansible-xxxx python2: ansible-test-module test!!!!!!!!!!!!!!!!!