Assigning one variable to another

35 views
Skip to first unread message

James Perrin

unread,
May 20, 2014, 6:01:26 AM5/20/14
to ansible...@googlegroups.com
Hi,

I want to ask what is the correct syntax for assigning one variable to another? I wish to do this in roles/for/vars/main.yml.

It should be something like:

  ip_address: {{ static_ip_address }}


but I think I've read somewhere that yml requires quotes around the template:

  ip_address: "{{ static_ip_address }}"

is this correct?


Thanks
James

Serge van Ginderachter

unread,
May 20, 2014, 7:36:21 AM5/20/14
to ansible...@googlegroups.com

On 20 May 2014 12:01, James Perrin <james....@googlemail.com> wrote:
but I think I've read somewhere that yml requires quotes around the template:

  ip_address: "{{ static_ip_address }}"

is this correct?

​Yes. Without the quoting, yaml interprets this as a hash.​

Petros Moisiadis

unread,
May 20, 2014, 7:47:20 AM5/20/14
to ansible...@googlegroups.com
Playbooks must be valid YAML, so, quotes are needed since you cannot _start_ a YAML string with a curly brace '{' character, because that character is used to define YAML dictionaries. If you were to use the var in the middle of a string though you would not need to use quotes. For example, the following syntax is correct:

welcome_message: Hello {{ name }}


Thanks
James
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21dc952f-2130-4307-b33c-7a2cdb0cca3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Pashkin

unread,
May 20, 2014, 3:10:44 PM5/20/14
to ansible...@googlegroups.com
Another use-case.
Say I have two of variables:
newrelic_license_key: null
newrelic_environment: null
newrelic: no
And if I need to test if there is need to install newrelic on a server I need to write this:
when: newrelic and newrelic_license_key is not none and newrelic_environment is not none
I would prefer to do this:
newrelic_license_key: null
newrelic_environment: null
newrelic: no
use_newrelic: {{
newrelic and newrelic_license_key is not none and newrelic_environment is not none }}
And then use use_newrelic in all conditional statements.

There is a workaround for this: wrap use_newrelic value to quotes and then check it in that way: use_newrelic == "True"

-- 
With kind regards, Andrew Pashkin.
cell phone - +7 (985) 898 57 59
Skype - waves_in_fluids
e-mail - andrew....@gmx.co.uk
Reply all
Reply to author
Forward
0 new messages