Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If condition possible in yaml file?

Tags:

yaml

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'
like image 554
Muhammad Umair Avatar asked Jan 15 '19 07:01

Muhammad Umair


People also ask

Can we use if condition in YAML file?

You cannot use if statements everywhere within your YAML.

How does YAML handle special characters?

YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: { , } , [ , ] , & , * , # , ? , | , - , < , > , = , ! , % , @ , : also ` and , YAML Spec.

What is valid YAML syntax?

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... . This is part of the YAML format and indicates the start and end of a document. All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):

Does YAML have data types?

Basic Data Types YAML excels at working with mappings (hashes / dictionaries), sequences (arrays / lists), and scalars (strings / numbers). While it can be used with most programming languages, it works best with languages that are built around these data structure types.


2 Answers

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.

like image 180
Chris Collett Avatar answered Sep 19 '22 18:09

Chris Collett


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.

like image 20
vaibhav lodha Avatar answered Sep 20 '22 18:09

vaibhav lodha