一、描述常用文件模块

1、常用文件模块

模块名称 模块说明
blockinfile 插入、更新或删除由可自定义标记线包围的多行文本块
copy 将文件从本地或远程计算机复制到受管主机上的某个位置。
类似于file模块,copy模块还可以设置文件属性,包括SELinux上下文件。
fetch 此模块的作用和copy模块类似,但以相反方式工作。此模块用于从远程计算机获取文件到控制节点,
并将它们存储在按主机名组织的文件树中。
file 设置权限、所有权、SELinux上下文以及常规文件、符号链接、硬链接和目录的时间戳等属性。
此模块还可以创建或删除常规文件、符号链接、硬链接和目录。其他多个与文件相关的
模块支持与file模块相同的属性设置选项,包括copy模块。
lineinfile 确保特定行位于某文件中,或使用反向引用正则表达式来替换现有行。
此模块主要在用户想要更改文件的某一行时使用。
stat 检索文件的状态信息,类似于Linux中的stat命令。
synchronize 围绕rsync命令的一个打包程序,可加快和简化常见任务。
synchronize模块无法提供对rsync命令的完整功能的访问权限,但确实最常见的调用更容易实施。
用户可能仍需通过run command模块直接调用rsync命令。

2、Ansible常用模块使用详解

二、file模块的自动化演示

1、使用file模块确保受管主机上存在文件

1️⃣:使用file模块处理受管主机上的文件。其工作方式与touch命令类似,如果不存在则创建一个空文件,如果存在,则更新其修改时间

  • 演示实例:

     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: look the file is exit
    file:
    path: /root/file
    mode: 0755
    state: touch //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [look the file is exit] **********************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

2、在受管主机上删除文件

1️⃣:从受管主机中删除文件的基本示例是使用file模块和state: absent参数

  • 演示实例:

     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: look the file is exit
    file:
    path: /root/file
    state: absent //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [look the file is exit] **********************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

3、在受管主机上复制和编辑文件

1️⃣:使用fetch模块将受管主机上的文件索取到控制节点上进行检索(fetch:拿来、取来、提取)

  • 演示实例:

     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    fetch:
    src: /root/file
    dest: files //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 //查看file下是否有该文件
    [root@localhost project]# ls files/
    client.example.com //fetch模块会自动生成以受管主机域名的目录 [root@localhost project]# cat files/client.example.com/root/file
    This is test file

2️⃣:使用lineinfile模块,确保件中主机单个文件中存在特定的单行文本

  • 演示实例:

     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    lineinfile:
    path: /etc/selinux/config
    line: SELINUX=enforcing
    state: present //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    ok: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    //OK表示存在该行,如果是changed则表示不存在
  • 注意:state:{present、absent}:present表示所列出的行在对应文本里边,如果存在,则报OK,如果不存在,则添加该行;absent表示如果存在对应的列,则删除该列,如果不存在,则返回OK

3️⃣:使用blockinfile模块将所列出的文件块添加到受管主机现有文件中

  • 演示实例:

       //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    blockinfile:
    path: /root/file
    block: |
    this is one line
    this is two line
    tjis is three line
    state: present //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 //查看受管主机上file文件
    [root@client ~]# cat file
    This is test file
    # BEGIN ANSIBLE MANAGED BLOCK
    this is one line
    this is two line
    tjis is three line
    # END ANSIBLE MANAGED BLOCK
  • 注意:state:{present、absent}:present表示在现有文件中,如果存在,则报OK;如果不存在,则添加该文件块;absent表示如果存在该文件块,则删除该文件块;如果不存在,则报OK、

4、使用sefcontext模块更新selinux策略

1️⃣:设置文件上下文时,file模块的行为与chcon类似;使用file设置上下文后,用户可以使用system模块集合中的sefcontext来更新SELinux策略,如semanage fcontext

2️⃣:sefcontext模块更新SELinux策略中目标的默认上下文(一般对目录使用,对文件使用file模块直接修改属性即可),但不更改现有文件的上下文

  • 演示实例:

    • 首先在控制节点和受管主机上安装两个模块:python3-libselinux和policycoreutils-python-utils这两个模块

      [root@localhost ~]# yum install -y python3-libselinux
      [root@localhost ~]# yum install -y policycoreutils-python-utils
  • 演示:
     //查看受管主机上的file文件属性
    [root@client ~]# ls -Z /var/www/html/file
    unconfined_u:object_r:admin_home_t:s0 /var/www/html/file //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    sefcontext:
    path: /var/www/html/file
    setype: httpd_sys_content_t
    state: present //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 //查看受管主机上的file文件属性
    [root@client html]# ll -Z file
    -rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 Sep 8 17:38 file

5、检索受管主机上的文件状态

1️⃣:stat模块检索文件的事实,类似于Linux中的stat命令;参数提供检索文件属性、确定文件检验和等功能

2️⃣:stat模块返回一个包含文件状态数据的值的散列字典,允许用户使用单独的变量引用各条信息

  • 演示实例:注册stat模块的结果,然后显示它检查的文件的MD5检验和

     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    stat:
    path: /root/file
    checksum_algorithm: md5
    register: result - debug:
    msg: "{{ result['stat']['checksum'] }}" //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    ok: [client.example.com] TASK [debug] **************************************************************************************************************************************************************
    ok: [client.example.com] => {
    "msg": "d41d8cd98f00b204e9800998ecf8427e"
    } PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

6、同步控制节点和受管主机之间的文件

1️⃣:synchronize模块是一个围绕rsync工具的打包程序,它简化了playbook中的常见文件管理任务

