Ansible:在字典,列表和列表上进行迭代

时间:2019-07-26 10:10:01

标签: python ansible jinja2

我需要知道为什么这段代码失败和/或如何实现,因为我无法做到

我运行了这块ansible,它使用每个主机的寄存器列表,将检查获得path值的证书状态。 但是看来我没有以正确的方式访问字典。

register_variable

获得的数据结构
ok: [node01] => {
  "service_certificate_found": {
      "changed": false,
      "msg": "All items completed",
      "results": [
          {
              "ansible_loop_var": "item",
              "changed": false,
              "examined": 12,
              "failed": false,
              "files": [
                  {
                      "atime": 1552906907.033556,
                      "ctime": 1552906907.033556,
                      "dev": 66305,
                      "gid": 999,
                      "inode": 302256,
                      "isblk": false,
                      "ischr": false,
                      "isdir": false,
                      "isfifo": false,
                      "isgid": false,
                      "islnk": false,
                      "isreg": true,
                      "issock": false,
                      "isuid": false,
                      "mode": "0644",
                      "mtime": 1552906906.6615531,
                      "nlink": 1,
                      "path": "/etc/keys/collectorCA.crt",
                      "rgrp": true,
                      "roth": true,
                      "rusr": true,
                      "size": 2061,
                      "uid": 1012,
                      "wgrp": false,
                      "woth": false,
                      "wusr": true,
                      "xgrp": false,
                      "xoth": false,
                      "xusr": false
                  },
                  {
                      "atime": 1552906902.7015238,
                      "ctime": 1552906902.7015238,
                      "dev": 66305,
                      "gid": 999,
                      "inode": 302254,
                      "isblk": false,
                      "ischr": false,
                      "isdir": false,
                      "isfifo": false,
                      "isgid": false,
                      "islnk": false,
                      "isreg": true,
                      "issock": false,
                      "isuid": false,
                      "mode": "0644",
                      "mtime": 1552906902.3415213,
                      "nlink": 1,
                      "path": "/etc/keys/InterCA.crt",
                      "rgrp": true,
                      "roth": true,
                      "rusr": true,
                      "size": 1554,
                      "uid": 1012,
                      "wgrp": false,
                      "woth": false,
                      "wusr": true,
                      "xgrp": false,
                      "xoth": false,
                      "xusr": false
                  }
              ]
          }
        ]
    }
}

Ansible摘录

- name: Check certificate is valid and on the right place
  block:
  - name: First find the certificates
    find: 
      paths: "{{ item }}"
      recurse: yes
    register: service_certificate_found
    with_items: 
     - "{{ service_certificate_paths }}"


  - name: Now check everyone of them
    openssl_certificate:
      path: "{{ item.item.path }}"
      provider: assertonly
      has_expired: false
    with_items: "{{ service_certificate_found.results }}"

Ansible错误

TASK [pb.servicetest : Now check everyone of them] ********************************************************************************************************************************************************
Friday 26 July 2019  11:58:56 +0200 (0:00:00.055)       0:00:03.856 ***********
fatal: [node1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'path'\n\nThe error appears to be in '/Users/me/repos/ansible-servicetest/tasks/main.yml': line 42, column 15, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  - debug: var=service_certificate_found\n              ^ here\n\nThere appears to be both 'k=v' shorthand syntax and YAML in this task. Only one syntax may be used.\n"}

想法?

1 个答案:

答案 0 :(得分:0)

是的,它只是列表项的一小段,现已修复。 但不管怎么说。我修好了自己:

这可以解决问题,在with_items循环中指定从哪里开始循环

- name: Now check everyone of them
    openssl_certificate:
      path: "{{ item.path }}"
      provider: assertonly
      has_expired: false
    with_items: "{{ service_certificate_found.results[0]['files'] }}"