Okay, let me briefly explain what’s on my mind right now.
I have a directory where another system stores files that I need to continue working with. The following files are currently located there, for example:
/srv/orders/local/Ocustomername1.+2023+february+1.txt
/srv/orders/local/Ocustomername5.+2020+may+14.txt
/srv/orders/local/Ocustomername2.+2025+october+1923.txt
/srv/orders/local/Ocustomername7.+2026+january+29.txt
The contents of the /srv/orders/local/ directory change, and I don’t know what the individual files are actually called. The only clue is the customer’s name, e.g., “customername1.”
Now I have to extract and remember the file name and path, including the name, for further actions. To do this, I have created the following small task string for customername2:
---
- name: "Determine customer file name."
ansible.builtin.command:
cmd: find /srv/orders/local/ -name Ocustomername2.*.txt
register: market_ordername
changed_when: market_ordername.rc != 0
- name: "Make a note of the file name and path of the order file."
ansible.builtin.set_fact:
market_order_path: "{{ market_ordername.stdout }}"
market_order_filename: "{{ market_ordername.stdout[19:] }}"
cacheable: true
- name: "Order filename"
ansible.builtin.debug:
msg: "The order filename is: {{ market_order_filename }}"
- name: "Order path."
ansible.builtin.debug:
msg: "The whole path of the order is: {{ market_order_path }}"
...
So far so good. That works without any problems, as I only pick one name from the order list. However, there are around 50 different files in the directory, and I need the data for not just one customer, but for four or 21 selected customers!
I therefore have a corresponding array in the inventory:
customers:
- info : 'Huber'
market_order_name: 'customername1'
market_order_filename: ''
market_order_path: ''
- info : 'Mustermann'
market_order_name: 'customername2'
market_order_filename: ''
market_order_path: ''
- info : 'Maier'
market_order_name: 'customername42'
market_order_filename: ''
market_order_path: ''
- info : 'Schneider'
market_order_name: 'customername69'
market_order_filename: ''
market_order_path: ''
If I knew all the customer data, I could put it in this array and then later access this content specifically in a task using with_items. Unfortunately, I don’t have this data because the data in the directory in question changes frequently.
I have to laboriously extract the data from the directory myself and remember that I can access it later.
My idea was as follows. I extract the determined data (file and path name) using the task shown above and write it to the existing fields of the array using set_facts. This allows me to access all data in the array later on.
- name: "Determine customer file name."
ansible.builtin.command:
cmd: find /srv/orders/local/ -name Ocustomername2.*.txt
register: market_ordername
changed_when: market_ordername.rc != 0
- name: "Make a note of the file name and path of the order file."
ansible.builtin.set_fact:
market_order_path: "{{ market_ordername.stdout }}"
market_order_filename: "{{ market_ordername.stdout[19:] }}"
cacheable: true
So I have to run this task for every market_order_name from the array and then use set_facht to put the result of market_order_filename and market_order_path into the respective fields of the array. That’s my naive approach.
My attempt to combine ansible.builtin.include_tasks with a with_items loop failed miserably.
But what’s the best way to do that? Maybe someone has an idea and can point me in the right direction. I’m grateful for any tips!