如何将Ansible的with_item与变量一起使用? [英] How to use Ansible's with_item with a variable?
问题描述
我正在尝试转换Ansible Playbook中列表项的某些字段.这是最简单的复制路径,跳过了转换.结果应与users
变量相同.
I'm trying to transform some fields of the items of a list in an Ansible Playbook. Here is the simplest reproduction path, skipping the transformation. The result should be identical to the users
variable.
---
# Run with:
# ansible-playbook -i "localhost," loop3.yml
- hosts: localhost
connection: local
gather_facts: false
vars:
users:
- name: paul
uid: 1
- name: pete
uid: 2
tasks:
- set_fact:
args:
useritem:
name: '{{ item.name }}'
uid: '{{ item.uid }}'
with_items:
- users
register: sf_result
- debug: var=sf_result
- set_fact:
userslist: "{{ sf_result.results | map(attribute='ansible_facts.useritem') | list }}"
- debug: var=userslist
我收到此错误:
TASK [set_fact useritem={u'name': u'{{ item.name }}', u'uid': u'{{ item.uid }}'}] ***
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! 'unicode object' has no attribute 'name'"}
有有几个 示例非常接近我需要的示例,但是我找不到使用set_fact
以及with_items
和项目作为地图的有效示例.
There are several examples very close to what I needbut I could find no working example using set_fact
along with with_items
and items as a map.
我尝试使用不同的错误消息尝试Ansible 1.9.2、1.9.4和2.0.0-0.6.rc1,但没有成功. Ansible 2应该允许跳过对set_fact
的第二个调用,但是在到达那里之前会发生错误.
I've tried Ansible 1.9.2, 1.9.4, and 2.0.0-0.6.rc1 with different error messages but no more success. Ansible 2 should allow skipping the second call to set_fact
but the error happens before getting there.
推荐答案
我想我确实读过某个地方with_items
接受一个简单的变量名,但事实并非如此.
I thought I did read somewhere that with_items
accepts a bare variable name, but it's not the case.
该程序使用以下命令按预期运行:
The program runs as expected using:
with_items: "{{ users }}"
这篇关于如何将Ansible的with_item与变量一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!