2️⃣:rsync工具必须同时安装在本机和远程主机上

3️⃣:默认情况下,在使用synchronize模块时,“本地主机”是同步任务所源自的主机(通常是控制节点),而“目标主机”是synchronize连接到的主机

  • 演示实例:

    • 首先下控制节点和受管主机上安装rsync工具

      [root@localhost ~]# yum install -y rsync
      [root@localhost ~]# yum install -y rsync
  • 演示:
     //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    synchronize:
    src: files/test
    dest: /root/file //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

7、使用file模块修改文件的上下文属性

1️⃣:使用file模块还可以确保新的或现有的文件具有正确的权限和SELinux类型

  • 演示实例:

    //查看受管主机上文件默认的上下文属性
    [root@client ~]# ls -Z /root/file
    unconfined_u:object_r:admin_home_t:s0 /root/file
    //可以看到默认对类型是admin_home_t,我们需要换成httpd_sys_content_t类型 //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    gather_facts: no
    tasks:
    - name: test
    file:
    path: /root/file
    setype: httpd_sys_content_t //执行play
    [root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [test] ***************************************************************************************************************************************************************
    changed: [client.example.com] PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 //执行成功够在受管主机上执行restoncon命令恢复file文件的安全上下文属性 //再次查看受管主机上文件上下文属性
    [root@client ~]# ls -Z /root/file
    unconfined_u:object_r:httpd_sys_content_t:s0 /root/file
    //此时发现file文件的上下文属性已经发生改变

Ansible_使用文件模块将修改文件复制到受管主机的更多相关文章

  1. 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图

    [源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...

  2. discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人

    x3.x数据字典 http://faq.comsenz.com/library/database/x3/x3_index.htm 先从根目录开始,根目录文件一般都是入口,即执行具体功能的代码一般不在这 ...

  3. 文件IO操作..修改文件的只读属性

    文件的IO操作..很多同行的IO工具类都是直接写..但是如果文件有只读属性的话..则会写入失败..所以附加了一个只读的判断和修改.. 代码如下: /// <summary> /// 创建文 ...

  4. [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表

    写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...

  5. python笔记(三)---文件读写、修改文件内容、处理json、函数

    文件读写(一) #r 只读,打开文件不存在的话,会报错 #w 只写,会清空原来文件的内容 #a 追加写,不会请求,打开的文件不存在的话,也会帮你新建的一个文件 print(f.read()) #获取到 ...

  6. git 提交本地文件,删除文件夹,修改文件等

    1. 下载git工具包 链接: https://git-scm.com/download/win 2. 右键打开git bash 登陆到自己的github账户 $ git config --globa ...

  7. 关于Npoi+excel文件读取,修改文件内容的处理方式

    因最近有需求场景,实现对文件的读写操作,又不单独生成新的文件,对于源文件的修改,做了一个简单实现,如下↓ // 要操作的excel文件路径 string fileName = Server.MapPa ...

  8. Linux下查看文件权限、修改文件权限的方法

    查看权限命令查看目录的相关权限可以采用命令ls -lD,或者直接用ls -la 如 ls -l www.jb51.net  //这里表示查看www.jb51.net目录 修改权限命令 chmod 77 ...

  9. linux c 判断文件存在,遍历文件,随机修改文件内容

    #include<stdio.h> #include<stdlib.h> #include<time.h> #include<assert.h> #in ...

随机推荐

  1. 《进击吧!Blazor!》系列入门教程 第一章 8.部署

    <进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门教程视频,此教程能让一个从未接触过Blazor的程序员掌握开发Blazor应用的能力. 视频地址:https://s ...

  2. Dynamics CRM存放选项集记录的表

    我们在做一些自定义查询的时候会去查询选项集字段的值,但是实体的选项集字段是一个整型字段,直接查询并不能找到对应的选项集的显示内容.所以我们需要找到存放选项集键值对的表来做关联查询找到我们想要的值. D ...

  3. 【转载】C# get 与set的一些说明

    转载 在面向对象编程(OOP)中,是不允许外界直接对类的成员变量直接访问的,既然不能访问,那定义这些成员变量还有什么意义呢?所以C#中就要用set和get方法来访问私有成员变量,它们相当于外界访问对象 ...

  4. Android-SQLite的介绍 以及四个基本操作~

    在Android 开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一 ...

  5. 【学习底层原理系列】重读spring源码3-加载beanDefinition的方法obtainFreshBeanFactory

    obtainFreshBeanFactory()方法概述 定义BeanFactory,并加载以下两种bean的定义,装配到BeanFactory: 1.配置文件中定义的bean 2.通过<con ...

  6. k8s configmap 挂载配置文件

    转自https://blog.csdn.net/weixin_34102807/article/details/85965725 1.新建ConfigMap apiVersion: v1 kind: ...

  7. aws eks ebs StorageClass PersistentVolume PersistentVolumeClaim

    aws EBS 提供存储资源 Amazon EBS CSI 驱动程序的安装,请参考https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/ebs- ...

  8. JVM JIT动态编译

    一.概述 1.1 基本概念 a. 动态编译(dynamic compilation)指的是"在运行时进行编译":与之相对的是事前编译(ahead-of-time compilati ...

  9. vagrant构建centos虚拟环境

    vagrant搭建centos 什么是vagrant 如何使用 1.构建本地的目录 2.官方下载对应的镜像文件,官方下载地址 3.导入刚刚下载的镜像(box文件) 4.初始化 5.修改Vagrantf ...

  10. 10276 - Hanoi Tower Troubles Again!(思维,模拟)

    People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...