首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环遍历文件以检查是否存在param,如果不存在,则使用默认值添加它

循环遍历文件以检查是否存在param,如果不存在,则使用默认值添加它
EN

Stack Overflow用户
提问于 2021-07-06 21:24:58
回答 1查看 34关注 0票数 0

对于部署过程,我必须检查配置文件的列表,并且对于每个配置文件,我必须检查参数是否存在,如果不存在,则将它们添加到文件中

这不仅仅是用带有lineinfile的缺省行替换行,参数可以手动修改,在这种情况下,我不能用缺省值更改它,而是保持实际值。

在我的角色/defaults/main.yml中,我创建了一个文件列表,其中包含要检查的参数和默认值

示例:

代码语言:javascript
复制
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在文件中添加行,但如果我找到了此参数,则不能对此参数执行任何操作:-)

现在,我可以像这样测试和注册测试

代码语言:javascript
复制
 - name: "Testing parameters"
   lineinfile:
     path: "{{ item.file }}"
     line: "{{ item.param }}"
     state: present
   check_mode: yes
   register: params
   loop: '{{ webui_test_conf_files }}'

但是我没有成功地使用这个注册的值做另一个循环:-(

你能帮我吗?

非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2021-07-07 22:16:51

成功使用此代码进行windows测试:-)

代码语言:javascript
复制
 - 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让我走上正轨!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68271414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档