Score:0

ansible print folder path from register variable

sv flag

I'm trying to print just the path of a register var that has multiple values, however I can only print a specific value. how can I print multiple values?

---
  - name: find directories
    find:
      paths: "{{ item }}"
      recurse: yes
      file_type: directory
      use_regex: yes
      patterns:
        - '.*log.*'
        - '.*sbin.*'
    with_items:
      - /var
      - /usr
    register: folders_info

  - name: print folders found
    debug:
      msg: "{{ item }}"
    with_items: "{{ folders_info.results[0].files[0].path }}"

This outputs:

    TASK [filebeat_confirm : find directories] ***********************************************************************************************************************************************
ok: [10.0.1.243] => (item=/var)
ok: [10.0.1.243] => (item=/usr)

TASK [filebeat_confirm : print folders found] ********************************************************************************************************************************************
ok: [10.0.1.243] => (item=/var/log) => {
    "msg": "/var/log"
}
Score:1
br flag

Q: "Print just the path of a register variable."

A: The declaration below gives you what you want

  my_dirs: "{{ folders_info.results|json_query('[].files[].path') }}"

Example

Given the tree for testing

shell> tree /tmp/test
/tmp/test
├── usr
│   └── bin
│       └── dosepsbin
└── var
    ├── db
    ├── lib
    │   └── dkms
    │       ├── test1
    │       │   └── log
    │       ├── test2
    │       │   └── log
    │       └── test3
    │           └── log
    ├── local
    └── log

15 directories, 0 files

The playbook

shell> cat pb.yml
- hosts: localhost
  become: true

  vars:

    my_dirs: "{{ folders_info.results|json_query('[].files[].path') }}"
  
  tasks:

    - name: find directories
      find:
        paths: "{{ item }}"
        recurse: true
        file_type: directory
        use_regex: true
        patterns:
          - '.*log.*'
          - '.*sbin.*'
      loop:
        - /tmp/test/var
        - /tmp/test/usr
      register: folders_info

    - debug:
        var: my_dirs

gives

shell> ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************************************************************************************************************

TASK [find directories] ***********************************************************************************************************************************************************************
ok: [localhost] => (item=/tmp/test/var)
ok: [localhost] => (item=/tmp/test/usr)

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => 
  my_dirs:
  - /tmp/test/var/log
  - /tmp/test/var/lib/dkms/test1/log
  - /tmp/test/var/lib/dkms/test2/log
  - /tmp/test/var/lib/dkms/test3/log
  - /tmp/test/usr/bin/dosepsbin

PLAY RECAP ************************************************************************************************************************************************************************************
localhost: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.