首页 > 解决方案 > 使用 remote_src 时,Ansible 副本不保留模式“保留”的原始权限

问题描述

我正在使用 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和保留原始文件/目录权限?复制模块的文档是这样说的,但我可能遗漏了一些东西。

标签: ansibleansible-2.x

解决方案


ansible 文件说

remote_src 从2.8版开始支持递归复制。从2.6版开始,remote_src 仅适用于 mode=preserve

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


推荐阅读