I have directory with yml files in there which set parameters for different instances of the software I'm to deploy.

It looks something like this:

  servername: mycoolserver
  instancetype: testing

This is loaded into an instances variable using include_vars

The design (which I have inherited, btw, so don't judge me too hard) specifies that there can be several files of this instance parameters in the directory, several for each instancetype, and different instances.

To run different tasks on these instances, there are blocks like this in my main site.yml

- name: Start software container 1
  include_tasks: software-container-1.yml
  loop: "{{ instances | flatten(levels=1) }}"

So we don't need to specify beforehand how many instances we have etc. (at least that is the basic idea behind it as I understand it). The main reason for this is that ansible runs locally on the servers from a common playbook, and not from a central server which logs into the different servers, which is just not possible due to security design in the environment.

I now want to filter the instances variable so it only includes all the items which have a certain instancetype, e.g. testing

I see that I could either set a new fact like filteredinstances, filter in the loop part of the tasks, or in the tasks itself - but I would prefer very much one of the first two options to keep it neat an clean.

Any hints about how to accomplish this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.