ansible - 使用 jinja 模板更改配置文件中的特定行
ansible - changing a specific line in configuration file using a jinja template
我有一个我想模板化的 .ini 配置文件,但我只想使用 jinja2 文件操作一两行。
示例配置文件:
[configuration]
config1 = this_is_fine
config2 = to_be_templated
config3 = to_be_templated
config_4 = this_is_fine
什么是定义只会改变的 configuration.ini.j2 的最佳方法
config2 = {{ assigned_value }}
config3 = {{ assigned_value }}
在 运行 剧本时保持格式和剩余配置不变?
问:"用Jinja2文件操作一两行。"
A:“一两行”表示一个块。使用 blockinfile 来操作文件中的块。你需要 markers 来做到这一点,例如给定文件
shell> cat conf.ini
[configuration]
config1 = this_is_fine
config2 = to_be_templated
config3 = to_be_templated
config_4 = this_is_fine
下面的两个任务在开始 config2 和结束 config3
的块周围放置标记
- replace:
path: conf.ini
regexp: '(config2.*)'
replace: |-
{{ '#' }} BEGIN ANSIBLE MANAGED BLOCK c2-3
\g<1>
- replace:
path: conf.ini
regexp: '(config2.*[\s\S]*?config3.*)'
replace: |-
\g<1>
{{ '#' }} END ANSIBLE MANAGED BLOCK c2-3
给予
shell> cat conf.ini
[configuration]
config1 = this_is_fine
# BEGIN ANSIBLE MANAGED BLOCK c2-3
config2 = to_be_templated
config3 = to_be_templated
# END ANSIBLE MANAGED BLOCK c2-3
config_4 = this_is_fine
这两个任务不是幂等的。 运行 他们只有一次。将它们放在单独的剧本中以设置项目或测试标记的存在。
现在您可以使用模板了
shell> cat conf.ini.j2
config2 = {{ assigned_value }}
config3 = {{ assigned_value }}
例如下面的任务
- blockinfile:
path: conf.ini
marker: '# {mark} ANSIBLE MANAGED BLOCK c2-3'
block: |
{{ lookup('template', 'conf.ini.j2') }}
vars:
assigned_value: AVALUE
给予
shell> cat conf.ini
[configuration]
config1 = this_is_fine
# BEGIN ANSIBLE MANAGED BLOCK c2-3
config2 = AVALUE
config3 = AVALUE
# END ANSIBLE MANAGED BLOCK c2-3
config_4 = this_is_fine
我有一个我想模板化的 .ini 配置文件,但我只想使用 jinja2 文件操作一两行。
示例配置文件:
[configuration]
config1 = this_is_fine
config2 = to_be_templated
config3 = to_be_templated
config_4 = this_is_fine
什么是定义只会改变的 configuration.ini.j2 的最佳方法
config2 = {{ assigned_value }}
config3 = {{ assigned_value }}
在 运行 剧本时保持格式和剩余配置不变?
问:"用Jinja2文件操作一两行。"
A:“一两行”表示一个块。使用 blockinfile 来操作文件中的块。你需要 markers 来做到这一点,例如给定文件
shell> cat conf.ini
[configuration]
config1 = this_is_fine
config2 = to_be_templated
config3 = to_be_templated
config_4 = this_is_fine
下面的两个任务在开始 config2 和结束 config3
的块周围放置标记 - replace:
path: conf.ini
regexp: '(config2.*)'
replace: |-
{{ '#' }} BEGIN ANSIBLE MANAGED BLOCK c2-3
\g<1>
- replace:
path: conf.ini
regexp: '(config2.*[\s\S]*?config3.*)'
replace: |-
\g<1>
{{ '#' }} END ANSIBLE MANAGED BLOCK c2-3
给予
shell> cat conf.ini
[configuration]
config1 = this_is_fine
# BEGIN ANSIBLE MANAGED BLOCK c2-3
config2 = to_be_templated
config3 = to_be_templated
# END ANSIBLE MANAGED BLOCK c2-3
config_4 = this_is_fine
这两个任务不是幂等的。 运行 他们只有一次。将它们放在单独的剧本中以设置项目或测试标记的存在。
现在您可以使用模板了
shell> cat conf.ini.j2
config2 = {{ assigned_value }}
config3 = {{ assigned_value }}
例如下面的任务
- blockinfile:
path: conf.ini
marker: '# {mark} ANSIBLE MANAGED BLOCK c2-3'
block: |
{{ lookup('template', 'conf.ini.j2') }}
vars:
assigned_value: AVALUE
给予
shell> cat conf.ini
[configuration]
config1 = this_is_fine
# BEGIN ANSIBLE MANAGED BLOCK c2-3
config2 = AVALUE
config3 = AVALUE
# END ANSIBLE MANAGED BLOCK c2-3
config_4 = this_is_fine