I have one list of dicts ( variable ):
sites:
- { site: "https://site1.com", project: "own", module: "2xx" }
- { site: "https://site2.com", project: "external", module: "2xx" }
- { site: "https://site3.net", project: "own", module: "ssl" }
- { site: "https://site4.org", project: "external", module: "ssl2" }
- { site: "https://site5.co", project: "external", module: "2xx"}
simple template:
{% for dict_item in sites %}
- targets:
- {{ dict_item.site }}
labels:
instance: {{ dict_item.site }}
job: "blackbox_exporter | sites_check"
project: {{ dict_item.project }}
module: {{ dict_item.module }}
{% endfor %}
and task :
- name: "Test multi"
template:
src: "test.yml.j2"
dest: "/opt/test/{{ item.module }}.yml"
with_items: "{{ sites }}"
So, with that task I'm trying to generate separate files ( {{ module}}.yml ) with own set of targets, based on "module" key. But unfortunately inside each of file I have whole list of targets.
What I have:
[root@localhost test]# cat 2xx.yml
- targets:
- https://site1.com
labels:
instance: https://site1.com
job: "blackbox_exporter | sites_check"
project: own
module: 2xx
- targets:
- https://site2.com
labels:
instance: https://site2.com
job: "blackbox_exporter | sites_check"
project: external
module: 2xx
- targets:
- https://site3.net
labels:
instance: https://site3.net
job: "blackbox_exporter | sites_check"
project: own
module: ssl
- targets:
- https://site4.org
labels:
instance: https://site4.org
job: "blackbox_exporter | sites_check"
project: external
module: ssl2
- targets:
- https://site5.co
labels:
instance: https://site5.co
job: "blackbox_exporter | sites_check"
project: external
module: 2xx
[root@localhost test]# diff -u 2xx.yml ssl.yml
[root@localhost test]#
[root@localhost test]# diff -u 2xx.yml ssl2.yml
[root@localhost test]#
So content within every file the same and has whole list of records. BUT I want to split it in this way:
[root@localhost test]# cat 2xx.yml
- targets:
- https://site1.com
labels:
instance: https://site1.com
job: "blackbox_exporter | sites_check"
project: own
module: 2xx
- targets:
- https://site2.com
labels:
instance: https://site2.com
job: "blackbox_exporter | sites_check"
project: external
module: 2xx
- targets:
- https://site5.co
labels:
instance: https://site5.co
job: "blackbox_exporter | sites_check"
project: external
module: 2xx
[root@localhost test]# cat ssl.yml
- targets:
- https://site3.net
labels:
instance: https://site3.net
job: "blackbox_exporter | sites_check"
project: own
module: ssl
[root@localhost test]# cat ssl2.yml
- targets:
- https://site4.org
labels:
instance: https://site4.org
job: "blackbox_exporter | sites_check"
project: external
module: ssl2
How I can differentiate targets by files ?
How I can differentiate targets by files ? I know that I can add different tasks for each of "module key" and filter it by "when" construction, but in the future I could have lots of modules and save DRY inside my roles tasks