【问题标题】:Ansible 2.2.1.0 with_subelements: ERROR! subelements lookup expects a dictionary, got 'incor'Ansible 2.2.1.0 with_subelements:错误!子元素查找需要字典,得到“incor”
【发布时间】:2017-02-05 09:46:56
【问题描述】:

我有以下问题:
当我使用变量 with_subelements 时出现错误:错误!子元素查找需要字典,得到 'incor'
我的主机文件:

[incor]
127.0.0.1

目录组变量

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

roles/p_get_file/tasks/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'

【问题讨论】:

    标签: ansible ansible-2.x


    【解决方案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 访问根级变量。

    【讨论】:

    • 但是现在 Ansible 2.2 查看所有目录中的所有变量不仅 global_vars/incor/main.yml ` global_vars/ --- incor/ --- main.yml --outcor/ --- main. yml ` 只记得最后一个
    • 现在我不能在 global_vars/incor/main.yml 和 global_vars/outcor/main.yml 中使用变量 projects
    • 您可能想要更改hash_behaviour 设置以合并您的项目字典(但一般不建议这样做)或修改您的变量布局。
    • 我尝试使用{{ 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中没有对象
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多