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 复制文件。

相关推荐
JIngJaneIL18 分钟前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
专注VB编程开发20年38 分钟前
C#全面超越JAVA,主要还是跨平台用的人少
java·c#·.net·跨平台
南_山无梅落1 小时前
9.Python3集合(set)增删改查和推导式
java·开发语言
爱笑的眼睛111 小时前
超越MSE与交叉熵:深度解析损失函数的动态本质与高阶设计
java·人工智能·python·ai
alengan1 小时前
linux上面写python3日志服务器
linux·运维·服务器
全靠bug跑2 小时前
Spring Cloud OpenFeign 实战三部曲:快速集成 · 连接池优化 · 客户端抽取
java·spring boot·openfeign
Evan芙2 小时前
搭建nexus服务,实现本地仓库、代理仓库
java·nginx·tomcat
乂爻yiyao2 小时前
Java LTS版本重要升级特性对照表
java·开发语言
原来是好奇心2 小时前
深入Spring Boot源码(六):Actuator端点与监控机制深度解析
java·开发语言·源码·springboot