Ansible模块——从控制节点向目标主机复制文件!

复制文件或字符串到被控主机的指定位置

ansible.builtin.copy:复制文件到被控主机指定位置,也可以将字符串写入文件(覆盖)。

选项 类型 默认值 描述
attributes str null 设置目标文件的额外属性,如 immutable 等。
backup bool false 如果目标存在,操作前创建备份文件。
checksum str null 用来验证源文件内容的一致性的 SHA1 校验值。
content str null 直接指定要写入文件的文本内容(不从 src 读取)。
decrypt bool true 如果 src 是加密的 vault 文件,是否解密。
dest path null 目标路径 ,必须指定。
directory_mode str null 如果递归复制目录时(目标目录不存在),目录使用的权限模式。
follow bool false 如果目标中存在文件系统链接,则应遵循该链接。
force bool true 如果目标存在且不同,是否强制覆盖。
group str null 设置目标文件的属组。
local_follow bool false 控制在 local(控制机)上的源文件是否跟随符号链接。
mode str null 设置目标文件权限(比如 06440755)。
owner str null 设置目标文件的属主。
remote_src bool no true 时表示 src 是目标机器上的路径,不从控制机复制。
selevel str null 设置 SELinux 的 level 属性。通常不需要。
serole str null 设置 SELinux 的 role 属性。
setype str null 设置 SELinux 的 type 属性。
seuser str null 设置 SELinux 的 user 属性。
src path null 源文件路径(在控制机或者远程主机,根据 remote_src)。
unsafe_writes bool false 允许以不安全的方式写入文件,提升性能(但可能有风险,比如写一半被打断)。
validate str null 在复制文件之前执行指定的验证命令,验证命令的返回值决定是否继续执行文件复制。如果命令返回非零状态,复制操作将失败。(s% 表示源文件,命令不支持管道符、重定向等 Shell 高级功能)

常用选项:

选项 类型 默认值 描述
backup bool false 如果目标存在,操作前创建备份文件。
content str null 直接指定要写入文件的文本内容(不从 src 读取)。
dest path null 目标路径 ,必须指定。
directory_mode str null 如果递归复制目录时(目标目录不存在),目录使用的权限模式。
follow bool false 如果目标中存在文件系统链接,则应遵循该链接。
force bool true 如果目标存在且不同,是否强制覆盖。
group str null 设置目标文件的属组。
local_follow bool false 控制在 local(控制机)上的源文件是否跟随符号链接。
mode str null 设置目标文件权限(比如 06440755)。
owner str null 设置目标文件的属主。
remote_src bool false true 时表示 src 是目标机器上的路径,不从控制机复制。
src path null 源文件路径(在控制机或者远程主机,根据 remote_src)。
validate str null 在复制文件之前执行指定的验证命令,验证命令的返回值决定是否继续执行文件复制。如果命令返回非零状态,复制操作将失败。(s% 表示源文件,命令不支持管道符、重定向等 Shell 高级功能)
复制代码
- name: Copy file with owner and permissions
  ansible.builtin.copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

- name: Another symbolic mode example, adding some permissions and removing others
  ansible.builtin.copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 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 version
  ansible.builtin.copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: '0644'
    backup: yes

- name: Copy a "sudoers" file on the remote machine for editing
  ansible.builtin.copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -csf %s

- name: Copy using inline content
  ansible.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.conf
  ansible.builtin.copy:
    src: /etc/foo.conf
    dest: /path/to/link  # link to /path/to/file
    follow: yes

- name: If follow=no, /path/to/link will become a file and be overwritten by contents of foo.conf
  ansible.builtin.copy:
    src: /etc/foo.conf
    dest: /path/to/link  # link to /path/to/file
    follow: no

复制模板文件到被控节点指定位置

ansible.builtin.template:复制 Jinja2 模板文件到被控节点指定位置。

参数名 类型 默认值 描述
attributes str null 设置目标文件的文件属性字符串,如 +x(类似 chmod 的符号格式)。
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 是否跟随软链接,false 表示源文件替换软链接,true 表示源文件替换软链接指向的文件。
force bool true 如果目标文件存在且不同,是否强制覆盖。
group str null 设置目标文件的属组。
lstrip_blocks bool false 去除 Jinja2 块标签前的空格。
mode str null 设置目标文件的权限,如 "0644"
newline_sequence str \n 设置模板渲染后的换行符(\n\r\r\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 本地模板文件的路径(相对于 templates/ 目录)。
trim_blocks bool false 控制是否移除 Jinja2 块之间的换行符。
unsafe_writes bool false 避免临时文件写法,某些情况下可以避免某些文件系统的问题。
validate str null 在替换目标文件前执行的验证命令(如 nginx -t -c %s)。
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 是否跟随软链接,false 表示源文件替换软链接,true 表示源文件替换软链接指向的文件。
force bool true 如果目标文件存在且不同,是否强制覆盖。
group str null 设置目标文件的属组。
mode str null 设置目标文件的权限,如 "0644"
owner str null 设置目标文件的属主。
src path null 本地模板文件的路径。
validate str null 在替换目标文件前执行的验证命令(如 nginx -t -c %s)。
复制代码
- name: Template a file to /etc/file.conf
  ansible.builtin.template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

- name: Template a file, using symbolic modes (equivalent to 0644)
  ansible.builtin.template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: u=rw,g=r,o=r

- name: Create a DOS-style text file from a template
  ansible.builtin.template:
    src: config.ini.j2
    dest: /share/windows/config.ini
    newline_sequence: '\r\n'

- name: Copy a new sudoers file into place, after passing validation with visudo
  ansible.builtin.template:
    src: /mine/sudoers
    dest: /etc/sudoers
    validate: /usr/sbin/visudo -cf %s

- name: Update sshd configuration safely, avoid locking yourself out
  ansible.builtin.template:
    src: etc/ssh/sshd_config.j2
    dest: /etc/ssh/sshd_config
    owner: root
    group: root
    mode: '0600'
    validate: /usr/sbin/sshd -t -f %s
    backup: yes

ansible.builtin.template 要配合 jinja2 使用,找时间写一下如何利用 ansible.builtin.template 配合 jinja2 复制文件。

相关推荐
转转技术团队4 分钟前
二奢仓店的静默打印代理实现
java·后端
钢铁男儿22 分钟前
C# 接口(什么是接口)
java·数据库·c#
丶小鱼丶35 分钟前
排序算法之【归并排序】
java·排序算法
上上迁37 分钟前
分布式生成 ID 策略的演进和最佳实践,含springBoot 实现(Java版本)
java·spring boot·分布式
永日4567038 分钟前
学习日记-spring-day42-7.7
java·学习·spring
龙谷情Sinoam1 小时前
扩展若依@Excel注解,使其对字段的控制是否导出更加便捷
java
二十雨辰1 小时前
[尚庭公寓]07-Knife快速入门
java·开发语言·spring
掉鱼的猫1 小时前
Java MCP 实战:构建跨进程与远程的工具服务
java·openai·mcp
世事如云有卷舒2 小时前
Ubunt20.04搭建GitLab服务器,并借助cpolar实现公网访问
linux·服务器·gitlab
我爱Jack2 小时前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法