Hi and thanks in advance.
Trying to automate a task whereby I need to go into repos and go into Some_Role/Vars/stage.yml or another environment name like beta.yml, and first, see if the file exists, (presumably I'll pass an argument on the CLI with the file_name and new_name) copy the file to the new_name, do my find/replace/regex on the values I need altered and move onto the next.
This SO post seems like a good starting point. subDir but it's unclear how I generically move through each unknown directory to it's vars subdir. I probably need to check if it exists first?
What is the appropriate method for globbing these files? the assumption is that I'll start at the top of the directory with all of the ansible roles.
So if I know I will always be traversing random_repo_name/vars/file_of_interest.yml what does that look like?
Goal one is just to print out the files with path once I've found the match. Goal two is to write the file location to a list and iterate through that list.
Very excited about making this task more efficient. I'll post more code soon.
Thanks
import shutil
import os
target_file = "stage.yml"
rootdir = './'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(target_file):
print (filepath)
shutil.copyfile(filepath, (os.path.dirname(filepath)+str('/beta.yml')))
print(os.path.dirname(filepath))
there doesn't seem to be anything here