使用remote_src时,Ansible副本不会以“ preserve”模式保留原始权限

时间:2019-10-11 15:48:42

标签: ansible ansible-2.x

我正在使用Ansible 2.8.5(目标服务器是Red Hat 4.8.5-39)。我正在从GitLab复制一些文件/目录到几个远程主机中。

我要先将初始副本复制到共享位置(因此run_once: true):

- name: "Copy/Transfer application, configuration, and support file(s)"
  block:
    - name: "Copying application build"
      copy:
        dest: "{{ path_tmp }}/{{ CI_PIPELINE_ID }}/"
        mode: "0755"
        src: "{{ CI_PROJECT_DIR }}/build/libs/{{ artifact_id }}.war"
      run_once: true
    - name: "Copying (template) configuration and support file(s)"
      template:
        dest: "{{ path_tmp }}/{{ CI_PIPELINE_ID }}/{{ item.dest }}"
        mode: "0644"
        src: "{{ item.src }}"
      run_once: true
      with_items:
        - { dest: "config/logback.xml", src: "logback.xml.j2" }
        - { dest: "{{ artifact_id }}.conf", src: "{{ artifact_id }}.conf.j2" }

...然后将文件复制到每个主机上的所需位置:

- name: "Deploy/Install new application"
  block:
    # All this Jiu Jitsu just to clear {{ path_home }}/ directory
    - name: "Collecting current directories and/or files inside {{ path_home }}/"
      find:
        file_type: any
        hidden: yes
        paths: "{{ path_home }}/"
      register: collected_items
    - name: "Removing current directories and/or files inside {{ path_home }}/"
      file:
        path: "{{ item.path }}"
        state: absent
      with_items: "{{ collected_items.files }}"
    - name: "Copying new application, configuration, and support files"
      copy:
        dest: "{{ path_home }}/"
        mode: preserve
        remote_src: yes
        src: "{{ path_tmp }}/{{ CI_PIPELINE_ID }}/"
    ...

问题在于文件权限没有得到“认可”,我不想定义几个步骤来更正此问题。这是最初复制文件/目录的方式(以及我想要的方式):

[deployer@unix core]$ ll -AR 41397/
41397/:
total 51M
drwxr-xr-x. 3 tomcat 4.0K Oct 11 11:23 .
drwxr-xr-x. 5 tomcat 4.0K Oct 11 11:22 ..
drwxr-xr-x. 2 tomcat 4.0K Oct 11 11:23 config
-rw-r--r--. 1 tomcat 1.2K Oct 11 11:23 core.conf
-rwxr-xr-x. 1 tomcat  50M Oct 11 11:23 core.war

41397/config:
total 12K
drwxr-xr-x. 2 tomcat 4.0K Oct 11 11:23 .
drwxr-xr-x. 3 tomcat 4.0K Oct 11 11:23 ..
-rw-r--r--. 1 tomcat 1.6K Oct 11 11:23 logback.xml

...这就是使用remote_src: yes复制后的样子:

[deployer@unix core]$ ll -AR /data/st01/apps/core/
/data/st01/apps/core/:
total 50M
drwxr-xr-x. 3 tomcat 4.0K Oct 11 11:23 .
drwxr-xr-x. 3 tomcat 4.0K Oct  9 16:36 ..
drwxr-xr-x. 2 tomcat 4.0K Oct 11 11:23 config
-rw-r-----. 1 tomcat 1.2K Oct 11 11:23 core.conf
-rw-r-----. 1 tomcat  50M Oct 11 11:23 core.war

/data/st01/apps/core/config:
total 12K
drwxr-xr-x. 2 tomcat 4.0K Oct 11 11:23 .
drwxr-xr-x. 3 tomcat 4.0K Oct 11 11:23 ..
-rw-r--r--. 1 tomcat 1.6K Oct 11 11:23 logback.xml

是否可以使用remote_src: yes并保留原始文件/目录权限? copy module的文档中是这样说的,但是我可能遗漏了一些东西。

2 个答案:

答案 0 :(得分:0)

可用文档说

remote_src从2.8版开始支持递归复制。 从2.6版本开始,remote_src仅与mode = preserve一起使用。

您要么需要将系统降级到ansible 2.6,要么尝试提供具有所需权限(例如0644或01777)的“模式”

答案 1 :(得分:0)

对我来说,另一种解决方案是使用synchronize模块,因为我没有很多文件要复制/移动

- name: "Copy latest application build, configuration, and support file(s)"
  synchronize:
    delete: yes
    dest: "{{ app_path }}/latest/"
    recursive: yes
    src: "{{ tmp_path }}/{{ PIPELINE_ID }}/"
  delegate_to: "{{ inventory_hostname }}"