Copy folder content with Ansible

橙三吉。 提交于 2020-01-24 05:29:05

问题


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.



来源:https://stackoverflow.com/questions/42279226/copy-folder-content-with-ansible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!