【Ansible】将文件部署到受管主机1:文件模块

文件模块与 Linux 文件管理相关的大部分常用模块都在ansible.builtin collection 中随 ansible-core 一起提供

ansible.builtin 中的常用⽂件模块:

ansible.builtin 中的常用⽂件模块 (续) :

确保受管主机上存在⽂件:

复制代码
- name: touch a file and set permissions
  ansible.builtin.file:
    path: /path/to/file
    owner: uaer1
    group: grooup1
    mode:0640
    state: touch

修改文件属性:

复制代码
- name: SELinux type is set to samba_share_t
  ansible.builtin.file:
    path: /path/to/samba_file
    setype: samba_share_file

在受管主机上复制和编辑⽂件:

默认谁执行,文件属于谁,可指定:

Ansible.builtin.copy模块用于将控制节点上Ansible工作目录中的文件复制到选定的受管主机

复制代码
- name:copy a file to managed hosts
  ansible.builtin.copy:
    src: file
    dest: /path/to/file

Ansible.builtin.fetch模块用于从受管主机提取文件

复制代码
- name: Retrieve SSH key from referrence hosts
  ansible.builtin.fetch:
    src: "/home/{{ uaer }}/.ssh/id_rsa.pub"
    dest: "files/keys/{{ user }}.pub"

Ansible.builtin.lineinfile模块用于确保现有文件中存在特定的单行脚本

复制代码
- name: add a line of text to file
  ansible.builtin.lineinfile:
    path: /path/to/file
    line: 'add this line to file'
    state: present

Ansibile.builtin.blockinfile模块用于将文本块添加到现有文件

复制代码
- name: add additional lines to file
  ansible.builtin.blockinfile:    
    path: /path/to/file
    block:|
      first line
      second line
    state: present

从受管主机中删除⽂件:

使用ansible.builtn.file模块配合state: absent参数

复制代码
- name: make a file does not xeist on managed host
  ansible.builtin.file:
    dest: /path/to/file
    state: absent

检索受管主机上的⽂件状态

使用ansible.builtin.stat模块

复制代码
- name: verify the chedksum of a file
  ansible.builtin.stat:
    path: /path/to/file
    checksum_algorithm: md5
  register: result

- ansible.builtin.debug
    msg: "the checksum of the file is {{ result.stat.checksum }}"

同步控制节点和受管主机之间的⽂件:

使用ansible.posix.synchronnize模块,类似于rsync命令

复制代码
- name: synchronize local file to remote files
  ansible.posix.synchoronize:
    src:file
    dest: /path/to/file
相关推荐
我爱学习好爱好爱4 小时前
Ansible 常用模块详解:cron、archive、unarchive实战
linux·服务器·ansible
我爱学习好爱好爱11 小时前
Ansible 详解:group模块、vars_files变量、user模块实战
linux·运维·ansible
我爱学习好爱好爱11 小时前
Ansible 自动化部署全栈项目(Spring Boot + Vue + MySQL + Redis)实战(Rockylinux9.6)
spring boot·自动化·ansible
我爱学习好爱好爱1 天前
Ansible 常用模块详解:lineinfile、replace、get_url实战
linux·python·ansible
我爱学习好爱好爱1 天前
Ansible 环境搭建
linux·运维·ansible
淼淼爱喝水1 天前
openEuler 系统下 Ansible 一键安装教程(保姆级)
运维·ansible·openeuler
我爱学习好爱好爱2 天前
Ansible 常用模块详解:hostname、selinux 、file实战
前端·chrome·ansible
我爱学习好爱好爱2 天前
Ansible 常用模块详解:yum、service/systemd、copy实战
linux·服务器·ansible
淼淼爱喝水2 天前
Ansible 配置与环境搭建超全教程(自动化运维基础)
运维·自动化·ansible
我爱学习好爱好爱3 天前
Ansible 入门:ad-hoc 临时命令与常用模块
linux·服务器·ansible