如何将 Ansible 的 with_item 与变量一起使用? [英] How to use Ansible's with_item with a variable?

查看:29
本文介绍了如何将 Ansible 的 with_item 与变量一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换 Ansible Playbook 中列表项的某些字段.这里是最简单的复制路径,跳过变换.结果应该与 users 变量相同.

---# 运行:# ansible-playbook -i "localhost," loop3.yml- 主机:本地主机连接:本地收集事实:假变量:用户:- 姓名:保罗用户名:1- 姓名:皮特用户名:2任务:- 设置事实:参数:用户项:名称:'{{ item.name }}'uid: '{{ item.uid }}'with_items:- 用户寄存器:sf_result- 调试:var=sf_result- 设置事实:用户列表:{{ sf_result.results | map(attribute='ansible_facts.useritem') | list }}"- 调试:var=userslist

我收到此错误:

TASK [set_fact useritem={u'name': u'{{ item.name }}', u'uid': u'{{ item.uid }}'}] ***致命:[本地主机]:失败!=>{"failed": true, "msg": "ERROR!'unicode object' 没有属性 'name'"}

几个 examples 非常接近我需要的但是我找不到使用 set_fact 以及 with_items 和 items 作为地图的工作示例.>

我已经尝试了 Ansible 1.9.2、1.9.4 和 2.0.0-0.6.rc1,但没有成功.Ansible 2 应该允许跳过对 set_fact 的第二次调用,但错误发生在到达那里之前.

解决方案

我以为我确实在某处读到 with_items 接受一个裸变量名,但事实并非如此.

程序按预期运行:

with_items: "{{ 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

I get this error:

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'"}

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.

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.

解决方案

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屋!

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