如果文件不存在,是否有方法在创建文件夹的同时复制文件?
下面是我要复制的代码,但是,它不起作用,因为它将source_code_management
创建为文件,而不是目录。
- name: Transfer file
copy:
src: "{{ playbook_dir }}/roles/source_code_management/logger.xml"
dest: "{{ configuration_path }}"
我希望同时创建和复制的原因是Jenkins将通过path/file_name。
您需要在此之前添加任务以确保目录存在:
- name: Directory source code exists
file:
src: "{{ playbook_dir }}/roles/source_code_management"
state: directory
这就是它迄今为止的工作方式(ansible 2.9和之前(
遗憾的是,使用Ansible无法在一个步骤中完成此操作。
也就是说,如果你在一个变量中存储了一个/path/to/file
,那么仍然有一个额外的Jinja过滤器适合这个工作:过滤器dirname
从/path/to/file这样的路径获取目录。
有了这些你可以,首先创建你的目录,然后复制你的文件:
- name: First, create the directory
file:
path: "{{ configuration_path | dirname }}"
state: directory
recurse: yes
- name: Then, transfer the file
copy:
src: "{{ playbook_dir }}/roles/source_code_management/logger.xml"
dest: "{{ configuration_path }}"