27

I need to add one more name based on if condition. If If variable value from another .yml file is "yes" then add a new name in the list

I've the following code in my yaml file:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'

I need to add more name based on if condition. If variable value from another .yml file is "yes" then add a new name like this:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - name: 'new.js'

is this possible?

Tried the following:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - | # Test multiline yaml support
   if [[ "site.data.settings.requiredjs" == "yes" ]]; then
     name: 'new.js'
   fi

I need to add more name based on if condition. If variable value from another .yml file is "yes" then add a new name like this:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - name: 'new.js'
Anthon
  • 69,918
  • 32
  • 186
  • 246
Muhammad Umair
  • 303
  • 1
  • 3
  • 8
  • 2
    Not with "just" YAML. YAML is akin to HTML or JSON in this regard: [see the YAML specification](https://yaml.org/spec/1.2/spec.html) for what is allowed. – user2864740 Jan 15 '19 at 07:09
  • I suggest you read up on YAML (the specfication, the yaml.org website). 1) The recommended extension for YAML files has been `.yaml` since 2006. 2) You seem to be under the illusion that literal block style scalars are interpreted by YAML. There is nothing in the specification even hinting at that (what your program does is of course a different matter). 3) You seem to think YAML has some dotted notation to get to nested values. Again read the spec it doesn't. – Anthon Jan 15 '19 at 19:24

7 Answers7

16

As a supplement to Vaibhav's response: although yml does not inherently support if statements, some applications that use yml files for instructions may be able to parse if statements included in the yml. GitLab is one notable example. In GitLab's CI/CD, the .gitlab-ci.yml file can be configured to include if statements such as:

job:
  script: "echo Hello, Rules!"
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
      when: always
    - if: '$VAR =~ /pattern/'
      when: manual
    - when: on_success

This is from https://docs.gitlab.com/ee/ci/yaml/#rules.

For more info, see this answer: https://stackoverflow.com/a/55464100/10792235.

Chris Collett
  • 1,074
  • 10
  • 15
  • What about a bash script in the yml file? What's the right format? I've got a question here, trying to solve it for a long time, could you take a look please https://stackoverflow.com/questions/65214195/how-do-i-tell-if-a-json-file-does-not-exist-inside-a-yaml-file – wawawa Dec 09 '20 at 09:59
  • Your question was removed, but you would add a `- script:` section and put your code into it. Each line should start with a dash. Sometimes you may need to enclose the line in quotes for certain characters to be read. – Chris Collett Jan 07 '21 at 22:01
13

You can't add conditions to a yml file its just a text formatting way, not a language. But still, you can load the yml file into a programming language and can apply if else to it. based on a string.

  • 2
    Thanks a million for the reply, I would be thankful to you if Could you please gimme an example? and how to add yml file into a programming language? – Muhammad Umair Jan 15 '19 at 07:15
  • check this post : https://stackoverflow.com/questions/41974628/reading-yaml-file-with-python-results-in-attributeerror/41975745#41975745 – vaibhav lodha Feb 04 '19 at 07:53
  • 1
    That's not true. inmy code for instance I use following code nagiosgroup: "{{ 'srv-test' if inventory_hostname in groups['KISS_ACPT'] else 'srv-dev' }}" – Thibault Richard Sep 24 '20 at 14:57
  • 1
    That's not true. inmy code for instance I use following code vartodefine: "{{ 'value1' if inventory_hostname in groups['BLAH'] else 'value2' }}" – Thibault Richard Sep 24 '20 at 14:58
  • I wanted to run a bash script inside yaml, can you take a look at this question please?https://stackoverflow.com/questions/65214195/how-do-i-tell-if-a-json-file-does-not-exist-inside-a-yaml-file – wawawa Dec 09 '20 at 09:58
5
{{if .Env.LOCAL_VARIABLE| eq ""}}
    - /^max_wal_senders = 0\s*.*/
{{ else }}
    - /^max_wal_senders = 10\s*.*/
    - /^vacuum_defer_cleanup_age = {{.Env.VACUUM}}\s*.*/
{{ end }}

Abrahim
  • 711
  • 6
  • 4
4

Try this

vartodefine: "{{ 'value1' if condition else 'value2' }}"
Thibault Richard
  • 356
  • 2
  • 11
4

An elegant option, when having multiple configuration files would be as follows:

application.yml

kafka.bootstrap-servers-nonprod: "nonprod-value"
kafka.bootstrap-servers-prod: "prod-value"
kafka.bootstrap-servers: ${kafka.bootstrap-servers-${env-type}}

application-qa.yml

env-type: "nonprod"
#kafka.bootstrap-servers will have the value: "nonprod-value"

application-prod.yml

env-type: "prod"
#kafka.bootstrap-servers will have the value: "prod-value"
Radu Linu
  • 1,143
  • 13
  • 29
1

Please try below syntax for condition in Yaml file

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
{{ if eq site.data.settings.requiredjs "yes" }}
 - name: 'new.js'
{{ end }}
Nikhil
  • 19
  • 1
  • 8
-3

Try this code, the word "item" could be whatever you want:

{% for item in JsNames %}
  {% if name == 'jquery.min.js' %}
    excute your code
  {% elif name == 'script.js' %}
    excute your code
  {% else %}
    excute your code
  {% endif %}
{% else %}
  some error message
{% endfor %}
Thylorion
  • 105
  • 4
  • which library would be able to support this in python? I tried the default pyyaml, it shows error while parsing % `found character '%' that cannot start any token` – Rohit Dwivedi Jul 21 '21 at 11:46