# Assigning one variable to another

**URL:** <https://forum.ansible.com/t/assigning-one-variable-to-another/15276>\
**Category:** Ansible Project\
**Created:** [May 20, 2014, 10:01am UTC](https://forum.ansible.com/t/assigning-one-variable-to-another/15276 "2014-05-20T10:01:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Perrin](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@James\_Perrin](https://forum.ansible.com/u/James_Perrin)\
**Post date:** [May 20, 2014, 10:01am UTC](https://forum.ansible.com/t/assigning-one-variable-to-another/15276/1 "2014-05-20T10:01:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 20, 2014, 11:36am UTC](https://forum.ansible.com/t/assigning-one-variable-to-another/15276/2 "2014-05-20T11:36:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 20, 2014, 11:47am UTC](https://forum.ansible.com/t/assigning-one-variable-to-another/15276/3 "2014-05-20T11:47:20Z")

</div>

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 }}

---

<div class="post-metadata">

**Author:** ![Andrei\_Pashkin](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Andrei\_Pashkin](https://forum.ansible.com/u/Andrei_Pashkin)\
**Post date:** [May 20, 2014, 7:10pm UTC](https://forum.ansible.com/t/assigning-one-variable-to-another/15276/4 "2014-05-20T19:10:44Z")

</div>

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"`
