Unable to wildcard a template destination parameter

20 views
Skip to first unread message

Tim Davidson

unread,
Nov 2, 2017, 9:44:48 AM11/2/17
to Ansible Project
Hello,

I'm trying to read some .csv files, then create config files for each of the .csv files, based on a template. Reading the files is not a problem. However, I can't seem to get the destination parameter to work. I would like the output file to be the name of the original .csv file with a .xml extension.

---

- name: Read the csv files
  find:
    paths: /home/sysadmin/TimsTest1/roles/opengear/files
    patterns: '*.csv'
  register: files_matched

- name: Create OpenGear device configs
  template:
    src: "opengear8port.j2"
    dest: "{{ files_matched.files.path | basename | regex_replace('csv','xml') }}"
  with_items: "{{ files_matched.files }}"

When I run the playbook, I receive the following on the final task:

TASK [opengear : Create OpenGear device configs] ***************************************************************************************************************************
task path: /home/sysadmin/TimsTest1/roles/opengear/tasks/main.yml:9
fatal: [localhost]: FAILED! => {
    "failed": true, 
    "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'path'\n\nThe error appears to have been in '/home/sysadmin/TimsTest1/roles/opengear/tasks/main.yml': line 9, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Create OpenGear device configs\n  ^ here\n"
}

I can't seem to get the syntax correct on the destination. Any help is appreciated.

Thank you

Kai Stian Olstad

unread,
Nov 2, 2017, 11:44:14 AM11/2/17
to ansible...@googlegroups.com
On 02. nov. 2017 14:44, Tim Davidson wrote:
> Hello,
>
> I'm trying to read some .csv files, then create config files for each of
> the .csv files, based on a template. Reading the files is not a problem.
> However, I can't seem to get the destination parameter to work. I would
> like the output file to be the name of the original .csv file with a .xml
> extension.
>
> ---
>
> - name: Read the csv files
> find:
> paths: /home/sysadmin/TimsTest1/roles/opengear/files
> patterns: '*.csv'
> register: files_matched
>
> - name: Create OpenGear device configs
> template:
> src: "opengear8port.j2"
> dest: "{{ files_matched.files.path | basename |
> regex_replace('csv','xml') }}"
> with_items: "{{ files_matched.files }}"


When you're using with_items, it takes the first item in files_matched.files and put it in the variable called item, so your dest should be

dest: "{{ item.path | basename | regex_replace('csv','xml') }}"


Regex_replace will overwrite csv in the filename too, to avoid that you can use this instead:

dest: "{{ (item.path | basename).rsplit('.', 1)[0] }}.csv"


--
Kai Stian Olstad

Tim Davidson

unread,
Nov 2, 2017, 12:53:10 PM11/2/17
to Ansible Project
Thank you Kai,

That worked. I'm obviously new to Ansible so I'm sure I'll have many more questions in the future.

-Tim
Reply all
Reply to author
Forward
0 new messages