2015-03-31 55 views
-1

我在我的CLi中使用了ansible。因此,我想自己做的所有錯誤處理,我不想提供額外的信息,可能迷失方向的用戶。在ansible.playbook.PlayBook中停止詳細信息

當我試圖在Python代碼運行ansible劇本,我已經收到詳細味精的:

PLAY [Role] *************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1] 

TASK: [base-devel-go | Install go lang support] ******************************* 
ok: [127.0.0.1] 

我用ansible-playbook作爲源爲我流。現在我看到了很多冗長的東西。我想隱藏它,但我找不到如何做到這一點?:

playbook = params["playbook"] 
inventory = ansible.inventory.Inventory(hosts) 
inventory.set_playbook_basedir(os.path.dirname(playbook)) 

stats = callbacks.AggregateStats() 
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) 
runner_cb = callbacks.PlaybookRunnerCallbacks(
    stats, verbose=utils.VERBOSITY) 


params["inventory"] = inventory 
params["stats"] = stats 
params["callbacks"] = playbook_cb 
params["runner_callbacks"] = runner_cb 

pb = ansible.playbook.PlayBook(**playbook_params) 
pr = pb.run() 

我試圖找到在callbacks.py但我的Python的水平是不太找到這個答案

+1

如果你不關心輸出,那麼執行'ansible-playbook pb.yml>/dev/null 2>&1'。你看到的不是詳細的。如果你想看到真正的詳細使用'-vvvv'選項。 – Kashyap 2015-03-31 17:35:20

+0

這就是我在python中運行時看到的! – 2015-04-01 08:41:59

+0

再次顯示:「你看到的不是詳細的」..它是「最小」的輸出,可靠的打印......將其重定向到「/ dev/null」! – Kashyap 2015-04-01 16:18:11

回答

0

它是禁用輸出最簡單的方法。 我在CLi中使用ansible。所以,當我運行它認爲Python綁定 - >有輸出。但我找到了禁用它的方法:

from ansible import callbacks 
# pylint: disable=too-many-arguments 
def display(msg, color=None, stderr=False, 
      screen_only=False, log_only=True, runner=None): 
    # prevent a very rare case of interlaced multiprocess I/O 
    callbacks.log_flock(runner) 
    msg2 = msg 
    if color: 
     msg2 = stringc(msg, color) 
    if not log_only: 
     if not stderr: 
      try: 
       print msg2 
      except UnicodeEncodeError: 
       print msg2.encode('utf-8') 
     else: 
      try: 
       print >>sys.stderr, msg2 
      except UnicodeEncodeError: 
       print >>sys.stderr, msg2.encode('utf-8') 
    if callbacks.constants.DEFAULT_LOG_PATH != '': 
     while msg.startswith("\n"): 
      msg = msg.replace("\n", "") 
     if not screen_only: 
      if color == 'red': 
       logging.error(msg) 
      else: 
       logging.info(msg) 
    callbacks.log_unflock(runner) 

# disable verbose 
callbacks.display = display