3

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?

Pavel Šimerda
  • 5,783
  • 1
  • 31
  • 31
  • 1
    You cannot do this *in YAML*, as it is not in the specification of YAML. But you can of course have an `=` in a non-quoted scalar string. How an application interprets that string once loaded, has nothing to do with YAML. – Anthon Mar 20 '17 at 16:57
  • Possible duplicate of [Ansible syntax best practice, YAML dictionary (key: value) or equal sign (key=value)?](http://stackoverflow.com/questions/39822354/ansible-syntax-best-practice-yaml-dictionary-key-value-or-equal-sign-key-va) – techraf Mar 21 '17 at 01:36

1 Answers1

6

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 
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193