Ansible trim extension name

Viewed 132

I want to get list of name files without full path and without extension name into 2 differents variable, I succeed for the first one, but not on second one.

I have a variable found_files which contains list files on folder

- name: Get only file names
  set_fact:
    file_names: "{{ found_files['files'] | map(attribute='path') | map('basename') | list }}"

- name: Get name without extension
  set_fact:
    file_names_without_extension: "{{ item | splitext | first | splitext | first }}"                                                                                          with_list: "{{ file_names }}" 

the first task, I get the correct name of files without path, but the second task I get only the first file withtout extension file, and don't succeed to get all file name without extension name.

1 Answers

Simply map splitext and first

    - set_fact:
        file_names_without_extension: "{{ file_names|
                                          map('splitext')|
                                          map('first')|
                                          list }}"
      vars:
        file_names:
          - test1.txt
          - test2.txt
          - test3.txt

gives

  file_names_without_extension:
  - test1
  - test2
  - test3
Related