Ansible将文本附加到INI文件的某些部分中

时间:2017-12-21 22:19:59

标签: linux ansible

我想知道是否有一种方法可以使用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

1 个答案:

答案 0 :(得分:1)

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

但是,您也可以使用ansible templates来完成此操作。

您可以使用以下内容创建模板文件(例如MBEDTLS_DEBUG_C)(为简洁起见,删除了其他块)。该文件将包含完整的INI文件内容。

file.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 传递允许的主机的完整列表。

extra-vars

但是,只有在您知道游戏手册运行时所有允许的主机时,这才适合您。