Ansible 调试在循环时提供所有元数据 [英] Ansible debug is giving all meta data when going over loop

查看:33
本文介绍了Ansible 调试在循环时提供所有元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从前一个循环的结果中获取调试消息,但我无法从中获得我想要的信息.Ansible 不断给我整个结果,而不是我要求的那一行.

I'm trying to get a debug message from the results of a previous loop, but I can't get just the bit that I want from it. Ansible keeps giving me the entire result instead just the line I'm asking for.

这是我正在使用的 2 个模块:

Here are the 2 modules I'm using:

- name: Make the curl call
  uri:
    url: https://www.theurl.com
    method: GET
    return_content: yes
    headers:
      XXXX: "xxxxxxxxxx"
  loop: "{{ simplelist }}"
  register: this



 - name: just testing
    debug:
      msg: "I just want to see: {{ item.json.section.test }}"
    loop: "{{ this.results}}"

正如你从 msg 中看到的,我只是想输出那个特定的值,但 Ansible 给我的是:

As you can see from the msg, I'm just trying to output that specific value, but what Ansible gives me is:

{
"ansible_loop_var": "item",
"_ansible_no_log": false,
"item": {
    "content_length": "",
    "cookies": {},
    "via": "1.1 varnish",
    "connection": "",
    "vary": "Accept-Encoding",
    "x_timer": "",
    "access_control_allow_methods": "OPTIONS, HEAD, GET, PATCH, DELETE",
    "x_cache_hits": "0",
    "failed": false,
    "access_control_allow_credentials": "true",
    "content": blah blah blah,
    "json": { the json },  
    "changed": false,
    "msg": "I just want to see: False",

所以它正在设置消息,正如您从最后一行看到的那样,并且它正在获取正确的值,但它没有输出该消息.我怎样才能得到要输出的消息?我测试过,我知道我可以得到这个值,因为 msg 有 False,我测试了失败/何时使用该值.

So it is setting the message, as you can see from the last line, and it is getting the correct value, but it's not outputting that message. How can I get just the message to be output? I tested and I know that I can get the value because the msg has False and I tested with doing a fail/when with that value.

推荐答案

您所看到的看起来像是使用 -v[vv] 选项运行的 ansible-playbook 的详细输出.您可以删除该选项以减少详细程度.

What you are seeing looks like a verbose output of ansible-playbook running with the -v[vv] option. You can drop that option to decrease verbosity.

与此同时,即使在非详细模式下,以及您使用的任何模块,当遍历循环时,ansible 会为每次迭代输出一个 label,大致如下所示(注意 labelcode>(item=....) 屏幕的一部分).

Meanwhile, even in non-verbose mode, and whatever module your are using, when going over a loop, ansible outputs a label for each iteration, roughly looking like the following (watch for the (item=....) part of the screen).

TASK [test] *******************************************************************************
ok: [localhost] => (item={'a': 1, 'b': 2}) => {
    "msg": "This is the value of a: 1"
}
ok: [localhost] => (item={'a': 3, 'b': 4}) => {
    "msg": "This is the value of a: 3"
}

默认情况下,标签是您当前正在循环的完整项目.但是您可以在 loop_control 参数中更改此标签,这对于复杂的数据结构来说可能有点过于冗长.如果你真的想要一个空标签,你可以使用下面的例子.但是你仍然会得到 ok: [server1] =>(item=) => 附加到每个迭代输出.

By default, the label is a the full item your are currently looping over. But you can change this label in the loop_control parameter which can be a little too verbose for complex data structures. If you really want an empty label your can use the following example. But you will still get ok: [server1] => (item=) => prepended to each iteration output.

 - name: just testing
   debug:
     msg: "I just want to see: {{ item.json.section.test }}"
   loop: "{{ this.results }}"
   loop_control:
     label: ""

有关更多信息,请参阅限制带有 label

For more info see limiting loop output with label

这篇关于Ansible 调试在循环时提供所有元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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