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'
You cannot use if statements everywhere within your YAML.
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.
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):
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With