如何使用Ansible lineinfile模塊管理文本文件

Ansible是一個廣泛使用的自動化工具,可以一次管理數百個節點。Ansible擁有許多優秀的功能之一就是使用Ansible lineinfile模塊在遠程節點上管理單行文件。

Ansible lineinfile模塊是一個在文件中執行各種操作的模塊,例如替換行、更新行或添加特定行。

在本教程中,您將學習什麼是Ansible lineinfile模塊,它是如何工作的,以及如何使用它來管理文本文件。

先決條件

本文將逐步介紹Ansible lineinfile模塊的教程。如果您想跟隨操作,請確保您已經準備好以下內容:

  • A remote computer to run commands. You’ll need an inventory file set up and one or more hosts already configured to run Ansible command and playbooks on. The remote Linux computer will be called myserver, and the tutorial will use an inventory group called web.
  • 如果您想按照教程完全操作,遠程計算機上應已安裝Apache。

修改文本文件使用 Ansible 的 lineinfile 模块。

让我们通过运行 Ansible lineinfile 模块使用 ad hoc 命令开始这个教程。Ad hoc 命令是在远程主机上测试或运行单个命令的快速方式。

登录到您的 Ansible 控制器并运行以下命令。此命令使用 lineinfile 模块(-m)连接到 Web 机器并传递一个参数(-a),即要执行的命令。

在这个示例中,lineinfile 模块通过将 IP 地址 127.0.0.1myapache 在 /etc/hosts 文件中进行映射来更新本地主机条目。通过将 127.0.0.1myapache 进行映射,您可以在 HTTP://myapache:80 上本地访问 apache 测试页面

  • path 表示文件位置。
  • regexp 查找文件中的正则表达式,如果有的话,将其更新为在 line 参数中指定的 127.0.0.1 myapache
  • –become 标志允许您以特权用户身份运行命令。
  • web 是一个包含所有服务器的清单组。
  • ansible.builtin.lineinfile 或简称 lineinfile 是模块名称。
ansible web -m ansible.builtin.lineinfile -a "path=/etc/hosts regexp='^127\.0\.0\.1'  line='127.0.0.1 myapache' state=present" --become

执行命令后,您应该看到一个CHANGED消息,确认该行已成功更新到远程主机。

Running the ad hoc command with ansible lineinfile module

使用SSH客戶端登錄到遠程節點,使用cat命令驗證/etc/hosts文件是否已更新為新值。

如下所示,localhost條目已成功在遠程機器上更新為127.0.0.1 myapache

Verifying the host file on remote machine

在Playbook中修改多個文本文件

使用單個即席命令來管理遠程機器上的行可能沒有問題,但如果您需要管理多個文件中的行或文件中的多行,這將變得困難。不要使用即席命令,請考慮在Playbook中使用Ansible lineinfile模塊,使用ansible-playbook命令。

現在,讓我們學習如何在Playbook中使用Ansible lineinfile模塊並修改幾行。

假設您已經登錄到Ansible控制器主機:

1. 在您的主目錄中創建一個名為ansible_lineinfile_module_demo的目錄。這個目錄將包含您將用於調用lineinfile模塊的playbook

mkdir ~/ansible_lineinfile_module_demo
cd ~/ansible_lineinfile_module_demo

2. 在〜/ansible_lineinfile_module_demo目錄中創建另一個名為my_playbook.yml的文件,並粘貼以下 YAML playbook 內容。此 playbook 包含多個任務,使用 Ansible lineinfile 模塊來管理遠程機器上 Apache 不同配置文件的行。

以下 playbook 包含以下任務:

1. 檢查是否在/etc/sudoers文件中存在ADMIN;如果沒有,則添加它。

2. 確保 Apache 默認在/etc/apache2/ports.conf文件中監聽8080;如果找到任何其他端口,lineinfile 模塊將其更新為端口8080。同樣在/etc/apache2/apache2.conf文件中,它將MaxKeepAliveRequests更新為1000,並將KeepAliveTimeout更新為100。

3. 在遠程服務器的/var/www/html目錄下的index.html頁面末尾添加包含文本“Hello This is my Apache Page”的行。

Ansible playbook 是用 YAML 編寫的。要了解更多關於 YAML 的信息,請點擊這裡

---
- name: Ansible lineinfile module example
# 定義 Ansible lineinfile 模組將生效的遠端伺服器
  hosts: web
  remote_user: ubuntu   # 使用遠端主機為 ubuntu
  become: true
  tasks:

# (任務-1)檢查 Sudoers 檔案是否允許管理員執行所有操作
    - name: Validate the sudoers file before saving
      ansible.builtin.lineinfile:
         path: /etc/sudoers
         state: present
         regexp: '^%ADMIN ALL='
         line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'

# (任務-2)將 Apache 默認端口更新為 8080
    - name: Ensure the default Apache port is 8080
      ansible.builtin.lineinfile:
         path: /etc/apache2/ports.conf
         regexp: '^Listen '
         insertafter: '^#Listen '
         line: Listen 8080

# (任務-3)在html頁面末尾添加一行文字,Hello This is my Apache Page
    - name: Add a line to a file if the file does not exist
      ansible.builtin.lineinfile:
         path: /var/www/html/index.html
         line: Hello This is my Apache Page
         create: yes

# (任務-4)檢查 Sudoers 檔案是否允許管理員執行所有操作
    - name: Ensure MaxKeepAliveRequests is set to greater than 100
      ansible.builtin.lineinfile:
         path: /etc/apache2/apache2.conf
         regexp: '^MaxKeepAliveRequests'
         line: MaxKeepAliveRequests=1000

# (任務-5)檢查 Sudoers 檔案是否允許管理員執行所有操作
    - name: Ensure KeepAliveTimeout is set to greater than 50
      ansible.builtin.lineinfile:
         path: /etc/apache2/apache2.conf
         regexp: '^KeepAliveTimeout'
         line: KeepAliveTimeout=100

3. 現在,呼叫 playbook 並使用 ansible-playbook 命令在遠端主機上添加或更新 playbook 中定義的所有行。

ansible-playbook my_playbook.yml 
Invoking the ansible playbook

下面,您可以看到 TASK 的狀態為 changed, 表示遠端主機不在正確狀態並已修改以執行該命令。對於具有 OK 狀態的 TASK,顯示它們不需要任何更改。

4. 接下來,使用您喜歡的 SSH 客戶端登入遠端主機。

5. 最後,使用 cat 命令驗證在遠端主機上是否更新或添加了 my_playbook.yml 中定義的所有行。

# 驗證管理員是否具有完整權限,如果沒有則添加
cat /etc/sudoers
# 驗證MaxKeepAliveRequests和KeepAliveTimeout是否已更新為分別為1000和100。
cat /etc/apache2/apache.config | grep Alive
# 驗證Apache是否在端口8080上聽取
cat /etc/apache2/ports.config

以下截圖確認admin已添加到sudoers文件中。

Verifying the sudoers file

同樣,以下截圖確認Apache默認在端口8080上聽取。

Verifying the ports for in the config file

最後,驗證MaxKeepAliveRequestsKeepAliveTimeout是否已分別更新為1000和100。

Verifying the MaxKeepAliveRequests and KeepAliveTimeout in the config file

結論

Ansible的lineinfile模塊是在遠程主機上修改文本文件的好方法。該模塊提供了在您的playbooks中添加、刪除和修改文本文件中的行的好方法。

您認為還有哪些用例可以從Ansible的lineinfile模塊中受益?

Source:
https://adamtheautomator.com/ansible-lineinfile/