Defining variables and save them as json file.

Hi,

I struggle with some issue with variables definition which I am not totally understand and can’t find help in ansible docs.

I am trying to have nice variables structure and save it as json file (re-use as local facts).

So I have groups_vars file:

`
g_core_vars:
version : “0.1.0”
varinfo:
vi_name : “core_vars”
vi_vartype : “groupvars-all”
vars:
status : “non-prod”
env : “develop”
lob : “none”
product: “none”
prole : “other”
roles:

  • web

`

Now I would like to create variable based on above values:
For example:

`
g_core_nagios_filename: ‘{{ g_core_vars.vars.env }}-{{ g_core_vars.vars.product }}’.cfg

`

However I am gettig error on ansible:
ERROR: One or more undefined variables: ‘dict object’ has no attribute: ‘vars’.

My goal is no that I can write this variabe ‘g_core_vars’ to json with ‘copy’ module:

`

  • name: Write g_core_vars variables to local facts
    copy: content=“{{ g_core_vars }}” dest=/etc/ansible/facts.d/{{ g_core_vars.varinfo.vi_name }}.fact
    owner=root group=root mode=0644

`

Summary:

  • How I can use defined variables which used previous variables names and can be saved as json in one task?
  • Not quite undertand this principle that I have to define each variable sepearatly.

`

Variables files.

var01: ‘value01’
var02: ‘value02’

somevars1:
v01: ‘{{ var01 }}’
v02: ‘{{ var02 }}’

Will work

somevars2: ‘{{ var01 }}-{{ var02 }}’

Will not work

somevars2: ‘{{ somevars1.var01 }}’

`

Please advice.

Best regards.
Marcin

If you have variables defined centrally, use them via vars_files, group_vars, or whatever, and you don’t need to copy them to local facts at all.

Local facts are there for when an administrator wants to make the mortals who use a system be able to configure it basically.

(Or it’s a shortcut to avoid writing a fact module, in some cases, though fact modules are better-er)

So ultimately I want to step back and understand the use case a bit rather than working through your questions above - though they aren’t bad questions, I think we’d be leading you a bit astray - maybe.

I could be wrong.