目录
copy
模块是Ansible中用于将本地或远程主机上的文件复制到目标主机上的重要模块之一。它可以将文件的实际内容直接写入目标主机,也可以从本地文件系统中复制文件。以下是copy
模块的详细介绍,包括其常用选项和示例。
-
src:
- 描述:要复制的本地源文件的路径。如果
content
参数没有被指定,则这个参数是必需的。 - 类型:字符串
- 示例:
src: /path/to/source/file
- 描述:要复制的本地源文件的路径。如果
-
content:
- 描述:要复制的文件内容。与
src
参数互斥。 - 类型:字符串
- 示例:
content: "This is the content of the file"
- 描述:要复制的文件内容。与
-
dest:
- 描述:要将文件复制到的远程主机上的路径。
- 类型:字符串
- 示例:
dest: /path/to/destination/file
-
backup:
- 描述:如果目标文件存在,是否在覆盖前创建一个备份。
- 类型:布尔值
- 默认值:
no
- 示例:
backup: yes
-
mode:
- 描述:设置文件的权限模式(类似于 chmod)。
- 类型:字符串
- 示例:
mode: '0644'
-
owner:
- 描述:设置文件的所有者。
- 类型:字符串
- 示例:
owner: username
-
group:
- 描述:设置文件的组。
- 类型:字符串
- 示例:
group: groupname
-
force:
- 描述:如果文件已经存在,是否强制替换文件。
- 类型:布尔值
- 默认值:
yes
- 示例:
force: no
-
remote_src:
- 描述:如果源文件在远程主机上而不是在本地,则设置为
yes
。 - 类型:布尔值
- 默认值:
no
- 示例:
remote_src: yes
- 描述:如果源文件在远程主机上而不是在本地,则设置为
-
validate:
- 描述:在复制文件之前,运行指定的命令以验证文件的正确性。该命令需要包含
%s
,它会被临时文件的路径替换。 - 类型:字符串
- 示例:
validate: '/usr/bin/sudo /usr/bin/visudo -cf %s'
- 描述:在复制文件之前,运行指定的命令以验证文件的正确性。该命令需要包含
使用 ansible
命令
直接在命令行上用 ansible
命令来执行单个任务:
ansible all -m copy -a "src=/path/to/source/file dest=/path/to/dest/file"
all
: 目标主机(对所有主机)。-m ansible.builtin.copy
: 模块名称 (copy
)。-a "src=/path/to/source/file dest=/path/to/dest/file"
: 模块参数,包含源文件和目标路径。
结合其他选项
设置文件权限和所有者
同样可以在命令中设置文件的权限和所有者:
ansible all -m copy -a "src=/path/to/source/file dest=/path/to/dest/file owner=user group=group mode=0644"
在覆盖之前创建备份
在覆盖目标文件前先创建备份:
ansible all -m copy -a "src=/path/to/source/file dest=/path/to/dest/file backup=yes"
从远程主机复制文件
如果源文件在远程主机上,可以使用 remote_src=yes
选项:
ansible all -m copy -a "src=/path/to/source/file dest=/path/to/dest/file remote_src=yes"
下面是如何在Ansible的Playbook中使用copy
模块的一些示例。Playbook是使用YAML格式编写的自动化脚本,可以定义一系列任务来管理你的基础设施。
Yaml基本示例
以下是一个将本地文件复制到远程主机的Playbook示例:
---
- name: Copy file example
hosts: all
tasks:
- name: Copy a file to the remote server
copy:
src: /path/to/source/file
dest: /path/to/dest/file
设置文件权限和所有者
在copy
模块中设置文件的权限、所有者以及组:
---
- name: Copy file with permissions
hosts: all
tasks:
- name: Copy a file with specific owner, group, and permissions
copy:
src: /path/to/source/file
dest: /path/to/dest/file
owner: username
group: groupname
mode: '0644'
在覆盖之前创建备份
在覆盖目标文件之前创建备份:
---
- name: Copy file with backup
hosts: all
tasks:
- name: Copy a file and create a backup if it exists
copy:
src: /path/to/source/file
dest: /path/to/dest/file
backup: yes
从远程主机复制文件
如果源文件在远程主机上而不是控制节点上,可以使用remote_src
参数:
---
- name: Copy file from remote source
hosts: all
tasks:
- name: Copy a file from a remote location
copy:
src: /path/to/source/file
dest: /path/to/dest/file
remote_src: yes
直接指定文件内容
还可以直接在Playbook中指定文件内容,而不是从文件中复制:
---
- name: Copy content directly
hosts: all
tasks:
- name: Provide content directly to the remote file
copy:
content: |
This is the content
of the file that we are copying.
dest: /path/to/dest/file
完整示例
以下是一个较为复杂的Playbook示例,包含多个任务:
---
- name: Comprehensive copy example
hosts: all
tasks:
- name: Copy file with specific permissions
copy:
src: /path/to/source/file
dest: /path/to/dest/file
owner: username
group: groupname
mode: '0644'
backup: yes
- name: Ensure directory exists before copying
file:
path: /path/to/destination
state: directory
owner: username
group: groupname
mode: '0755'
- name: Copy file from remote source
copy:
src: /remote/path/to/source/file
dest: /path/to/destination/file
remote_src: yes
- name: Provide content directly
copy:
content: |
This is the content
of the file that we are copying.
dest: /path/to/destination/content_file
owner: username
group: groupname
mode: '0644'