lonix
lonix

Reputation: 20947

YAML syntax for grouping some arguments and some not

I often have to do something like this in ansible, and I need to document individual arguments:

- name: foo                          # ...
  module_name:                       # ...
    arg1: 1                          # ...
    arg2: 2                          # ...
    bar: "bar"                       # explanation     <---- related
    baz: "baz"                       # explanation     <---- related
    qux: 42                          # ...

If arguments are related, it would be nice to group them.

I tried all the following, which are incorrect syntax:

    arg1: 1                          # ...
    arg2: 2                          # ...
    bar: "bar" baz:"baz"             # explanation
    qux: 42                          # ...
    arg1: 1                          # ...
    arg2: 2                          # ...
    bar: "bar", baz: "baz"           # explanation
    qux: 42                          # ...
    arg1: 1                          # ...
    arg2: 2                          # ...
    bar="bar" baz="baz"              # explanation
    qux: 42                          # ...

I skimmed the yaml 1.2 reference, but it's surprisingly complicated.

Is this possible?

Upvotes: 1

Views: 45

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39284

Well, stricto sensu, you cannot make this in the mapping syntax, because, likewise Python, indentation and line breaks have a syntactical meaning in YAML.

But maybe what you are looking at can be achieved using the abbreviated form (as pointed out here).

- name: foo                          # ...
  module_name: {                     # ...
    arg1: 1,                         # ...
    arg2: 2,                         # ...
    bar: "bar", baz: "baz",          # explanation     <---- related
    qux: 42                          # ...
  }

This is due to the fact that YAML can be viewed as a natural superset of JSON.

Demo:

---
- hosts: localhost
  vars:
    module_name:                       # ...
      arg1: 1                          # ...
      arg2: 2                          # ...
      bar: "bar"                       # explanation     <---- related
      baz: "baz"                       # explanation     <---- related
      qux: 42
    module_name_alt: {                 # ...
      arg1: 1,                         # ...
      arg2: 2,                         # ...
      bar: "bar", baz: "baz",          # explanation     <---- related
      qux: 42
    }    

  tasks:
    - debug:
        msg: "{{ module_name }}"

    - debug:
        msg: "{{ module_name_alt }}"

Does output:

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": {
        "arg1": 1,
        "arg2": 2,
        "bar": "bar",
        "baz": "baz",
        "qux": 42
    }
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": {
        "arg1": 1,
        "arg2": 2,
        "bar": "bar",
        "baz": "baz",
        "qux": 42
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Related: https://stackoverflow.com/a/1729545/2123530

Upvotes: 1

Related Questions