Ansible_使用文件模塊將修改文件復制到受管主機


一、描述常用文件模塊

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-libselinuxpolicycoreutils-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文件的上下文屬性已經發生改變

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM