Ansible,列表对象没有属性

Ansible, list object has no attribute

我用的是Ansible 2.3.0.0,我在Ansible 2.4.0.0上测试过,结果一样。我的问题很简单,就是看不出问题

我在 Ansible 中定义了一个对象列表如下:

  vars:
     password_text_to_encrypt:
          - { line: "{{truststore_pass }}" , regexp: '${TRUSTSTORE_PASS}*'}
          - { line: "{{ keystore_pass }}" , regexp: '${KEYSTORE_PASS}*'}
          - { line: "{{ gp_pass }}" , regexp: '${GP_PASS}*'}
          - { line: "{{ datasource_password }}" , regexp: '${DATASOURCE_PASS}*'}
          - { line: "{{ server_password }}" , regexp: '${SERVER_PASSWORD}*'}
          - { line: "{{ sftp_password }}" , regexp: '${SFTP_PASSWORD}*'}
          - { line: "{{ db_userpassword }}" , regexp: '${DB_PASSWORD}*'}
     roles:
       - basic_role

我的Basic_role只是打印items,想获取每一行的内容:

---

- name: "print password"
  debug: 
    msg: "The content of the line is: {{ item.line}}"
  with_nested:
    - "{{password_text_to_encrypt}}"

但是我得到的结果是:

FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'line'\n\nThe error appears to have been in.....

如果我将 item.line 更改为 item,它可以工作,但会打印:

ok: [localhost] => (item=[u'regexp', u'line']) => {
    "item": [
        "regexp",
        "line"
    ],
    "msg": "The content of the line is: [u'regexp', u'line']"
}
.
.
.

总而言之,Ansible 不考虑 o regexp 行的内容。我一直在做测试,用于初始化行和正则表达式的变量不为空。

使用 with_items 代替 with_nested

我认为您需要 loops and includes,因为您得到的是预期的扁平化列表(根据文档)。