使用 with_items 和 register 进行迭代 [英] iteration using with_items and register
问题描述
寻求帮助解决我几个小时以来一直在努力解决的问题.我想迭代一个列表,运行一个命令,为每个命令注册输出,然后在每个唯一的寄存器 {{ someregister }}.stdout
Looking for help with a problem I've been struggling with for a few hours. I want to iterate over a list, run a command, register the output for each command and then iterate with debug over each unique registers {{ someregister }}.stdout
例如下面的代码会吐出"msg":"1"和"msg":"2"
For example, the following code will spit out "msg": "1" and "msg": "2"
---
- hosts: localhost
gather_facts: false
vars:
numbers:
- name: "first"
int: "1"
- name: "second"
int: "2"
tasks:
- name: Register output
command: "/bin/echo {{ item.int }}"
register: result
with_items: "{{ numbers }}"
- debug: msg={{ item.stdout }}
with_items: "{{ result.results }}"
但是,如果我尝试在使用 with_list 命名的寄存器变量中捕获命令的输出,我将无法访问列表或其中的元素.例如,将代码稍微更改为:
If however, I try and capture the output of a command in a register variable that is named using with_list, I am having trouble accessing the list or the elements within it. For example, altering the code slightly to:
---
- hosts: localhost
gather_facts: false
vars:
numbers:
- name: "first"
int: "1"
- name: "second"
int: "2"
tasks:
- name: Register output
command: "/bin/echo {{ item.int }}"
register: "{{ item.name }}"
with_items: "{{ numbers }}"
- debug: var={{ item.name.stdout }}
with_items: "{{ numbers }}"
给我:
TASK [debug]
> ******************************************************************* fatal: [localhost]: FAILED! => {"failed": true, "msg": "'unicode
> object' has no attribute 'stdout'"}
是否不能将寄存器动态命名为命令的输出,然后可以在剧中稍后调用?我希望命令的每次迭代及其后续的 register 名称都可以唯一访问,例如,鉴于最后一个示例,我希望有注册的变量称为first"和第二"但没有.
Is it not possible to dynamically name the register the output of a command which can then be called later on in the play? I would like each iteration of the command and its subsequent register name to be accessed uniquely, e.g, given the last example I would expect there to be variables registered called "first" and "second" but there aren't.
从调试节中删除 with_items,并使用 first.stdout 显式定义 var 或消息返回未定义".
Taking away the with_items from the debug stanza, and just explicitly defining the var or message using first.stdout returns "undefined".
Centos 7_2 上的 Ansible 版本为 2.0.2.0.
Ansible version is 2.0.2.0 on Centos 7_2.
提前致谢.
推荐答案
OK 所以我找到了一个 stackoverflow 上的帖子 帮助我更好地了解这里发生了什么以及如何访问 result.results 中的元素.
OK so I found a post on stackoverflow that helped me better understand what is going on here and how to access the elements in result.results.
我最终得到的代码是:
---
- hosts: localhost
gather_facts: false
vars:
numbers:
- name: "first"
int: "1"
- name: "second"
int: "2"
tasks:
- name: Register output
command: "/bin/echo {{ item.int }}"
register: echo_out
with_items: "{{ numbers }}"
- debug: msg="item.item={{item.item.name}}, item.stdout={{item.stdout}}"
with_items: "{{ echo_out.results }}"
这给了我想要的结果:
"msg": "item.item=first, item.stdout=1"
"msg": "item.item=second, item.stdout=2"
这篇关于使用 with_items 和 register 进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!