Ansible正试图在我不想要的嵌套元素上应用条件

时间:2017-04-27 12:49:27

标签: ansible

给出vars/all.yml

中的项目
websites:
    -
        dest: test_project
        db_password: 'xxxx'
        delete: false

在一个条件下调用backend.yml的main.yml

- debug: msg={{ websites | selectattr("dest", "equalto", project_name) | list }}
  register: website

ok: [xxx.com] => {
    "changed": false, 
    "msg": [
        {
            "db_password": "xxxx", 
            "delete": false, 
            "dest": "test_project", 

        }
    ]
}

- include: backend.yml
  when: not website.msg.delete|bool
  #static: no #tried this too, but has no effect

然后,backend.yml包含:

...
- name: Grant ACL for write inside "wp-content" (default)
  acl:
    name: "{{ ansible_www_home }}/{{ item[0].dest }}/wp-content/"
    entity: "{{ item[1] }}"
    etype: group
    permissions: rwx
    state: present
  with_nested:
    - "{{ website.msg }}"
    - ['www-data', 'test']
...

错误是

The conditional check 'not item.delete|bool' failed. 
The error was: error while evaluating conditional (not item.delete|bool): 'list object' has no attribute 'delete'

The error appears to have been in 'backend.yml': line 58, column 3, but may be elsewhere in the file depending on the exact syntax problem

似乎Ansible正在尝试在第二个嵌套数组delete上查找['www-data', 'test']属性,这当然不存在。

我不明白为什么ansible试图在这个级别上应用我的include条件。如果item.delete不为真,我只想要ansible包含文件,而不是试图在backend.yml

的哪一步调用这个条件

你对如何解决这个问题有所了解吗?

PS:我尝试将static: no添加到main.yml上的条件,但它没有效果。

1 个答案:

答案 0 :(得分:0)

好的,确定了多个愚蠢的错误,解决方案是:

- include: backend.yml
  when: not website.msg.0.delete|bool
  static: no