# Descending into subdirs and running commands against files

**URL:** <https://forum.ansible.com/t/descending-into-subdirs-and-running-commands-against-files/43423>\
**Category:** Get Help\
**Created:** [June 16, 2025, 6:58pm UTC](https://forum.ansible.com/t/descending-into-subdirs-and-running-commands-against-files/43423 "2025-06-16T18:58:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trt757](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trt757](https://forum.ansible.com/u/trt757)\
**Post date:** [June 16, 2025, 6:58pm UTC](https://forum.ansible.com/t/descending-into-subdirs-and-running-commands-against-files/43423/1 "2025-06-16T18:58:31Z")

</div>

I have a directory on the intermediate CA server, that holds two subdirs, that are named with the hostnames of my two test workstations. What I’m trying to do is instruct Ansible to descent into each directory and run an openssl command against a host.csr and host.req. This is what I have so far:  
First: just to find the csr and req files in the subdirs:

- name: find all csr  
ansible.builtin.find:  
paths: /mypath  
recurse: yes  
file\_type: file  
patterns: “_.req" (this whole section repeated for "_.csr”  
register: serverCSR (for the next section - serverReq)

- name: Run registered results in openssl  
ansible.builtin.shell  
cmd: openssl ca -bath {{ serverReq.files }} -extensions server\_cert -days 730 -notext -md sha256 -in {{ serverCSR }} -out server.crt

Of course the above isn’t working and I don’t know if you can use registered variables in this way. Also, my preference would be “hostname”.crt to match the directory the \*.req and \*.csr were in, but I could mv the file in a later play.

Someone suggested using block, but I don’t see how that would be helpful. I’d really wish this could be run on the hosts but security rules prevent me from copying the CA’s private key to the workstations.

If anyone has any ideas I’d appreciate it.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [June 16, 2025, 9:20pm UTC](https://forum.ansible.com/t/descending-into-subdirs-and-running-commands-against-files/43423/2 "2025-06-16T21:20:15Z")

</div>

You can use registered variables like that, it is just that it won’t give you what you want, in this case `files` is a list of dictionaries, what you want is to extract the `paths` key of each, try:

```auto
... {{ serverReq.files|map(attribute='path')|join(' ') }}

```

That will add all the files, space separated, but I think you want a loop, your command does not look like it is set to handle the list

```auto
name: Run registered results in openssl
ansible.builtin.shell:
cmd: openssl ca -bath -extensions server_cert -days 730 -notext -md sha256 -in {{ item }} -out {{item|basename}}-server.crt
loop: "{{ serverReq.files|map(attribute='path') }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 16, 2025, 9:20pm UTC](https://forum.ansible.com/t/descending-into-subdirs-and-running-commands-against-files/43423/3 "2025-07-16T21:20:52Z")

</div>

Closed on July 16, 2025, 9:20pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
