python jinja2 generator two or more template file

task



- name: create manifest directory
file:
state: directory
path: "{{pushgateway_manifest_dir}}"


- name: generator pushgateway manifest
template:
src: "pushgateway-scale.json.j2"
dest: "{{pushgateway_manifest_dir}}/pushgateway-scale-{{item}}.json"
when: '"pushgateway" in groups and env_list is defined'
with_items: "{{ env_list }}" ## 必须数组
vars:
current_env: "{{ item }}"

- name: generator pushgateway test manifest
template:
src: "pushgateway-test.json.j2"
dest: "{{pushgateway_manifest_dir}}/pushgateway-test-{{item}}.json"
when: '"pushgateway" in groups and env_list is defined'
with_items: "{{ env_list }}"
vars:
current_env: "{{ item }}"



host inventry


prometheus:
children:
prom_group_1:
hosts:
192.168.99.4:
prometheus_external_domain: http://192.168.99.4:9090
192.168.99.15:
prometheus_external_domain: http://192.168.99.15:9090
vars:
vm:
- prometheus
- alertmanager
- cassandra
- kafka
vm_env:
- qa
- test1
pushgateway:
- uvp
- am
pushgateway_env:
- qa
- test1
prom_group_2:
hosts:
192.168.99.18:
prometheus_external_domain: http://192.168.99.18:9090
192.168.99.16:
prometheus_external_domain: http://192.168.99.16:9090
vars:
vm:
- prometheus
- alertmanager
- cassandra
- kafka
vm_env:
- test1
pushgateway:
- uvp
- am
pushgateway_env:
- test1

pushgateway:
vars:
env_list:
- qa
- test
children:
pgw-group-1:
hosts:
pushgateway1:
expose_ports:
push: 12001
scrape: 13001
pushgateway2:
expose_ports:
push: 12002
scrape: 13002
pushgateway3:
expose_ports:
push: 12003
scrape: 13003
vars:
env: qa
host_ip: 192.168.101.6
cluster_url: cluster.com
pgw-group-2:
hosts:
pushgateway4:
expose_ports:
scrape: 12004
push: 13004
pushgateway5:
expose_ports:
scrape: 12005
push: 13005
vars:
env: test
host_ip: 192.168.101.6
cluster_url: cluster.com


alertmanager:
children:
am_ha_g1:
hosts:
192.168.99.6:
alertmanager_cluster_listen_address: 192.168.99.6:8001
alertmanager_external_domain: http://192.168.99.6:9093
192.168.99.17:
alertmanager_cluster_listen_address: 192.168.99.17:8001
alertmanager_peers: 192.168.99.17:8001
alertmanager_external_domain: http://192.168.99.17:9093
vars:
listen_port: 9093
alertmanager_cluster_port: 8001



template1


{% set counter = [] %}
{% set job_counter = [] %}
{% for pgw in groups['pushgateway'] %}
{% if hostvars[pgw]['env'] == current_env%}
{% if counter.append(1) %}{% endif %}
{% endif %}
{% endfor %}
{"services": {
{% if groups['pushgateway'] is defined%}
{% set index = 0 %}
{% for host in groups['pushgateway'] %}
{% if hostvars[host]['env'] == current_env%}
{% set push_groups_last = loop.last%}
"service::${APCERA_DEPLOY_NAMESPACE}::nginx-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-service": {
"description": "Nginx-{{host}} Generic Service http://{{hostvars[host]['host_ip']}}:{{hostvars[host]['expose_ports']['scrape']}}",
"type": "generic",
"params": {
"url": "http://{{hostvars[host]['host_ip']}}:{{hostvars[host]['expose_ports']['scrape']}}"
}
}
{% if index < counter|length %}, {% set index = index + 1 %}{% endif %}
{% endif %}
{%endfor%}
{% endif %}
"service::${APCERA_DEPLOY_NAMESPACE}::pushgateway-cluster-apcera-service": {
"description": "pushgateway-cluster-apcera-service",
"type": "generic",
"params": {
"url": "http://{%if ENV=='AWS' %}{{aws.pushgateway_cluster_service}}{%else%}{{cvc.pushgateway_cluster_service}}{%endif%}"
}
}},
"jobs":{
{% set job_index = 0 %}
{% for host in groups['pushgateway'] %}
{% if hostvars[host]['env'] == current_env%}
"job::${APCERA_DEPLOY_NAMESPACE}::vas-pm-prometheus-pushgateway-node-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}": {
"docker": {
"image": "${GLOBALENV_DOCKER_REGISTRY_BASE_URL}${IMAGE_NAME_TAG_VAS_PM_PROMETHEUS_PUSHGATEWAY}",
"password": "${GLOBALENV_DOCKER_REGISTRY_PASSWORD}",
"username": "${GLOBALENV_DOCKER_REGISTRY_USERNAME}"
},
"exposed_ports": [
80, 9091
],
"state": "started",
"env": {
"CLEANUP_INTERVAL_SECONDS": "60",
"PUSHGATEWAY_URL": "http://pushgateway-node-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-{%if ENV=='AWS' %}{{aws.pushgateway_node_endpoint}}{%else%}{{cvc.pushgateway_node_endpoint}}{%endif%}",
"METRIC_TIMEOUT_SECONDS": "{{metric_timeout}}",
"NGINX_SERVER_LIST":"{% for nginx_host in groups['pushgateway'] %}{% if hostvars[nginx_host]['env'] == current_env%}server {{hostvars[nginx_host]['host_ip']}}:{{hostvars[nginx_host]['expose_ports']['push']}};{%endif%}{% endfor %}"
},
"routes": [{
"type": "http",
"endpoint": "pushgateway-node-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-{%if ENV=='AWS' %}{{aws.pushgateway_node_endpoint}}{%else%}{{cvc.pushgateway_node_endpoint}}{%endif%}",
"config": {
"/": [{
"port": 9091
}]
}
},
{
"type": "http",
"endpoint": "{{hostvars[host]['cluster_url']}}",
"config": {
"/": [{
"port": 9091
}]
}
},
{
"type": "tcp",
"endpoint": "{{hostvars[host]['host_ip']}}:{{hostvars[host]['expose_ports']['scrape']}}",
"config": [{
"port": 9091
}]
},
{
"type": "tcp",
"endpoint": "{{hostvars[host]['host_ip']}}:{{hostvars[host]['expose_ports']['push']}}",
"config": [{
"port": 80
}]
}
],
"services": {
{% set service_index = 1 %}
{% for service_host in groups['pushgateway'] %}
{% if hostvars[service_host]['env'] == current_env%}
"NGINX-{{hostvars[service_host]['host_ip']}}-{{hostvars[service_host]['expose_ports']['scrape']}}-SERVICE": {
"fqn": "service::${APCERA_DEPLOY_NAMESPACE}::nginx-{{hostvars[service_host]['host_ip']}}-{{hostvars[service_host]['expose_ports']['scrape']}}-service"
}
{% if service_index < counter|length %}, {% set service_index = service_index + 1 %}{% endif %}
{% endif %}
{%endfor%}
"CLUSTER-SERVICE": {
"fqn": "service::${APCERA_DEPLOY_NAMESPACE}::pushgateway-cluster-apcera-service"
}

}
,
"restart_mode": "always",
"ssh": true,
"start": {
"timeout": 30
}
}
{% if job_counter.append(1) %}{% endif %}
{% if job_counter|length < counter|length %}, {% endif %}
{% endif %}
{%endfor%}
}
}



