Ansible 从结果中检索多个数组值

标签 ansible

我希望能够遍历所有名称值,但我不确定如何使用 Ansible。变量域是一个列表,使用寄存器。

- name: find *.ccfg files in domain(s)
  find:
    paths: "/tmp/opt/{{ item }}/ccfg"
    patterns: "*.ccfg"
    recurse: yes
    excludes: "Admin.ccfg"
  with_items: "{{ domain }}"
  register: files
  when: ('local' in group_names)

- debug:
    msg: "{{ files.results }}"

路径 每个数组中的值可以是 1 到 20 之间的任何值。数组中的每个索引都有多个值。某些数组可能没有任何值

标准输出:
ok: [127.0.0.1] => {
    "msg": [
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_label": "CIE",
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": false,
            "examined": 3,
            "failed": false,
            "files": [
                {
                    "atime": 1541632866.4095802,
                    "ctime": 1541632866.4095802,
                    "dev": 64768,
                    "gid": 0,
                    "gr_name": "root",
                    "inode": 52174935,
                    "isblk": false,
                    "ischr": false,
                    "isdir": false,
                    "isfifo": false,
                    "isgid": false,
                    "islnk": false,
                    "isreg": true,
                    "issock": false,
                    "isuid": false,
                    "mode": "0644",
                    "mtime": 1541632866.4095802,
                    "nlink": 1,
                    "path": "/tmp/opt/CIE/ccfg/cie.ccfg",
                    "pw_name": "root",
                    "rgrp": true,
                    "roth": true,
                    "rusr": true,
                    "size": 0,
                    "uid": 0,
                    "wgrp": false,
                    "woth": false,
                    "wusr": true,
                    "xgrp": false,
                    "xoth": false,
                    "xusr": false
                }
            ],

最佳答案

看看 json_query 过滤器:

    - debug:
        msg: "{{ item }}"
      with_items: "{{ files | json_query('results[*].files[*].path') }}"

官方文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter

无耻的插头有更多的例子:https://parko.id.au/2018/08/16/complex-data-structures-and-the-ansible-json_query-filter

关于Ansible 从结果中检索多个数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53199994/

相关文章:

docker - 从 Ansible 使用预设 IP 运行 Docker 容器

Ansible - 在收集事实之前采取的行动

python - 使用ansible检查python模块版本

ansible - 如何将ansible变量传递给vagrant

python - Ansible python 版本不会改变

json - Ansible 查找 json 文件中的一行

安塞 bool 2.7 : How to list files from unarchive

amazon-ec2 - 使用 ansible 配置 EC2 自动缩放策略和警报

Ansible apt 模块显示 "item changed",但我认为它没有

循环时,Ansible 仅在包含的 playbook 中运行任务一次