# Evaluate variable immediately, save for later

**URL:** <https://forum.ansible.com/t/evaluate-variable-immediately-save-for-later/20415>\
**Category:** Ansible Project\
**Created:** [February 2, 2016, 3:04am UTC](https://forum.ansible.com/t/evaluate-variable-immediately-save-for-later/20415 "2016-02-02T03:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Byrne](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Colin\_Byrne](https://forum.ansible.com/u/Colin_Byrne)\
**Post date:** [February 2, 2016, 3:04am UTC](https://forum.ansible.com/t/evaluate-variable-immediately-save-for-later/20415/1 "2016-02-02T03:04:25Z")

</div>

Hey, so I have the following sequence where I first register a variable, then want to evaluate stats from it later but I want the stats from when the variable was first created. What is the best way to do this?

I could see registering a second variable immedeatly that evaulates properties of the variable that was just registered, but it doesn’t seem too ‘clean’ of a method.

What I am trying to do here is register a variable about a directory path, and only ‘mysql\_install\_db’ if that directory doesn’t exist. However, I have to create that directory at some point, so by the time I run ‘mysql\_install\_db’ that ‘p.stat.isdir’ will evaluate to true every time, even if it didn’t exist a second ago. So I somehow need to define and cache a variable one step ahead.

`

- stat: path=/mnt/mysql  
register: p

- name: Create /mnt/mysql directory for database  
file:  
path: /mnt/mysql  
state: directory  
mode: 0750  
owner: mysql  
group: mysql

- name: Install mysql database in atypical datadir

command: mysql\_install\_db --user=mysql --datadir=/mnt/mysql  
when: p.stat.isdir is defined and p.stat.isdir == False  
`

Thanks for any help. It is greatly appriciated!

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [February 2, 2016, 1:29pm UTC](https://forum.ansible.com/t/evaluate-variable-immediately-save-for-later/20415/2 "2016-02-02T13:29:48Z")

</div>

Hi,

try use handler:

- stat: path=/mnt/mysql  
register: p

- name: Create /mnt/mysql directory for database  
file:  
path: /mnt/mysql  
state: directory  
mode: 0750  
owner: mysql  
group: mysql  
when: not p.stat.exists  
notify:

- create database

handlers:

- name: create database  
command: mysql\_install\_db --user=mysql --datadir=/mnt/mysql

First time, there will not be /mnt/mysql, so we will create it in second task. It has changed state, therefore we will call handler and create a database. To be sure, we will skip it every time, the path already exists.

---

<div class="post-metadata">

**Author:** ![Colin\_Byrne](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Colin\_Byrne](https://forum.ansible.com/u/Colin_Byrne)\
**Post date:** [February 2, 2016, 6:20pm UTC](https://forum.ansible.com/t/evaluate-variable-immediately-save-for-later/20415/3 "2016-02-02T18:20:01Z")

</div>

Thanks! For some reason I was stuck on using exclusively handlers or registered variables. Using them in combination is definitely the way to go.  
-C
