zoukankan      html  css  js  c++  java
  • Ansible_常用文件模块使用详解

    一、Ansibel常用文件模块使用详解

    1、file模块

    1️⃣:file模块常用的参数列表:

    • path       被管理文件的路径
    • state状态常用参数:
      • absent           删除目标文件
      • touch             如果目标文件不存在,则创建文件;如果存在,则更改目标文件的时间戳
      • directory        创建目录
      • hard               给目标文件创建硬链接(与src一起使用)
      • link                 给目标文件创建软链接(与src一起使用)
    • setype      设置目标文件安全上下文属性
    • owner       设备目标文件的所属主
    • group        设置目标文件的所属组
    • mode         设置文件的权限
      • mode常用的格式:文件:0644    目录:0755   
        • 或者用引号:文件:'0644'    目录:'0755'
        • 也开始指定符号模式:mode: u+rwx    或者  mode: u=r,g=w,o=x   或者:mode: u+r,g+w,o+x
    • src      指定链接文件的路径

    2️⃣:演示实例

    • 实例一:使用file模块创建一个文件,文件名file
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: create file
            file:
              path: /opt/file
              state: touch
              owner: root
              group: root
              mode: 0644
      
      [root@localhost ~]# ansible all -a 'ls -l /opt'
      client.example.com | CHANGED | rc=0 >>
      total 0
      -rw-r--r--. 1 root root 0 Sep  9 09:56 file
    • 实例二:使用file模块创建一个目录,目录名为dir
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: create dir
            file:
              path: /opt/dir
              state: directory
              mode: 0755
      
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
      dir
      [root@localhost ~]# ansible all -a 'ls -l /opt'
      client.example.com | CHANGED | rc=0 >>
      total 0
      drwxr-xr-x. 2 root root 6 Sep  9 09:59 dir
    • 实例三:使用file模块删除刚刚创建的file文件和dir目录
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: delete file
            file:
              path: /opt/file
              state: absent
      
          - name: delete dir
            file:
              path: /opt/dir
              state: absent
      
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
    • 实例四:使用file模块设置file文件的安全上下文,设置类型为:httpd_sys_content_t
      [root@localhost ~]# ansible all -a 'ls -Z /opt/'
      client.example.com | CHANGED | rc=0 >>
      unconfined_u:object_r:usr_t:s0 file
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: set httpd_sys_content_t
            file:
              path: /opt/file
              setype: httpd_sys_content_t
      
      [root@localhost ~]# ansible all -a 'ls -Z /opt/'
      client.example.com | CHANGED | rc=0 >>
      unconfined_u:object_r:httpd_sys_content_t:s0 file
    •  注意:使用此方法与使用chcon命令类似,只是临时的更改了文件的安全上下文属性,使用restorecon恢复文件安全上下文环境(restorecon -Rv
    • chcon语法详细使用方法:访问:https://man.linuxde.net/chcon

    2、fetch模块

    1️⃣:fetch模块常用的参数列表:

    • src        指定目标主机上的文件的路径(必须是文件,不能是目录)
    • dest       指定所索取的文件所要保存的目录

    2️⃣:演示实例:

    • 实例:将目标主机上的文件索取到本地
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: fetch /opt/file
            fetch:
              src: /opt/file
              dest: /opt
      
      [root@localhost ~]# ls /opt/
      client.example.com
      [root@localhost ~]# ls /opt/client.example.com/opt/
      file

    3️⃣:说明:使用fetch模块后,索取到本地目录下的文件会自动生成与目标主机的域名或IP地址的目录存放索取的文件

    3、lineinfile模块

    1️⃣:lineinfile常用的参数列表:

    • path      要修改的目标主机上的文件路径
    • line       要在目标文件中插入或替换的行(必须与state一起使用)
    • state参数常用的选项:
      • present           设置为present,如果目标文件中没有匹配的行,则添加该行;如果没有则略过
      • ansent            设置为absent,如果目标文件中没有匹配的行,则略过;如果有,则删除该行
    • create参数常用的选项:(可与state: present一起使用)
      • yes         设置为yes,如果目标文件不存在,则创建该文件,如果匹配了对应的行,则一并写入该文件
      • no           设置为no,则不创建该文件

    2️⃣:演示实例:

    • 实例一:匹配目标文件中没有的行
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: lineinfile
            lineinfile:
              path: /opt/file
              line: 'aaaaaaaaa'
              state: present
      
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      aaaaaaaaa
    • 实例二:匹配目标文件中存在的行
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      aaaaaaaaa
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: lineinfile
            lineinfile:
              path: /opt/file
              line: 'aaaaaaaaa'
              state: absent
      
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >> 
    •  实例三:使用create参数创建不存在的文件
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
      
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: lineinfile
            lineinfile:
              path: /opt/test
              line: 'aaaaaaaaa'
              owner: root
              group: root
              mode: 0644
              create: yes
      
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
      test
      [root@localhost ~]# ansible all -a 'ls -l /opt'
      client.example.com | CHANGED | rc=0 >>
      total 4
      -rw-r--r--. 1 root root 10 Sep  9 11:00 test

    4、blockinfile模块

     1️⃣:blockinfile常用参数列表:

    • path           要修改目标主机上的文件路径
    • block         要插入目标文件的文本(文本块、字符串);在block后面必须接管道符“|”
    • state参数常用的选项:
      • present            设置为present,如果目标文件中没有匹配的文本,则添加该文本;如果存在,则略过
      • absent             设置为absent,如果目标目标文件中没有匹配的文本,则略过;如果存在,则删除
    • create参数常用的选项:
      • yes         设置yes,如果path指定的目标文件不存在,则创建该文件;如果指定的文本块,则一并写到该文件中
      • no           设置no,如果path指定的目标文件不存在,则不创建

    2️⃣:演示实例:

    • 实例一:匹配不存在的文本
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: blockinfile
            blockinfile:
              path: /opt/file
              state: present
              block: |
                aaaaaaaaa
                bbbbbbbbb
                ccccccccc
      
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      # BEGIN ANSIBLE MANAGED BLOCK
      aaaaaaaaa
      bbbbbbbbb
      ccccccccc
      # END ANSIBLE MANAGED BLOCK
    • 实例二:匹配已经存在的文件
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
      # BEGIN ANSIBLE MANAGED BLOCK
      aaaaaaaaa
      bbbbbbbbb
      ccccccccc
      # END ANSIBLE MANAGED BLOCK
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: blockinfile
            blockinfile:
              path: /opt/file
              state: absent
              block: |
                aaaaaaaaa
                bbbbbbbbb
                ccccccccc
      
      [root@localhost ~]# ansible all -a 'cat /opt/file'
      client.example.com | CHANGED | rc=0 >>
    • 实例三:使用create参数创建不存在的文件
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
      
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: blockinfile
            blockinfile:
              path: /opt/test
              create: yes
              owner: root
              group: root
              mode: 0644
              block: |
                aaaaaaaaa
                bbbbbbbbb
                ccccccccc
      
      [root@localhost ~]# ansible all -a 'ls /opt'
      client.example.com | CHANGED | rc=0 >>
      test
      [root@localhost ~]# ansible all -a 'ls -l  /opt'
      client.example.com | CHANGED | rc=0 >>
      total 4
      -rw-r--r--. 1 root root 88 Sep  9 11:20 test
      [root@localhost ~]# ansible all -a 'cat /opt/test'
      client.example.com | CHANGED | rc=0 >>
      # BEGIN ANSIBLE MANAGED BLOCK
      aaaaaaaaa
      bbbbbbbbb
      ccccccccc
      # END ANSIBLE MANAGED BLOCK

    5、sefcontext模块

    1️⃣:使用sefcontext模块需要安装两个包:libselinux-pythonpolicycoreutils-python ;需要在控制节点和受管主机上都安装

    2️⃣:在RHEL8上这两个包的包名分别是:python3-libselinuxpolicycoreutils-python-utils

    3️⃣:注意:sefcontext模块更新SELinux策略中目标的默认上下文,但不更改现有文件的上下文(可以使用semanage -l 查看)

    4️⃣:sefcontextxt常用参数:

    • target            目标文件或目录的路径
    • state参数常用选项:
      • present          设置present,则修改为指定的类型
      • absent            设置absnet ,则删除指定的类型
    • setype          指定目标Selinux类型

    5️⃣:演示实例:

    •  首先安装python3-libselinuxpolicycoreutils-python-utils
      [root@client ~]# yum install -y python3-libselinux
      [root@client ~]# yum install -y policycoreutils-python-utils
      
       //在受管主机上安装semanage
      [root@localhost ~]# yum install -y policycoreutils-python-utils
    • 实例一:在/opt下创建share目录,修改默认的安全上下文环境
      //在受管主机的/opt下创建share目录
      [root@client ~]# mkdir /opt/share
      
      
       //在受管主机上查看share的安全上下文
      [root@client ~]# ls -Z /opt/
      unconfined_u:object_r:usr_t:s0 share
      
      
      //在受管主机上查看该share目录的默认Selinux安全上下文
      [root@client ~]# semanage fcontext -l | grep /opt/share/               //回车后并没有安全上下文,以为是我们手动创建的,不是系统默认的安全上下文
      
      
      //使用playbook设施/opt/share的安全上下文
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: sefcontext
            sefcontext:
              path: /opt/share
              setype: httpd_sys_content_t
              state: present
      
       
      //执行play后,在受管主机上查看/opt/share的安全上下文
      [root@client ~]# semanage fcontext -l | grep /opt/share
      /opt/share       all files          system_u:object_r:httpd_sys_content_t:s0                //此时已经创建了默认的Selinu的安全上下文
      
      
      //再次查看share的安全上下文
      [root@client ~]# ls -Z /opt/
      unconfined_u:object_r:usr_t:s0 share                //发现share的安全上下文没有发生改变
      
      
      //使用restorecon命令恢复share的安全上下文
      [root@client ~]# restorecon -Rv /opt/share/
      Relabeled /opt/share from unconfined_u:object_r:usr_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0                   //-R:递归恢复 -v:显示操作过程
      
      
       //再次查看share的安全上下文
      [root@client ~]# ls -Z /opt/
      unconfined_u:object_r:httpd_sys_content_t:s0 share                 //此时share的安全上下文已经正常
      
      
      //在share目录里面创建文件或目录,是否继承share的安全上下文属性
      [root@client ~]# mkdir /opt/share/dir
      [root@client ~]# touch /opt/share/file
      [root@client ~]# ll -Z /opt/share/
      total 0
      drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Sep  9 13:18 dir
      -rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 Sep  9 13:18 file
       //由此可见,在share里面创建的文件或目录都会继承share的安全上下文属性
      

        

    •  实例二:删除默认的安全上下文
      //在受管主机上查看share的Selinux默认安全上下文
      [root@client ~]# semanage fcontext -l | grep /opt/share
      /opt/share                                         all files          system_u:object_r:httpd_sys_content_t:s0 
      
      
       //使用playbook删除share的默认安全上下文 
      [root@localhost ~]# cat playbook.yaml 
      ---
      - hosts: all
        gather_facts: no
        tasks:
          - name: sefcontext
            sefcontext:
              path: /opt/share
              setype: httpd_sys_content_t
              state: absent
      
       //执行play后在受管主机上查看share的Selinux默认安全上下文
      [root@client ~]# semanage fcontext -l | grep /opt/share         //回车后,什么也没有,说明已经删除了
  • 相关阅读:
    Apache Commons IO之FileUtils的常用方法
    Java之字节数组和字符串的转换问题
    Java之高级IO,Properties
    Java之IO流(字节流,字符流)
    Java之File与递归
    Java之线程池和Lambda表达式
    java之初学线程
    Java之初学异常
    Java之使用链表实现队列
    请求参数的绑定
  • 原文地址:https://www.cnblogs.com/itwangqiang/p/13637566.html
Copyright © 2011-2022 走看看