Ansible Copy 模块使用详解

Ansible copy模块是用于将文件从控制节点复制到远程服务器的模块。它可以将文件、目录或文本从控制节点复制到远程服务器,也可以创建目标目录。

下面是一些EXAMPLE:

- name: Copy a single file to remote host
  copy:
    src: /path/to/local/file.txt
    dest: /path/to/remote/directory/
    owner: username
    group: usergroup
    mode: '0644'
    backup: yes

上面的example使用了copy模块将本地文件/path/to/local/file.txt复制到远程服务器的/path/to/remote/directory/目录下。ownergroup参数指定目标文件的所有者和组,mode参数设置文件权限,backup参数启用备份。

以下是一些copy模块的常见用例:

1. 复制文件到远程服务器

- name: Copy a single file to remote host
  copy:
    src: /path/to/local/file.txt
    dest: /path/to/remote/directory/

上述例子使用了copy模块将本地文件/path/to/local/file.txt复制到远程服务器的/path/to/remote/directory/目录下。

2. 复制多个文件到远程服务器

- name: Copy multiple files to remote host
  copy:
    src: "{
    
    { item }}"
    dest: /path/to/remote/directory/
  with_items:
    - /path/to/local/file1.txt
    - /path/to/local/file2.txt
    - /path/to/local/file3.txt

上面的example使用了copy模块将多个本地文件复制到远程服务器的/path/to/remote/directory/目录下。with_items参数指定了要执行的多个拷贝任务。

3. 复制文件并改变所有权和权限

- name: Copy file with changed ownership and permissions
  copy:
    src: /path/to/local/file.txt
    dest: /path/to/remote/directory/
    owner: username
    group: usergroup
    mode: '0644'

上面的example使用了copy模块将本地文件/path/to/local/file.txt复制到远程服务器的/path/to/remote/directory/目录下,并将目标文件的所有者、组和权限更改为指定的值。

4. 复制目录到远程服务器

- name: Copy directory recursively to remote host
  copy:
    src: /path/to/local/directory/
    dest: /path/to/remote/directory/
    owner: username
    group: usergroup
    mode: '0755'
    recursive: yes

上面的example使用了copy模块将本地目录/path/to/local/directory/及其所有子目录和文件递归地复制到远程服务器的/path/to/remote/directory/目录下,并将其所有者、用户组和权限更改为指定的值。

5. 从文本中复制内容到远程服务器

- name: Copy text content to remote host
  copy:
    content: |
      This is an example text.
      This is another line of text.
    dest: /path/to/remote/directory/file.txt

上面的example使用了copy模块将文本内容复制到远程服务器的/path/to/remote/directory/file.txt文件中。文本内容通过content参数提供。

猜你喜欢

转载自blog.csdn.net/qq_34185638/article/details/131177065