-1

I currently pass parameters to my script which an ansible tower job template/role calls. In order to make it more user friendly, I have decided to use a survey to do this. The script takes on filenames as parameters.

The script accepts the parameters in this format

'file1.txt','file2.txt','file3.txt'

However, here is the problem

The file could be one, it could be 2 or three up to possibly 5.

I have thought about a solution and I think the best design is to have a comma delimited list of files coming from ansible survey, for example file1, file2, file3

How can I have a logic whereby with the list of files, they can be split and a loop used to copy them one by one if there are more than one file provided in the list, then have a variable that will add single quotes and a comma to the file list. For example in the survey the values provided such as file1.txt, file2.txt, file3.txt will then be transformed into a variable which contains the following

'file1.txt','file2.txt','file3.txt'

The other issue is this.

The ansible role copies the given file name onto a directory, I know the split function can be used to split a comma separated list, how can I then copy them onto a folder in a loop ? If we look at the example below, it only works for a single file.

EDIT.

I have looked at the split function and combining it with a loop. I get an error when I run it. Template error while templating string

---

- name: Set file name
  set_fact:
  file1: "file1.txt"
  file_list: "file1.txt"


- name: Set working directory
  set_fact:
  standard_path: "{{ansible_user_dir}}\\execution"
  content_file: "{{standard_path}}\\{{file1}}"

- name: Copy file to working directory
  win_copy:
    src: "file1.txt"
    dest: "{{content_file}}"

- name: Set parameters for script
  set_fact:
    params: "-filenames '{{content_file}}'"


- name: Run a loop to copy the files.
  win_copy:
    src: "{{ item }}"
    dest: "{{standard_path \\ item }}"
  with_items: "{{file_list.split(',') }}"
user3535468
  • 145
  • 13
  • 1
    The multiple choice survey type returns a list. You can loop on that to copy and use the `join` filter to create your parameter string. – Zeitounator Aug 24 '21 at 06:22
  • Thanks for the comment, I have edited the question to use a single text field and provide a comma delimited list of files. this will be easier to transform based on my research. – user3535468 Aug 24 '21 at 11:47
  • The text survrey type returns a string. `your_var.split(',')` that string to get a list then go back to the second part of my first comment. – Zeitounator Aug 24 '21 at 15:17
  • Please do you have an example as I have searched online to find how to use a split and loop. – user3535468 Aug 24 '21 at 23:05
  • I already gave a split example. Loops have [an entire dedicated page](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html) in the documentation. – Zeitounator Aug 25 '21 at 06:31
  • Thanks, I edited the code to test out the split. it splits out the values which is fine, I then thought i could use this within a loop using with_items, its giving me an error above. – user3535468 Aug 27 '21 at 16:49
  • The error is not in the loop but in dest. Good luck. – Zeitounator Aug 27 '21 at 20:25
  • 1
    Thanks @Zeitounator I figured out the issue and fixed it. – user3535468 Sep 01 '21 at 15:10

1 Answers1

1

The section within the code that was throwing an exception has now been fixed. The jinja2 standard requires the variables to be in separate {{}} as seen below.

- name: Run a loop to copy the files.
  win_copy:
    src: "{{ item }}"
    dest: "{{standard_path}}\\{{item }}"
  with_items: "{{file_list.split(',') }}"
user3535468
  • 145
  • 13