tracking state between module calls

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.

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/

I think 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

Hope that helps.

Luke

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

Whoops, didn’t reply all…

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.