Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible retrieve multiple array values from results

Tags:

ansible

I'd like to be able to iterate over all of the name values, but I'm not sure how to do so with Ansible. The variable domain is a list, and register is used.

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

The path value in each array could be anywhere from 1 to 20. Each index in the array has multiple values. Some arrays may not have any values

Standard Output:

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
                }
            ],
like image 717
Victor Onyenze Avatar asked Oct 19 '25 13:10

Victor Onyenze


1 Answers

Take a look at the json_query filter:

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

Official doco: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter

Shameless plug with more examples: https://parko.id.au/2018/08/16/complex-data-structures-and-the-ansible-json_query-filter

like image 154
Matt P Avatar answered Oct 21 '25 05:10

Matt P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!