根据条件选择变量

我有以下变量:


secureVar:

  cosmosconstr: ""

  postgresUrl: ""

它可以为空或包含数据库 url,具体取决于我需要在 kubernetes 中有条件地创建外部服务:


- name: Get DB data

  when: (secureVars | from_json)['cosmosconstr'] | length == 0 and (secureVars | from_json)['postgresUrl'] | length == 0

  shell: ./python/db_credentials.py -dp {{ deploymentPrefix }}

  register: dbData


- name: Generate external services templates

  when: (secureVars | from_json)['cosmosconstr'] | length == 0 and (secureVars | from_json)['postgresUrl'] | length == 0

  action: template src=k8s/common/external-services.j2 dest=/tmp/k8s-external-svc.yml

  vars:

    cosmosUrl: "{{ dbData.stdout_lines[0] | urlsplit('hostname') }}"

    postgresUrl: "{{ dbData.stdout_lines[1] }}"


- name: Generate external services templates

  when: (secureVars | from_json)['cosmosconstr'] | length > 0 and (secureVars | from_json)['postgresUrl'] | length > 0

  action: template src=k8s/common/external-services.j2 dest=/tmp/k8s-external-svc.yml

  vars:

    cosmosUrl: "{{ ( secureVars | from_json )['cosmosconstr'] | urlsplit('hostname') }}"

    postgresUrl: "{{ ( secureVars | from_json )['postgresUrl'] }}"

这就是我现在正在做的事情,我想设置一个条件,以便如果跳过 dbData 我会使用secureVars,如果没有跳过我会使用 dbData,问题是,我无法检查是否dbData.skipped等于 true,因为如果它没有被跳过,它不包含skipped属性


慕姐8265434
浏览 150回答 1
1回答

守着一只汪

您可能希望使用默认的 False 值。什么时候: dbData.skiped | 默认('假')从 OP 编辑:我最终这样做了:"{% if dbData.skipped is defined %}{{ ( secureVars.stdout | from_json )['postgresUrl'] }}{% else %}{{ dbData.stdout_lines[1] }}{% endif %}"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python