# tracking state between module calls

**URL:** <https://forum.ansible.com/t/tracking-state-between-module-calls/28845>\
**Category:** Ansible Developer\
**Created:** [October 22, 2018, 4:45pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845 "2018-10-22T16:45:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Michael\_Johnson](https://forum.ansible.com/u/Michael_Johnson)\
**Post date:** [October 22, 2018, 4:45pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845/1 "2018-10-22T16:45:15Z")

</div>

Is there an “official” way to track state between module calls? I’ve come up with one option where I read a state file on each invocation, update the data and then write out the state file at the end, but it seems like this should be able to be done in memory without having to constantly read/write to a file.

I did attempt to accomplish this task by using a fact and at first it seemed like it was going to work. I was reading the fact in the setup module. However, the data returned from the setup module seems to always be the facts as they would appear at the beginning of the run and don’t include any modifications (including variables updated/added with set\_fact). This isn’t super surprising, but leaves me back where I began – trying to figure out a way to track state that doesn’t rely on a bunch of file access.

If the answer is that it’s just not possible given the way ansible works, that’s fine, I just figured I would check as it seems like I must be missing something.

---

<div class="post-metadata">

**Author:** ![Luke\_Murphy](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@Luke\_Murphy](https://forum.ansible.com/u/Luke_Murphy)\
**Post date:** [October 22, 2018, 5:00pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845/2 "2018-10-22T17:00:19Z")

</div>

My two cents. I think you're on the right track! There isn't much out  
there for this specifically in Ansible that is obvious.

I do know that testinfra was created to test against specific states, so  
you can make verification steps, if that is useful: [https://testinfra.readthedocs.io/en/latest/](https://testinfra.readthedocs.io/en/latest/)

I think [https://github.com/aelsabbahy/goss](https://github.com/aelsabbahy/goss) is also used for this. It is  
included in Molecule.

Also referring to Molecule, they use the read/write strategy with their  
'instance config' which is dumped for persistence between create/destroy  
runs:

[https://github.com/metacloud/molecule/blob/0272bc0560df4f76e1c00617ca0e0dd8b5a46b58/molecule/cookiecutter/scenario/driver/ec2/{{cookiecutter.molecule\_directory}}/{{cookiecutter.scenario\_name}}/create.yml#L91-L113](https://github.com/metacloud/molecule/blob/0272bc0560df4f76e1c00617ca0e0dd8b5a46b58/molecule/cookiecutter/scenario/driver/ec2/%7B%7Bcookiecutter.molecule_directory%7D%7D/%7B%7Bcookiecutter.scenario_name%7D%7D/create.yml#L91-L113)

Hope that helps.

Luke

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 22, 2018, 6:47pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845/3 "2018-10-22T18:47:14Z")

</div>

not sure if this is what you want/need but aside from normal vars you  
can also use 'stats'  
[https://docs.ansible.com/ansible/latest/modules/set\_stats\_module.html](https://docs.ansible.com/ansible/latest/modules/set_stats_module.html)

---

<div class="post-metadata">

**Author:** ![Michael\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Michael\_Johnson](https://forum.ansible.com/u/Michael_Johnson)\
**Post date:** [October 22, 2018, 9:03pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845/4 "2018-10-22T21:03:14Z")

</div>

Whoops, didn’t reply all…

---

<div class="post-metadata">

**Author:** ![Michael\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Michael\_Johnson](https://forum.ansible.com/u/Michael_Johnson)\
**Post date:** [October 22, 2018, 9:05pm UTC](https://forum.ansible.com/t/tracking-state-between-module-calls/28845/5 "2018-10-22T21:05:37Z")

</div>

I did look at the stats module early on, but you make a really good point. I’ll revist that module and see if I can steal some ideas from it. If not, I can always stick with a state tracking file.
