Essentially, I want to be able to handle "wildcard file names" on Linux using ansible. Essentially, this means using the ls command with part of the file name followed by "*" so that it displays ONLY certain files.
However, I cannot save the output correctly in a variable, since more than one file name is likely to be returned. Thus, I want to be able to store these results no matter how many they can be in the array during one task. Then I want to get all the results from an array in a later task. Also, since I do not know how many files can be returned, I cannot complete the task for each file name, and the array makes more sense.
The reason for this is that files in random storage often change, but they always have the same first half. This is their second half of their names, which are random, and I do not want hard code, which is basically.
I'm not sure how to properly implement / manipulate an array in an indispensable way, so the following code is an example of what I'm trying to execute. Obviously, it will not function as intended if more than one file name is returned, so I asked for help on this topic:
- hosts: <randomservername>
remote_user: remoteguy
become: yes
become_method: sudo
vars:
aaaa: b
tasks:
- name: Copy over all random file contents from directory on control node to target clients. This is to show how to manipulate wildcard filenames.
copy:
src: /opt/home/remoteguy/copyable-files/testdir/
dest: /tmp/
owner: remoteguy
mode: u=rwx,g=r,o=r
ignore_errors: yes
- name: Determine the current filenames and store in variable for later use, obviously for this exercise we know part of the filenames.
shell: "ls {{item}}"
changed_when: false
register: annoying
with_items: [/tmp/this-name-is-annoying*, /tmp/this-name-is-also*]
- name: Run command to cat each file and then capture that output.
shell: cat {{ annoying }}
register: annoying_words
- debug: msg=Here is the output of the two files. {{annoying_words.stdout_lines }}
- name: Now, remove the wildcard files from each server to clean up.
file:
path: '{{ item }}'
state: absent
with_items:
- "{{ annoying.stdout }}"
I understand that the YAML format is a bit confused, but if it is fixed, it will work fine, it just won’t give me the result I’m looking for. Thus, if there were 50 files, I would like them to be able to process them all and / or delete them all .. etc. Etc.
- , , !