如何为每个异步循环项打印消息?

huangapple go评论70阅读模式
英文:

How to print a message for each async loop item?

问题

以下是剧本的一部分。如何在每次循环项运行命令后打印一条消息?类似这样:

image.pyc 正在为 item1 运行
image.pyc 正在为 item2 运行

等等

- name: Image Server(s)
  command: python3 image.pyc {{ item }} "{{ systemVersion }}"
  register: async_out
  async: 7200
  poll: 0
  with_items: "{{ hostinfo_input.hosts }}"

我想要的只是这个:

任务 [Image Server(s)] ************* 
changed: [localhost] => (item=8m007) 
changed: [localhost] => (item=8m013) 
为 8m007 运行 image.pyc
为 8m013 运行 image.pyc
英文:

Below is a snippet from a playbook. How can I print a message after the command is run for each loop item? Something like

image.pyc is now running for item1
image.pyc is now running for item2

and so on

- name: Image Server(s)
  command: python3 image.pyc {{ item }} "{{ systemVersion }}"
  register: async_out
  async: 7200
  poll: 0
  with_items: "{{ hostinfo_input.hosts }}"

What I want is simply this

TASK [Image Server(s)] ************* 
changed: [localhost] => (item=8m007) 
changed: [localhost] => (item=8m013) 
Running image.pyc for 8m007 
Running image.pyc for 8m013

答案1

得分: 2

以下是代码部分的中文翻译:

- name: 图像服务器
  ansible.builtin.command: python3 image.pyc {{ item }} "{{ systemVersion }}"
  register: async_out
  async: 7200
  poll: 0
  with_items: "{{ hostinfo_input.hosts }}"

- name: 等待命令完成
  ansible.builtin.async_status:
    jid: "{{ item.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 720
  delay: 10
  loop: "{{ async_out.results }}"

- name: 显示异步命令输出
  ansible.builtin.debug:
    msg: "{{ item.stdout }}"
  loop: "{{ async_out.results }}"

- name: 好客人总是在完成后清理
  ansible.builtin.async_status:
    jid: "{{ item.ansible_job_id }}"
    mode: cleanup
  loop: "{{ async_out.results }}"

参考链接:

英文:

In a nutshell, extremely simple, untested, to be adapted to your use case specifically in terms of error/output control:

    - name: Image Server(s)
      ansible.builtin.command: python3 image.pyc {{ item }} "{{ systemVersion }}"
      register: async_out
      async: 7200
      poll: 0
      with_items: "{{ hostinfo_input.hosts }}"

    - name: Wait for commands to finish
      ansible.builtin.async_status:
        jid: "{{ item.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 720
      delay: 10
      loop: "{{ async_out.results }}"

    - name: Show async command output
      ansible.builtin.debug:
        msg: "{{ item.stdout }}"
      loop: "{{ async_out.results }}"

   - name: Good guests always clean up after themselves
     ansible.builtin.async_status:
       jid: "{{ item.ansible_job_id }}"
       mode: cleanup
     loop: "{{ async_out.results }}"

References:

答案2

得分: 0

查看 Ansible 文档中关于 debug 模块的信息:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html

例如,可以像这样使用它:

# playbook.yaml
---
- hosts: localhost
  connection: local
  vars:
    ip_addresses:
      - 10.0.0.10
      - 10.0.0.11
  tasks:
    - name: Task
      include_tasks: task.yaml
      loop: "{{ ip_addresses }}"
# task.yaml
---
- name: Command
  ansible.builtin.shell: "echo {{ item }}"
  register: output

- name: Message
  ansible.builtin.debug:
    msg: |
      - ip_address={{ item }}
      - ouput={{ output }}      

不幸的是,这在使用 async 时无法工作。请参考 https://github.com/ansible/ansible/issues/22716

英文:

Have a look at Ansible documentation for the debug module: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html

E.g.use it like this:

# playbook.yaml
---
- hosts: localhost
  connection: local
  vars:
    ip_addresses:
      - 10.0.0.10
      - 10.0.0.11
  tasks:
    - name: Task
      include_tasks: task.yaml
      loop: "{{ ip_addresses }}"
# task.yaml
---
- name: Command
  ansible.builtin.shell: "echo {{ item }}"
  register: output

- name: Message
  ansible.builtin.debug:
    msg: |
      - ip_address={{ item }}
      - ouput={{ output }}      

Unfortunately this does not work with async. See https://github.com/ansible/ansible/issues/22716.

答案3

得分: 0

您目前描述的要求如下:

在每次循环项目运行命令后,如何打印消息?

可以通过loop输出与label来实现。

这是为了使控制台输出更易读...

例如:

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Image Server(s)
    command: "python3 image.pyc {{ item }} {{ systemVersion }}"
    register: async_out
    async: 7200
    poll: 0
    loop: "{{ ansible_play_hosts }}"
    loop_control:
      label: "Running image.pyc for {{ item }}"

结果输出为:

TASK [Image Server(s)] *******************************************************
changed: [test1.example.com] => (item=Running image.pyc for test1.example.com)
changed: [test2.example.com] => (item=Running image.pyc for test2.example.com)
...

给出的示例仅在命令发送到远程节点并在那里启动后提供消息。这简单地因为没有定义谁应该提供(打印)消息,loop通过labelasyncscript都可以实现。请参考@Zeitounator的评论。

英文:

Your requirement as it is described currently

> How can I print a message after the command is run for each loop item?

could just be fulfilled by loop output with label

> This is for making console output more readable ...

in example

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Image Server(s)
    command: "python3 image.pyc {{ item }} {{ systemVersion }}"
    register: async_out
    async: 7200
    poll: 0
    loop: "{{ ansible_play_hosts }}"
    loop_control:
      label: "Running image.pyc for {{ item }}"

resulting into an output of

TASK [Image Server(s)] *******************************************************
changed: [test1.example.com] => (item=Running image.pyc for test1.example.com)
changed: [test2.example.com] => (item=Running image.pyc for test2.example.com)
...

The given example just provides the message after the command is sent over to the Remote Node and was started there. That is simply working because it is not defined who should provide (print) the message, the loop via label, async or the script. See also the comment of @Zeitounator ...

huangapple
  • 本文由 发表于 2023年2月6日 16:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定