rush
rush

Reputation: 2564

ansible variables inside variables

I'm trying to build a role which uses some variables from a file. Though this variables also depend on some common variable which I want to define for all roles only one without copy&paste it to every single variable file.

Though I can't understand how I can do that. I'm talking about vars file only.

E.g. roles/dns/tasks/main.yml includes file for specific location:

- include_vars: ../vars/dns/domains_{{ location }}.yml

location is defined on role level. Inside this I'm trying to define settings for various dns names, e.g.:

domains: [
  {
    domain: "domain.com",
    location: "america", 
    ip: "xx.xx.xx.xx",
    ttl: 1800,
    mx: "10 mail",
    subdomains: [
      { name: "www",    ip: "xx.xx.xx.xx"},
    ]   
  },  
]

so here I have the same IP defined for each and every entry. Is there a way to put all IPs into some global var file (e.g. group_vars/all/vars_file.yml ) and use it inside this role specific var file like this:

domains: [
  {
    domain: "domain.com",
    location: "america", 
    ip: server.america.ip,
    ttl: 1800,
    mx: "10 mail",
    subdomains: [
      { name: "www",    ip: server.america.ip },
    ]   
  },  
]

where server.america.ip is defined somewhere global?

Upvotes: 0

Views: 2824

Answers (2)

udondan
udondan

Reputation: 59989

Yes, that's possible. Variable files actually run through jinja as well, so you can have basic expressions and variables in variable files.

domains: [
  {
    domain: "domain.com",
    location: "america", 
    ip: "{{ server.america.ip }}",
    ttl: 1800,
    mx: "10 mail",
    subdomains: [
      { name: "www",    ip: "{{ server.america.ip }}" },
    ]   
  },  
]

Upvotes: 1

limace255
limace255

Reputation: 41

You can include variables from role X, at beginning of tasks/main.yml of your role Y, for example. If you have a common role, let's put location on this one, for example. Only potential problem, is that you might need to use 'with_items' to call included variables.

Upvotes: 0

Related Questions