如何显示长期运行的Ansible任务的进度?

问题描述 投票:26回答:4

我有一些Ansible任务执行不幸的长操作 - 比如运行S3文件夹的同步操作。并不总是很清楚它们是否正在进展,或者只是卡住了(或者ssh连接已经死亡),所以显示某种进度输出会很好。如果直接显示命令的stdout / stderr,我会看到,但Ansible会捕获输出。

管道输出回is a difficult problem for Ansible to solve in its current form。但是,我有什么可以使用的Ansible技巧来提供事情仍在发展的某种迹象吗?

目前的票是https://github.com/ansible/ansible/issues/4870

ansible ansible-2.x
4个回答
5
投票

Ansible自此实施了以下内容:

---
# Requires ansible 1.8+
- name: 'YUM - async task'
  yum:
    name: docker-io
    state: installed
  async: 1000
  poll: 0
  register: yum_sleeper

- name: 'YUM - check on async task'
  async_status:
    jid: "{{ yum_sleeper.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 30

有关详细信息,请参阅有关该主题的official documentation(确保您选择的Ansible版本)。


19
投票

我今天在OSX上遇到了这个问题,在那里我运行了一个docker shell命令,它花了很长时间来构建,并且在构建时没有输出。不明白命令是否挂起或者进展缓慢是非常令人沮丧的。

我决定将shell命令的输出(和错误)传递给一个端口,然后可以通过另一个终端中的netcat监听该端口。

myplaybook.yml

- name: run some long-running task and pipe to a port
  shell: myLongRunningApp > /dev/tcp/localhost/4000 2>&1

在一个单独的终端窗口中:

$ nc -lk 4000
Output from my
long
running
app will appear here

请注意,我将错误输出传递给同一个端口;我可以轻松地管道到不同的端口。

此外,我最终设置了一个名为nc_port的变量,它允许在端口正在使用时更改端口。然后,ansible任务看起来像:

  shell: myLongRunningApp > /dev/tcp/localhost/{{nc_port}} 2>&1

请注意,命令myLongRunningApp正在localhost上执行(即库存中的主机设置),这就是我用nc监听localhost的原因。


10
投票

你可以做几件事,但正如你正确指出的那样,Ansible目前的形式并没有真正提供一个好的解决方案。

官方解决方案:

一个想法是将任务标记为异步并轮询它。显然,这只适用于能够以这种方式运行而不会导致剧本中其他地方失败的情况。异步文档是here,这是一个从它们中取出的例子:

- hosts: all
  remote_user: root
  tasks:
  - name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
    command: /bin/sleep 15
    async: 45
    poll: 5

这至少可以让你“ping”知道任务没有挂起。

唯一正式认可的方法是Ansible Tower,它有任务进度条但不是免费的。

Hacky-ish解决方案:

除此之外,你几乎不得不自己动手。您可以使用脚本定期调用AWS CLI并计算存储桶中的项目数来轻松监控同步S3存储桶的具体示例,但这不是一个好的通用解决方案。

我能想象的唯一有效的方法是从你的一个节点观看传入的ssh会话。

为此,您可以将该计算机上的ansible用户配置为通过屏幕连接并主动观看。或者也许在该用户的sudoers条目中使用log_output选项,允许您拖尾文件。 log_output的详细信息可以在sudoers man page上找到


4
投票

如果您使用的是Linux,则可以使用systemd-run创建瞬态单元并使用journalctl检查输出,如:

sudo systemd-run --unit foo \                                      
     bash -c 'for i in {0..10}; do 
                   echo "$((i * 10))%"; sleep 1;
              done;
              echo "Complete"'

在另一场会议上

sudo journalctl -xf --unit foo

它会输出如下内容:

Apr 07 02:10:34 localhost.localdomain systemd[1]: Started /bin/bash -c for i in {0..10}; do echo "$((i * 10))%"; sleep 1; done; echo "Complete".
-- Subject: Unit foo.service has finished start-up
-- Defined-By: systemd
-- Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit foo.service has finished starting up.
-- 
-- The start-up result is done.
Apr 07 02:10:34 localhost.localdomain bash[10083]: 0%
Apr 07 02:10:35 localhost.localdomain bash[10083]: 10%
Apr 07 02:10:36 localhost.localdomain bash[10083]: 20%
Apr 07 02:10:37 localhost.localdomain bash[10083]: 30%
Apr 07 02:10:38 localhost.localdomain bash[10083]: 40%
Apr 07 02:10:39 localhost.localdomain bash[10083]: 50%
Apr 07 02:10:40 localhost.localdomain bash[10083]: 60%
Apr 07 02:10:41 localhost.localdomain bash[10083]: 70%
Apr 07 02:10:42 localhost.localdomain bash[10083]: 80%
Apr 07 02:10:43 localhost.localdomain bash[10083]: 90%
Apr 07 02:10:44 localhost.localdomain bash[10083]: 100%
Apr 07 02:10:45 localhost.localdomain bash[10083]: Complete
© www.soinside.com 2019 - 2024. All rights reserved.