Mirage
Mirage

Reputation: 31568

Can i use variable inside another variable in ansible

In my role i have default.yml like this

site_root: "/var/www/html/sites"

project_root: "/var/www/html/sites/site1"

Can i use like this

project_root: "{{site_root }}/site1"

I tried that but its not working

Upvotes: 1

Views: 471

Answers (1)

Shahar
Shahar

Reputation: 810

This should definitely work. For example, the following playbook should print out the resolved project_root as you would expect it (when running the playbook use the -v flag to see the stdout of the echo command):

---
- hosts: localhost
  vars:
    - site_root: "/var/www/html/sites"
    - project_root: "{{site_root}}/site1"
  tasks:
    - shell: echo {{project_root}}

This also works when used in a role. Notice though that defining default variables for a role is done, not in default.yml, but rather in a main.yml file located under the defaults directory:

- 
|- roles
   |- <your_role_name>
      |- defaults
         |- main.yml

This works for ansible version > 1.5 (and probably for earlier versions as well)

Upvotes: 1

Related Questions