How to append to an existing collection

Talking about variables, I would like to add new items to an existing collection of value.

Use case:

There is a group with a variable in their group_vars file which defines a series of cronjobs that will be installed on all hosts of that group. In addition to that, there is just one host within that group that requires and additional cronjob and I would like to add the definition of that additional cronjob in host_vars to the existing variable from group_vars

i have checked the YAML documentation for that but haven’t found anything like that. However, I guess there is a trick to get there and wonder if anyone around here was aware of a solution.

It’s probably not what you what, but within a template, you can use the {% set %} parameter in Jinja2.
This won’t really help you in a playbook. set_fact exists, but that’s not really a good way to set complex datastructures.

In general, I tend to think when programmatic questions like this, it’s more of a modelling question, and there are often easier ways to do it rather than thinking programmatically.

What you might want to do though is just use the cron module to add the jobs, or better, template cron.d and leave this as two tasks.

I don’t think they need to be specified all in one “with_items” block, the actions to add the cron tabs could even be spread through different roles.

Getting closer perhaps?

You’re correct, that’s not exactly what I was looking for but it brought me to the idea to define two lists and two tasks - one for group based cronjobs and another one for host based cronjobs. In fact, in the tasks I’m already using the cron module and I just repeat that one task in the main.yml of the common role. Thanks.