`
annan211
  • 浏览: 446452 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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
			}
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics