Ansible 2.2.1.0 with_subelements:错误!子元素查找需要一本字典,得到了' incor'

时间:2017-02-05 09:46:57

标签: ansible ansible-2.x

我有以下问题:
当我使用变量with_subelements时,我有一个错误:错误!子元素查找需要一本字典,得到了 '
我的主人档案:

[incor]
127.0.0.1

dir group_vars

group_vars
  --- all
  --- incor
           --- main.yml

group_vars / INCOR / main.yml

---
incor:
  - pfileurl: "http://domain/file1.jar"
    pfilename: "user1.jar"
    pfiletype: "lib"
    pfileset:
      - {type: "type1", reg: "reg1" }
      - {type: "type2", reg: "reg2" }

  - pfileurl: "http://domain/file2.jar"
    pfilename: "user2.jar"
    pfiletype: "lib"
    pfileset:
      - {type: "type1", reg: "reg1" }
      - {type: "type2", reg: "reg2" }

角色/ p_get_file /任务/ main.yml

- name: Download files
  get_url:
    url="{{ item.0.pfileurl }}"
    dest="{{ path }}{{ item.1.type }}/{{ item.1.reg }}/{{ item.0.pfilename }}"
    force="yes"
  with_subelements:
    - "{{ projectname }}"
    - "pfileset"
  when: "'item.0.pfiletype is defined' and item.0.pfiletype == 'lib'"
  delegate_to: "127.0.0.1"

myplaybook.yml

- hosts: "{{ hosts }}"
  roles:
    - { role: p_get_file }

启动剧本

ansible-playbook myplaybook.yml --extra-vars "hosts='incor' projectname='incor'"

Ansible 1.9.4中的变量{{projectname}}出现问题,但是 在Ansible 2.2.1.0中不起作用:错误!子元素查找需要一个字典,得到' incor'

2 个答案:

答案 0 :(得分:1)

自Ansible 2.2以来,您无法使用裸变量。

在1.9中它起作用,因为这种语法是可以接受的:

  with_subelements:
    - incor
    - pfileset

2.2+ incor被视为字符串。

解决此问题的最简单方法是将incor dict包装到某些顶级词典中,如projects

---
projects:
  incor:
    - pfileurl: "http://domain/file1.jar"
      pfilename: "user1.jar"
      pfiletype: "lib"
      pfileset:
        - {type: "type1", reg: "reg1" }
        - {type: "type2", reg: "reg2" }
    ...

然后像这样访问它:

  with_subelements:
    - "{{ projects[projectname] }}"
    - pfileset

或者您可以jump trough some hoops访问根级变量。

答案 1 :(得分:0)

我尝试使用jump trough some hoops {{ vars[projectname] }}但变量incor原始

incor: 
   - pfileurl: "http://domain/{{ projectname }}/file1.jar" 
     pfilename: "user1.jar" 
     pfiletype: "lib" 
     pfileset: 
       - {type: "type1", reg: "reg1" } 
       - {type: "type2", reg: "reg2" }  

{{ projectname }}中的变量pfileurl没有对象

相关问题