Search code examples
ansible

“Destination directory does not exist” error while doing template copying


While copying over templates using the following code

- name: "Template source files to temp directory"
  template:
    src: '{{ item.src }}'
    dest: '{{ temp_file_path.path }}/{{ item.path }}'
    force: yes
  with_filetree: "{{ nginx_path }}/"
  delegate_to: 127.0.0.1
  when: item.state == 'file' 

I got hit by the error in the title. I got a possible solution from here but the problem here is that one needs to know the names of all the sub directories. This isn’t possible when you runs into tens of them.

What’s the right way to solve this. Something similar to what rysnc does with the - -relative option


Solution

  • You need to create the directories first. You can do it in the similarly to your current task:

    - name: "Template source files to temp directory"
      file:
        path: '{{ temp_file_path.path }}/{{ item.path }}'
        state: directory
      with_filetree: "{{ nginx_path }}/"
      delegate_to: 127.0.0.1
      when: item.state == 'directory'