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

Ansible 是一个广泛使用的自动化工具,可以一次性管理数百个节点。Ansible 拥有许多出色的功能之一是利用 Ansible lineinfile 模块在远程节点上管理文件中的单行。

Ansible lineinfile 模块是一个在文件中执行各种操作的模块,例如替换一行、更新一行或添加特定行。

在本教程中,您将学习 Ansible lineinfile 模块是什么,它是如何工作的,以及如何使用它来管理文本文件。

先决条件

本文将逐步介绍 Ansible lineinfile 模块的教程。如果您想跟着做,请确保您已准备好以下内容:

  • An Ansible 控制主机 – 本教程将在 Ubuntu 18.04.5 LTS 机器上使用 Ansible v2.9.24。
  • 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 模块的临时命令来开始本教程。临时命令是在远程主机上测试或运行单个命令的快速方式。

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

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

  • path 表示文件位置。
  • regexp 查找文件中的正则表达式(如果有)并用 127.0.0.1 myapache(在 line 参数中指定)进行更新。
  • –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

在播放书中修改多个文本文件

使用单个临时命令来管理远程计算机上的行可能是可以的,但如果您有多个文件中的行或一个文件中的多个行需要管理,那将会很困难。不要使用临时命令,考虑在播放书中使用Ansible lineinfile模块,使用ansible-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 目录下,将带有文本“Hello This is my Apache Page”的行添加到 index.html 页面的末尾。

Ansible playbooks 是用 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 状态,表示远程主机不处于正确状态,并已修改以运行命令。 对于 TASK 具有 OK 状态的任务,表示它们不需要任何更改。

4. 接下来,使用您喜欢的 SSH 客户端 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模块是修改远程主机上文本文件的绝佳方式。该模块提供了在您的playbook中添加、删除和修改文本文件中行的绝佳方式。

您还看到哪些用例可以从Ansible的lineinfile模块中受益呢?

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