gpt4 book ai didi

linux - Ansible 将文本附加到 INI 文件特定部分的行

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:20 25 4
gpt4 key购买 nike

我想知道是否有一种方法使用 Ansible 将文本附加到文件特定部分的行尾,一个示例将阐明我想要做什么:

想象一个这样的文件:

[section01]
path = /home/section01
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada

[section02]
path = /home/section02
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada

[section03]
path = /home/section03
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada

我想在[section02]的host_allow上添加“brazil”来获取这个“新文件”

[section01]
path = /home/section01
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada

[section02]
path = /home/section02
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada,brazil

[section03]
path = /home/section03
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = mexico,usa,canada

最佳答案

正如 @Dan Farrell 在评论中提到的,您最好生成整个文件,因为部分更新可能不可靠且有问题。

但是您可以使用 ansible templates也能实现这一点。

您使用以下内容创建一个模板文件(例如file.ini)(为了简洁,删除了其他 block )。该文件将包含完整的 INI 文件内容。

[section02]
path = /home/section02
read only = yes
list = yes
uid = apache
gid = apache
hosts deny = 0.0.0.0/0.0.0.0
hosts allow = {{allow_hosts}}

然后,在您的剧本中添加一个任务来模板化此文件。

- name: Template INI file
template:
dest: "/path/to/some/file.ini"
src: file.ini
mode: 664
owner: root
group: root

实例化 playbook 时,您可以通过 extra-vars 传递允许的主机的完整列表。

ansible-playbook -i 主机 --extra-vars="allow_hosts=墨西哥、美国、加拿大、巴西"my-playbook.yml

但是,只有当您知道剧本运行时所有允许的主机时,这才对您有用。

关于linux - Ansible 将文本附加到 INI 文件特定部分的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47933480/

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