gpt4 book ai didi

regex - 使用正则表达式匹配方括号内的特定单词

转载 作者:行者123 更新时间:2023-12-01 23:36:47 25 4
gpt4 key购买 nike

我有一个文件/home/ec2-user/hosts,其中有一个名为 [sample_word_test] 的组,带有方括号,我想使用 ansible playbook 在该组下添加一个 Ip 地址。我想使用 lineinfile 正则表达式匹配 [sample_word_test] 并在该匹配组下添加 Ip 地址

以下是我的剧本代码

lineinfile:
path: /home/ec2-user/hosts
regexp: "\[.*?sample_word_test.*$\]"
line: "{{ new_server_ip }}"
backup: yes

最佳答案

非常简单的解决方案是使用 ini_file .下面的任务执行请求的操作。

- ini_file:
path: /home/ec2-user/hosts
section: sample_word_test
option: "{{ new_server_ip }}"
allow_no_value: yes
backup: yes

例如
$ cat hosts
[all]
test1
test2

[sample_word_test]
192.168.1.99

关于regex - 使用正则表达式匹配方括号内的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56901800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com