# Defining variables and save them as json file.

**URL:** <https://forum.ansible.com/t/defining-variables-and-save-them-as-json-file/15765>\
**Category:** Ansible Project\
**Created:** [July 24, 2014, 3:17pm UTC](https://forum.ansible.com/t/defining-variables-and-save-them-as-json-file/15765 "2014-07-24T15:17:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [July 24, 2014, 3:17pm UTC](https://forum.ansible.com/t/defining-variables-and-save-them-as-json-file/15765/1 "2014-07-24T15:17:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 24, 2014, 10:47pm UTC](https://forum.ansible.com/t/defining-variables-and-save-them-as-json-file/15765/2 "2014-07-24T22:47:22Z")

</div>

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.
