Ansible 有一个 ‘replace’、’blockinfile’ 和 ‘lineinfile’ 模块用于处理文件中的行。 它们中的每一个都用于特定目的。 选择其中之一的选择视情况而定。
我们将涵盖哪些内容?
在这篇文章中,我们将探讨如何使用 Ansible 替换文件中的任何字符串。 Ansible 有一个用于相同目的的“ansible.builtin.replace”模块。 让我们探索下面的替换模块。
Replace 模块的一般语法如下:
- name: Name of the task ansible.builtin.replace: path: path of the file that needed to be changed regexp: regex expression for old string to be replaced replace: new string that needs to be added to file
让我们看看一些 example 下面的剧本。 下面的剧本将检查提供的路径中的主机文件,并将旧主机名 (managed1.anslab.com) 替换为新主机名 (linuxhint.com):
--- - hosts: managed1(Name of your target node) gather_facts: no become: true tasks: - name: Replace module demo ansible.builtin.replace: path: /etc/hosts regexp: '(\s+)managed1\.anslab\.com(\s+.*)?$' replace: '\1linuxhint.com\2'
您可以在下面看到此剧本的输出。 它将文件中每次出现的“managed1.anslab.com”替换为“linuxhint.com”。
我在这里使用正则表达式。 正则表达式有助于匹配多个字符串或单个字符串。 在此处了解有关 python 正则表达式的更多信息 https://docs.python.org/3/library/re.html。
现在,让我们探讨一下这个模块的其他参数:
1. ‘之后’
当我们将“after”参数与“replace”一起使用时,它就是内容替换实际开始的“after”参数,并一直持续到文件末尾。
为了更清楚,让我们看下面的剧本。 在这里,我们将“after”参数值设置为“PubkeyAuthentication”。 遇到“PubkeyAuthentication”字符串后,它会将“yes”替换为“no”。
句法:
- name: Syntax for Replace module with the ‘after’ expression. ansible.builtin.replace: path: path of the file after: word after which replacement starts regexp: regular expression to match the word that needs to be changed replace: string that will replace old word
在下面的剧本中,观察“after”表达式的用法:
--- - hosts: managed1 (Name of your target node) gather_facts: no become: true tasks: - name: Replace module demo ansible.builtin.replace: path: /etc/ssh/sshd_config regexp: 'yes' after: 'PubkeyAuthentication' replace: 'no'
如果您检查下面的输出,您可能会注意到字符串“PubkeyAuthentication”之后。 字符串“yes”现在被替换为“no”。 我正在使用 sed 从文件中获取一行以便于查看。
2.‘之前’
“之前”与“之后”相反。 它将替换为“之前”参数指定的每个匹配字符串。
句法:
- name: Syntax for Replace module with the ‘before’ expression. ansible.builtin.replace: path: path of the file that needs to be updated before: before this word, it will match the regex and replace it with new word regexp: regular expression to match the word to be changed replace: string that will replace the old word
让我们看一个 example 同样的剧本。 下面的剧本使用“之前”关键字:
--- - hosts: managed1(Name of your target node) gather_facts: no become: true tasks: - name: Replace module demo ansible.builtin.replace: path: /etc/hosts before: 'managed1' regexp: 'linuxhint.com' replace: 'linuxways.net'
在上面的剧本中,在单词“managed1”之前,如果有任何字符串,如“linuxhint.com”,为了简单起见,它将被替换为“linuxways.net”。 在这里,我没有使用任何复杂的正则表达式。 您可以在上面的屏幕截图中看到运行输出前后的文件。
3.“之前和之后”
您还可以同时使用 before 和 after 参数来替换其间的单词。
句法:
- name: Syntax for Replace module with the ‘before’ and ‘after’ expressions. ansible.builtin.replace: path: path of the file after: after this word the word will be replaced before: before this word the word will be replaced regexp: regular expression to match the word to be replaced replace: string that will replace old word
现在,让我们看一个简单的 example 下面的剧本展示了之前和之后。
--- - hosts: managed1 gather_facts: no become: true tasks: - name: Replace module demo ansible.builtin.replace: path: /etc/hosts after: '127.0.2.1' before: 'managed1' regexp: 'linuxways.net' replace: 'tecofers.com'
在上面的剧本中,你可以清楚地观察到“managed1”之前和“127.0.2.1”字符串之后的字符串被替换为字符串“tecofers.com”。
更进一步,这个模块还有一些更有趣的属性,例如 example 备份.
4.“备份”
‘backup’ 属性有两个选项,可以设置为 ‘yes’ 或 ‘no’。 如果将 backup 设置为 yes,它将创建原始文件的备份文件及其时间戳。 所以,如果你弄乱了原始文件,你可以参考备份文件(建议用于生产系统)。
结论
在本指南中,我们看到了 Ansible 中“替换”模块用于替换文件中的字符串的用法。 此外,您可以参考官方 ansible 文档以了解此模块附带的更多参数。