Ansible:如何调试多个任务、命令或迭代? [英] Ansible: How to debug mutiple tasks, commands or iterations?

查看:25
本文介绍了Ansible:如何调试多个任务、命令或迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 YAML 剧本中,我需要通过数组中的每个元素运行多个命令.我正在使用 with_items 进行迭代,但是当我尝试将 debug 混合到任务中时出现语法错误.

In a YAML playbook, I need to run multiple commands through each element in an array. I am using with_items to iterate, but I am getting syntax errors when I try to blend debug into the tasks.

当我删除 debug 模块时一切正常,但我看不到发生了什么.示例如下.

Everything works when I remove the debug module, but than I can't see whats happening. Example provided below.

产生语法错误:

- name: Iterate through array and display results  
  shell: "run command 1 on {{ item }}"  
  register: command1  
  debug:  
    msg: "Command 1 || {{ command1.stdout_lines}}"  
  shell: "run command 2 on {{ item }}"  
  register: command2  
  debug:  
    msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"

删除调试后一切似乎都正常,但我需要查看每个命令产生的内容,因为它包含重要信息.

Everything seems to be working fine when I remove the debugs, but I need to see what each command produces because it has important info.

有没有办法打印/调试结果,同时将其保留在 with_items 迭代中?

Is there some way to print/debug the results while keeping it inside the with_items iterations?

我还尝试了一些其他方法,但它们也给了我一个语法错误

I also tried a few more things but they also gave me a syntax error

也试过 1:

- name: Iterate through array and display results  
- shell: "run command 1 on {{ item }}"  
  register: command1  
- debug:  
    msg: "Command 1 || {{ command1.stdout_lines}}"  
- shell: "run command 2 on {{ item }}"  
  register: command2  
- debug:  
    msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"

也试过 2:

- name: Iterate through array and display results  
  - shell: "run command 1 on {{ item }}"  
    register: command1  
  - debug:  
      msg: "Command 1 || {{ command1.stdout_lines}}"  
  - shell: "run command 2 on {{ item }}"  
    register: command2  
  - debug:  
      msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"

推荐答案

我能够解决这个问题,Ansible 不允许通过多个任务进行迭代.我不得不将任务放在单独的 yml 剧本中,并使用 include_tasks: 来调用剧本.

I was able to solve the issue, Ansible does not allow iterations through multiple tasks. I had to put the tasks in a separate yml playbook and use include_tasks: to call the playbook.

这篇关于Ansible:如何调试多个任务、命令或迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