Splitting a role in 2 but need to pass fact from one to the other

I’ve currently got a role that runs server updates. One of the facts it sets in there is “updates”. This can then be used as “updates.stdout_lines” to determine if there were any updates. There are 2 steps at the end of the role which reboot the server if there were updates and the ansible-shell step running “needs-restarting -r” registered a non zero return code (machine needs rebooting). At the moment I’ve had to set my device downtime at 30 minutes to allow time for updates to run and the reboot to take place. I’d like to split the reboot part into a seperate role so I can run the lm-device-sdt role immediately before the reboot and set it to 5 mins.

So I have 2 questions regarding this.

  1. How do I pass the fact from one role to another within the same playbook
  2. Is their a better way of doing this that doesn’t involve adding the lm-device-sdt into the existing role.

Both facts (from module returning ansible_facts) or host variables (what set_fact does most of the time, yes, name is confusing …) are set per host, so they are ‘passed’ through the whole run, even if you don’t want them to.

There is no need to pass them explicitly between roles. The only time you cannot access them directly is when you start a new play that does not target the same hosts, but you still have the data accessible via hostvars[<previously targeted host>][<varname>]

2 Likes