对于部署过程,我必须检查配置文件的列表,并且对于每个配置文件,我必须检查参数是否存在,如果不存在,则将它们添加到文件中
这不仅仅是用带有lineinfile的缺省行替换行,参数可以手动修改,在这种情况下,我不能用缺省值更改它,而是保持实际值。
在我的角色/defaults/main.yml中,我创建了一个文件列表,其中包含要检查的参数和默认值
示例:
webui_test_conf_files:
- file: '{{ common_pms_workspace_path }}conf\conf-PROD.properties'
param: 'printing.timeout'
value: '{{ webui_template_configuration_vars.confPRODpropertiesj2.printing_timeout }}'
- file: '{{ common_pms_workspace_path }}conf\conf-PROD.properties'
param: 'period.bom.check'
value: '{{ webui_template_configuration_vars.confPRODpropertiesj2.period_bom_check }}'
- file: '{{ common_pms_workspace_path }}conf\conf-PROD.properties'
param: 'enabled.bom.check'
value: '{{ webui_template_configuration_vars.confPRODpropertiesj2.enabled_bom_check }}'
- file: '{{ common_pms_workspace_path }}conf\sso-conf.properties'
param: 'user.redirect.url'
value: '{{ webui_template_configuration_vars.ssoconfpropertiesj2.user_redirect_url }}'
- file: '{{ common_pms_workspace_path }}conf\sso-conf.properties'
param: 'authorization.endpoint'
value: '{{ webui_template_configuration_vars.ssoconfpropertiesj2.authorization_endpoint }}'
- file: '{{ common_pms_workspace_path }}conf\sso-conf.properties'
param: 'token.endpoint'
value: '{{ webui_template_configuration_vars.ssoconfpropertiesj2.token_endpoint }}'对于每个文件(在本例中是两个),我必须测试每个参数是否存在。如果没有出现此参数,则使用: param=value在文件中添加行,但如果我找到了此参数,则不能对此参数执行任何操作:-)
现在,我可以像这样测试和注册测试
- name: "Testing parameters"
lineinfile:
path: "{{ item.file }}"
line: "{{ item.param }}"
state: present
check_mode: yes
register: params
loop: '{{ webui_test_conf_files }}'但是我没有成功地使用这个注册的值做另一个循环:-(
你能帮我吗?
非常感谢你的帮助!
发布于 2021-07-07 22:16:51
成功使用此代码进行windows测试:-)
- name: "Test parameters"
win_shell: (Select-String -Path "{{ item.file }}" -Pattern "{{ item.param }}=")
register: params
check_mode: no
ignore_errors: yes
changed_when: no
loop: '{{ webui_test_conf_files }}'
- name: "Add missing parameters"
win_lineinfile:
path: "{{ item.item.file }}"
line: "{{ item.item.param }}={{ item.item.value }}"
state: present
when: item.stdout | length == 0
loop: "{{ params.results }}"感谢@Zeitounator让我走上正轨!
https://stackoverflow.com/questions/68271414
复制相似问题