Skip to content
Oct 4 / Greg

Passing Ansible Variables In Workflows Using set_stats

Workflows allow AAP, AWX, and Ascender to visually connect playbooks together. This works great…until you try and pass variables from one job template(playbook and required parts in Ascender) to another. That’s where the set_stats module comes in. It has the ability to pass between workflow jobs, but the documentation doesn’t fully explain how it works, and some much needed functionality doesn’t exist…so this blog post will show you how to work around these limitations!

Video

set_fact Vs set_stats

set_fact

If you are to the point in your automation journey where you are playing with workflows I’m going to go out on a limb and say you’ve already used the set_fact module. In short, it allows you to create and/or set the contents of a variable. Here’s an example:

1
2
3
- name: What am I having for lunch?
  ansible.builtin.set_fact: 
    lunch: tacos

This works a treat unless you want to pass this variable to another job template in a workflow…these variables are scoped to the individual job template only, but there is a workaround.

set_stats

The set_stats module looks pretty similar to set_fact…so for example:

1
2
3
4
- name: What am I having for lunch?
  ansible.builtin.set_stats: 
    data:
      lunch: tacos

The only real difference I see here beyond a different module name, is an option called “data” and then my new variables. So it should feel fairly familiar. One of its key features is that set_stats variables(depending on how I create them) can be passed to another job template in a workflow! Its operation, however, is quite different.

Functional Differences

By default set_facts will instantiate a “lunch” variable(lunch as in the variable name from the above example) for each host, and each host can have different contents of the “lunch” variable.

As for set_stats, by default it does not do per host and the information is aggregated. This means if I have 10 hosts, there is only 1 set_stats, and when I set new information in the variable it will just start smashing it all together.

Playbooks And Examples

You can find my playbooks here in my ansbile-misc repository.

First is the playbook doing collection(set-stats-test.yml) and I’ve got a second playbook that displays all of the information(set-stats-test2.yml). They are in two playbooks so that I can build a workflow. I’m only going to cover the collection playbook, though.

I’ll show one or two tasks and then results from the second playbook to give you an idea of what each does.

Per Host Stats

1
2
3
4
5
6
7
8
9
10
11
12
  - name: per_host true and aggregate true
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_true_agg_true: "{{ ansible_facts.distribution_version }}"
      per_host: true
 
  - name: per_host true and aggregate false
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_true_agg_false: "{{ ansible_facts.distribution_version }}"
      per_host: true
      aggregate: false

These two tasks both set the per_host option to true, but one has aggregation enabled. The results in the second job template for these variables are:

1
2
    "dist_ver_per_host_true_agg_true = not set",
    "dist_ver_per_host_true_agg_false = not set",

As you can see, neither option actually made it to the second job template…odd, huh? This is because workflows don’t pass per host stats. The stats info is passed as extra vars to the rest of the playbooks, which is why it won’t do per host information.

Not Per Host

1
2
3
4
5
6
7
8
9
10
11
12
  - name: per_host false and aggregate true- default behavior
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_false_agg_true: "{{ ansible_facts.distribution_version }}"
      per_host: false
 
  - name: per_host false and aggregate false
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_false_agg_false: "{{ ansible_facts.distribution_version }}"
      per_host: false
      aggregate: false

These two tasks don’t do per host, so their info should be passed. One is using aggregation and one is not. By default set_stats has per_host false and aggregate: true. Here are the results:

1
2
    "dist_ver_per_host_false_agg_true = 8.68.8",
    "dist_ver_per_host_false_agg_false = 8.8"

So the two hosts I’m running this against return different info; one passes back 8.6 and the other passes 8.8.
As you can see the default action will just start smashing together all of the returned information, which, generally, isn’t that helpful, unless you are really just passing over simple information.

The second task with aggregation turned off simply returns the value of the very last host processed…which also isn’t always that useful in certain situations.

Per Host Stats Workaround

There is a workaround for passing per host information!

1
2
3
4
5
6
7
8
9
10
11
  - name: per host collection and aggregate false via a workaround
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_agg_false: "{{ dist_ver_per_host_agg_false | default({}) | combine({ inventory_hostname : { 'ver' : ansible_facts.distribution_version}}) }}"
      aggregate: false
 
  - name: per host collection and aggregate true via a workaround
    ansible.builtin.set_stats:
      data:
        dist_ver_per_host_agg_true: "{{ dist_ver_per_host_agg_true | default({}) | combine({ inventory_hostname : { 'ver' : ansible_facts.distribution_version}}) }}"
      aggregate: true

Looking at both tasks, there is a lot going on in a single line…let’s break it down:

1
        dist_ver_per_host_agg_false: "{{ dist_ver_per_host_agg_false | default({}) | combine({ inventory_hostname : { 'ver' : ansible_facts.distribution_version}}) }}"

This is actually one long line(even if there is word wrap on it).
It takes the existing variable and builds on it.
default({}) sets the variable to empty if it doesn’t exist(it’s really a way around causing an error).
combine joins two dictionaries together(which is what we are creating). This one builds the variable out with the additional information.

