Ansible模块——从控制节点向目标主机复制文件!
复制文件或字符串到被控主机的指定位置
ansible.builtin.copy
:复制文件到被控主机指定位置,也可以将字符串写入文件(覆盖)。
选项 | 类型 | 默认值 | 描述 |
attributes | str | null | 设置目标文件的额外属性,如 |
backup | bool | false | 如果目标存在,操作前创建备份文件。 |
checksum | str | null | 用来验证源文件内容的一致性的 SHA1 校验值。 |
content | str | null | 直接指定要写入文件的文本内容(不从 |
decrypt | bool | true | 如果 |
dest | path | null | 目标路径 ,必须指定。 |
directory_mode | str | null | 如果递归复制目录时(目标目录不存在),目录使用的权限模式。 |
follow | bool | false | 如果目标中存在文件系统链接,则应遵循该链接。 |
force | bool | true | 如果目标存在且不同,是否强制覆盖。 |
group | str | null | 设置目标文件的属组。 |
local_follow | bool | false | 控制在 |
mode | str | null | 设置目标文件权限(比如 |
owner | str | null | 设置目标文件的属主。 |
remote_src | bool | no | true 时表示 |
selevel | str | null | 设置 SELinux 的 level 属性。通常不需要。 |
serole | str | null | 设置 SELinux 的 role 属性。 |
setype | str | null | 设置 SELinux 的 type 属性。 |
seuser | str | null | 设置 SELinux 的 user 属性。 |
src | path | null | 源文件路径(在控制机或者远程主机,根据 |
unsafe_writes | bool | false | 允许以不安全的方式写入文件,提升性能(但可能有风险,比如写一半被打断)。 |
validate | str | null | 在复制文件之前执行指定的验证命令,验证命令的返回值决定是否继续执行文件复制。如果命令返回非零状态,复制操作将失败。( |
常用选项:
选项 | 类型 | 默认值 | 描述 |
backup | bool | false | 如果目标存在,操作前创建备份文件。 |
content | str | null | 直接指定要写入文件的文本内容(不从 |
dest | path | null | 目标路径 ,必须指定。 |
directory_mode | str | null | 如果递归复制目录时(目标目录不存在),目录使用的权限模式。 |
follow | bool | false | 如果目标中存在文件系统链接,则应遵循该链接。 |
force | bool | true | 如果目标存在且不同,是否强制覆盖。 |
group | str | null | 设置目标文件的属组。 |
local_follow | bool | false | 控制在 |
mode | str | null | 设置目标文件权限(比如 |
owner | str | null | 设置目标文件的属主。 |
remote_src | bool | false | true 时表示 |
src | path | null | 源文件路径(在控制机或者远程主机,根据 |
validate | str | null | 在复制文件之前执行指定的验证命令,验证命令的返回值决定是否继续执行文件复制。如果命令返回非零状态,复制操作将失败。( |
- name: Copy file with owner and permissionsansible.builtin.copy:src: /srv/myfiles/foo.confdest: /etc/foo.confowner: foogroup: foomode: '0644'- name: Another symbolic mode example, adding some permissions and removing othersansible.builtin.copy:src: /srv/myfiles/foo.confdest: /etc/foo.confowner: foogroup: foomode: u+rw,g-wx,o-rwx- name: Copy a new "ntp.conf" file into place, backing up the original if it differs from the copied versionansible.builtin.copy:src: /mine/ntp.confdest: /etc/ntp.confowner: rootgroup: rootmode: '0644'backup: yes- name: Copy a "sudoers" file on the remote machine for editingansible.builtin.copy:src: /etc/sudoersdest: /etc/sudoers.editremote_src: yesvalidate: /usr/sbin/visudo -csf %s- name: Copy using inline contentansible.builtin.copy:content: '# This file was moved to /etc/other.conf'dest: /etc/mine.conf- name: If follow=yes, /path/to/file will be overwritten by contents of foo.confansible.builtin.copy:src: /etc/foo.confdest: /path/to/link # link to /path/to/filefollow: yes- name: If follow=no, /path/to/link will become a file and be overwritten by contents of foo.confansible.builtin.copy:src: /etc/foo.confdest: /path/to/link # link to /path/to/filefollow: no
复制模板文件到被控节点指定位置
ansible.builtin.template
:复制 Jinja2
模板文件到被控节点指定位置。
参数名 | 类型 | 默认值 | 描述 |
attributes | str | null | 设置目标文件的文件属性字符串,如 |
backup | bool | false | 如果目标文件发生变化,则备份原文件。 |
block_start_string | str | {% | Jinja2 块开始标识符(高级定制)。 |
block_end_string | str | %} | Jinja2 块结束标识符(高级定制)。 |
comment_start_string | str | {# | Jinja2 注释开始标识符。 |
comment_end_string | str | #} | Jinja2 注释结束标识符。 |
dest | path | null | 模板渲染后复制到受控节点的路径。 |
follow | bool | false | 是否跟随软链接, |
force | bool | true | 如果目标文件存在且不同,是否强制覆盖。 |
group | str | null | 设置目标文件的属组。 |
lstrip_blocks | bool | false | 去除 Jinja2 块标签前的空格。 |
mode | str | null | 设置目标文件的权限,如 |
newline_sequence | str | \n | 设置模板渲染后的换行符( |
output_encoding | str | utf-8 | 输出文件编码。 |
owner | str | null | 设置目标文件的属主。 |
selevel | str | null | SELinux 等级。 |
serole | str | null | SELinux 角色。 |
setype | str | null | SELinux 类型。 |
seuser | str | null | SELinux 用户。 |
src | path | null | 本地模板文件的路径(相对于 |
trim_blocks | bool | false | 控制是否移除 Jinja2 块之间的换行符。 |
unsafe_writes | bool | false | 避免临时文件写法,某些情况下可以避免某些文件系统的问题。 |
validate | str | null | 在替换目标文件前执行的验证命令(如 |
variable_start_string | str | {{ | Jinja2 变量开始标识符。 |
variable_end_string | str | }} | Jinja2 变量结束标识符。 |
常用选项:
参数名 | 类型 | 默认值 | 描述 |
backup | bool | false | 如果目标文件发生变化,则备份原文件。 |
comment_start_string | str | {# | Jinja2 注释开始标识符。 |
comment_end_string | str | #} | Jinja2 注释结束标识符。 |
dest | path | null | 模板渲染后复制到受控节点的路径。 |
follow | bool | false | 是否跟随软链接, |
force | bool | true | 如果目标文件存在且不同,是否强制覆盖。 |
group | str | null | 设置目标文件的属组。 |
mode | str | null | 设置目标文件的权限,如 |
owner | str | null | 设置目标文件的属主。 |
src | path | null | 本地模板文件的路径。 |
validate | str | null | 在替换目标文件前执行的验证命令(如 |
- name: Template a file to /etc/file.confansible.builtin.template:src: /mytemplates/foo.j2dest: /etc/file.confowner: bingroup: wheelmode: '0644'- name: Template a file, using symbolic modes (equivalent to 0644)ansible.builtin.template:src: /mytemplates/foo.j2dest: /etc/file.confowner: bingroup: wheelmode: u=rw,g=r,o=r- name: Create a DOS-style text file from a templateansible.builtin.template:src: config.ini.j2dest: /share/windows/config.ininewline_sequence: '\r\n'- name: Copy a new sudoers file into place, after passing validation with visudoansible.builtin.template:src: /mine/sudoersdest: /etc/sudoersvalidate: /usr/sbin/visudo -cf %s- name: Update sshd configuration safely, avoid locking yourself outansible.builtin.template:src: etc/ssh/sshd_config.j2dest: /etc/ssh/sshd_configowner: rootgroup: rootmode: '0600'validate: /usr/sbin/sshd -t -f %sbackup: yes
ansible.builtin.template
要配合jinja2
使用,找时间写一下如何利用ansible.builtin.template
配合jinja2
复制文件。