Copy folder content with Ansible

2019-06-27 07:04发布

问题:

For some weird reasons I'm having troubles with a simple task which is copying a content of the folder myfiles (few files in there) to the dist/myfiles location. Task looks like this:

name: Deploy config files like there is no tomorrow
copy:
  src: "{{ item }}"
  dest: "/home/{{ ansible_user_id }}/dist/{{ item }}"
with_items:
  - 'config'
  - 'myfiles/'

myfiles folder exist under the dist and config file is copied to the dist folder.

Is this possible in Ansible or I should copy each file separately? Am I doing it completely wrong?

回答1:

Your task copies both: the config file and the myfiles on Debian and CentOS targets properly.


If for some reason you have a problem, you might have a look at Looping over Fileglobs.

You need to split the task into two, with the second one looking like:

- name: Deploy multiple config files
  copy:
    src: "{{ item }}"
    dest: "/home/{{ ansible_user_id }}/dist/myfiles/{{ item | basename }}"
  with_fileglob:
    - /path/to/myfiles/*

For a recursive copy, check this question on SeverFault


Alternatively, you could use the synchronize module, but pay special attention when using become. See this question on SuperUser.



标签: ansible