Here are the results when run:
First task

1
2
3
  "dist_ver_per_host_agg_false": {
    "greg-rocky86nonlts": {
      "ver": "8.8"

Second task

1
2
3
4
5
6
7
  "dist_ver_per_host_agg_true": {
    "greg-rocky86lts": {
      "ver": "8.6"
    },
    "greg-rocky86nonlts": {
      "ver": "8.8"
    }

You can see the first task has aggregate turned off, so it only shows whatever host was processed last…which means it’s not really per host!

The second task returns information for all of my hosts! This means that I can collect and pass per host information from one job template to another in a playbook!

Just to bring it all home, here’s the variables section for the second job template(shows variables passed as “extra vars”):

Conclusion

While it’s not always necessary, it is beyond useful to pass per host information in your workflows, and how you have a solid method to do so! If you have any tweaks or tunes you’d make to this, please let me know.

Good luck, and happy automating!

Oct 1 / Greg

Fantasy Restaurant Jeff Kellum

Welcome to the warmup exercise for the Why Am I podcast called “the Fantasy Restaurant.” In here my guests get to pick their favorite: drink, appetizer, main, sides, and dessert…anything goes. While Jeff may have been prepared, you can never be fully ready for the full force of the fantasy. He actually puts together a pretty good meal…though he does have a worrying brussel sprout obsession… At any rate, I hope you enjoy this meal with Jeff. Help us grow by sharing with someone!
Youtube version here:

Please show them some love on their socials here: ⁠⁠⁠⁠⁠⁠⁠⁠⁠https://www.linkedin.com/in/jeffkellum/⁠.

Inspired by one of my favorite podcasts: https://www.offmenupodcast.co.uk/

If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)
Sep 24 / Greg

Why Am I Jeff Kellum

Hey everybody, I’m Greg Sowell and this is Why Am I, a podcast where I talk to interesting people and try to trace a path to where they find themselves today.  My guest this go around is Jeff Kellum.  He’s a content manager (and actually the person who discovered me for Lynda), a cyclist, and a sometimes world traveler.  What’s interesting to me is he finds all of these random people and alters the course of their lives, but wants to do it quietly behind the scenes.  Well, I see you Jeff, and I appreciate you.  I hope you enjoy this chat with Jeff. Help us grow by sharing with someone!
Youtube version here:

Please show them some love on their socials here: https://www.linkedin.com/in/jeffkellum/⁠.

If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)
Sep 17 / Greg

Fantasy Restaurant Loose Ends

Welcome to the warmup exercise for the Why Am I podcast called “the Fantasy Restaurant.” In here my guests get to pick their favorite: drink, appetizer, main, sides, and dessert…anything goes. Maysey and Jen have differing meals, but seem like they fit together surprisingly well. Both sound amazing, but Masey’s Honey Paw order is the real star of the show. I hope you enjoy this meal with Masey and Jen. Help us grow by sharing with someone!
Youtube version here:

Please show them some love on their socials here: https://www.looseendsproject.org/ , https://www.instagram.com/thelooseendsproject/.

Inspired by one of my favorite podcasts: https://www.offmenupodcast.co.uk/

If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)
Sep 10 / Greg

Why Am I Loose Ends

Hey everybody, I’m Greg Sowell and this is Why Am I, a podcast where I talk to interesting people and try to trace a path to where they find themselves today.  My guest this go around is Loose Ends…or more specifically the cofounders Masey and Jen.  These two are friends on opposite ends of the country who have joined forces to create Loose Ends, which is a nonprofit that “aims to ease grief, create community, and inspire generosity by matching volunteer handwork finishers with projects people have left unfinished due to death or disability.”  As you can imagine, it is a powerful story.  I hope you enjoy this chat with Masey and Jen. Help us grow by sharing with someone!
Youtube version here:
If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)
Aug 27 / Greg

Fantasy Restaurant Liz Giuffre

Welcome to the warmup exercise for the Why Am I podcast called “the Fantasy Restaurant.”  In here my guests get to pick their favorite: drink, appetizer, main, sides, and dessert…anything goes. We start by setting the weather and then working backwards…which is just how it should be LOL.  I don’t know what I want until I know the atmosphere.  Liz invents the mushroom situation, which I’m definitely here for.  I hope you enjoy this meal with Liz. Help us grow by sharing with someone!
Youtube version here:
If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)
Aug 20 / Greg

Why Am I Liz Giuffre

Hey everybody, I’m Greg Sowell and this is Why Am I, a podcast where I talk to interesting people and try to trace a path to where they find themselves today. My guest this go around is Liz Giuffre. This doctor lady is a senior lecturer at the University of Technology in Sydney by day, and by night she’s an author, blogger, and podcaster. Liz isn’t a fan of gate keepers, or really anyone that stands in the way of progress. She’s opinionated and funny, which is one of my favorite combos. I hope you enjoy this chat with Liz. Help us grow by sharing with someone!
Youtube version here:
If you want to support the podcast you can do so via https://www.patreon.com/whyamipod (this gives you access to bonus content including their Fantasy Restaurant!)