template2


{% set counter = [] %}
{% set route_counter = [] %}
{% for pgw in groups['pushgateway'] %}
{% if hostvars[pgw]['env'] == current_env%}
{% if counter.append(1) %}{% endif %}
{% endif %}
{% endfor %}
{
"jobs": {
"job::${APCERA_DEPLOY_NAMESPACE}::pushgateway-docker-auto-test": {
"docker": {
"image": "${GLOBALENV_DOCKER_REGISTRY_BASE_URL}${IMAGE_NAME_TAG_PUSHGATEWAY_DOCKER_AUTO_TEST}",
"password": "${GLOBALENV_DOCKER_REGISTRY_PASSWORD}",
"username": "${GLOBALENV_DOCKER_REGISTRY_USERNAME}"
},
"exposed_ports": [80, 9093],
"state": "started",
"env": {
"PROMETHEUS_PM_URI": "{%if ENV=='AWS' %}{{aws.pushgateway_cluster_service}}{%else%}{{cvc.pushgateway_cluster_service}}{%endif%}",
"PUSHGATEWAY_IPS": "{% for host in groups['pushgateway'] %}{% if hostvars[host]['env'] == current_env%}{% set push_groups_last = loop.last%}pushgateway-node-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-{%if ENV=='AWS' %}{{aws.pushgateway_node_endpoint}}{%else%}{{cvc.pushgateway_node_endpoint}}{%endif%}{% if ( push_groups_last==False) %},{% endif %}{% endif %}{%endfor%}"
},
"routes": [
{
"type": "http",
{% for host in groups['pushgateway'] %}
{% if hostvars[host]['env'] == current_env and loop.index <=1 %}
"endpoint": "{{hostvars[host]['cluster_url']}}",
{% endif %}
{%endfor%}
"config": {
"/": [{
"port": 80
}]
}
},

{% for host in groups['pushgateway'] %}
{% if hostvars[host]['env'] == current_env%}
{
"type": "tcp",
"endpoint": "{{hostvars[host]['host_ip']}}:{{hostvars[host]['expose_ports']['scrape']}}",
"config": [{
"port": 80
}]
}
{% if route_counter.append(1) %}{% endif %}
{% if route_counter|length < counter|length %}, {% endif %}
{% endif %}
{%endfor%}

],

"services":{
{% set service_index = 0 %}
{% for host in groups['pushgateway'] %}
{% if hostvars[host]['env'] == current_env%}
"NGINX-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-SERVICE": {
"fqn": "service::${APCERA_DEPLOY_NAMESPACE}::nginx-{{hostvars[host]['host_ip']}}-{{hostvars[host]['expose_ports']['scrape']}}-service"
}
{% set service_index = service_index + 1 %}
{% if service_index < counter|length %}, {% endif %}
{% endif %}
{%endfor%}

"CLUSTER-SERVICE": {
"fqn": "service::${APCERA_DEPLOY_NAMESPACE}::pushgateway-cluster-apcera-service"
}
},
"restart_mode": "always",
"ssh": true,
"start": {
"timeout": 30
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

annan211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值