Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML: When an equals sign (=) can be used for dictionaries?

Tags:

yaml

ansible

I read key1=value1 key2=value2 style dictionaries all the time in ansible playbooks that are supposed to be written in YAML. On the other hand I didn't find any documentation for this format and there seem to be cases where it doesn't work for me. What is the exact specification and where can I find it?

like image 989
Pavel Šimerda Avatar asked Aug 31 '25 16:08

Pavel Šimerda


1 Answers

In Ansible key=value is not used for dicts in general.

It is an alternative syntax to pass parameters to actions/modules, like:

- name: restart apache
  service: name=apache state=restarted

Here you pass name and state parameters to service module.

From YAML perspective name=apache state=restarted is a string. There's some magic done under the hood by Ansible to split it. But it become unreliable and cumbersome with complex arguments, so I always use native YAML syntax:

- name: restart apache
  service:
    name: apache
    state: restarted

And this key=value works only for modules/actions parameters, you can't define dictionaries like this:

vars:
  # this will give you a string, not dict
  mydict: key1=value1 key2=value 
like image 168
Konstantin Suvorov Avatar answered Sep 02 '25 21:09

Konstantin Suvorov