`
annan211
  • 浏览: 460639 次
  • 性别: 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
			}
		}
	}
}
分享到:
评论

相关推荐

    python包jinja2

    **Python包Jinja2详解** Jinja2是一个强大的、现代的、设计者友好的模板引擎,它是用Python语言编写的,被广泛应用于Web应用开发中,以生成动态HTML、XML或其他标记语言。Jinja2的设计灵感来源于Django模板语言,但...

    python 利用jinja2模板生成html代码实例

    Jinja2是一个流行的模板引擎,用于Python编程语言,广泛用于Web开发框架如Flask和Django中。其核心功能是将数据与模板结合生成HTML页面或其他格式的文档。 在本文中,会通过实例代码详细解释如何使用Jinja2来生成...

    Python-JinjaSQL使用Jinja模板生成SQL

    它允许我们在Python中使用模板语言来动态生成SQL,充分利用Jinja2的灵活性和强大功能。结合Python的数据库驱动,如`psycopg2`、`pyodbc`等,我们可以构建出高效且安全的数据库操作代码。在实际项目中,合理使用`...

    python-jinja2-26-2.6-3.el6.noarch.rpm

    python-jinja2-26-2.6-3.el6.noarch.rpm

    python2-jinja2-2.10-2.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    python-jinja2-2.7.2-4.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    jinja2-python27

    Jinja2 is a full featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.

    Python库 | jinja2_ospath-0.1.0-py2.py3-none-any.whl

    **Python库 - Jinja2与ospath** Jinja2是一个强大的、现代的、设计者友好的模板引擎,它是用Python语言编写的。Jinja2基于成熟的Mako模板引擎,为Python开发者提供了高度灵活的模板设计和渲染能力。它广泛应用于Web...

    jinja2简单示例

    **Jinja2**是Python中一个强大的模板引擎,被广泛应用于Web开发中,尤其是与Flask和Django等框架结合使用。它提供了丰富的语法结构,允许开发者以清晰、简洁的方式编写模板,用于生成动态HTML或其他格式的文档。下面...

    python2-jinja2-2.8.1-1.el7.noarch.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    Python-Jinja2是纯Python的模板引擎

    **Python-Jinja2模板引擎详解** Jinja2是一款强大的、高效且灵活的纯Python模板引擎,它是基于Python语言设计的,无需任何外部依赖。由Pallets团队开发,广泛应用于Web应用开发,如Flask框架中。Jinja2的设计灵感...

    Python库 | pyramid_jinja2-1.9.zip

    Jinja2是一个现代的、设计者友好的模板语言,其设计灵感来源于Django模板语言和Python本身。它具有清晰的语法,易于阅读和编写,并且支持继承、宏、过滤器、表达式和控制结构等高级特性。通过pyramid_jinja2库,...

    python-docx-template:使用docx作为jinja2模板

    使用docx作为jinja2模板 介绍 该软件包使用2个主要软件包: 用于读取,编写和创建子文档的python-docx jinja2用于管理插入到模板docx中的标签 之所以创建python-docx-template,是因为python-docx具有创建文档但...

    基于Python Web 实战,Flask + Jinja2 + Bootstrap 开发的招聘网站,本科毕业设计项目,课设作业

    基于 Flask / Jinja2 / Bootstrap / MySQL 开发,仿照拉勾网的风格,实现了招聘网站的必需功能 环境 Python 3 MySQL 快速开始 1. 安装 Python 依赖 $ pip3 install -r requirements.txt 2. 修改配置文件 根据自己...

    python Jinja2==3.0.3

    odoo包

    jinja2 chm python

    Jinja2 是一个 Python 的功能齐全的模板引擎。它有完整的 unicode 支持,一个可选 的集成沙箱执行环境,被广泛使用,以 BSD 许可证授权。

    jinja2js:一个让你将 Jinja2 模板编译为 JavaScript 的项目

    python compile_to_js.py /path/to/jinja2/template 上面的命令将产生一个 JavaScript 函数,当使用包含对应于模板请求的值作为第一个参数的值的 JSON blob 执行时,将产生模板的呈现版本。 将--attributes标志...

    Jinja2中文文档

    Jinja2是一种现代、设计者友好的Python模板引擎,它模仿了Django模板系统的设计理念,但又在此基础上进行了许多改进与优化。Jinja2的主要特点包括: - **高性能**:Jinja2在设计上特别注重性能优化,能够快速生成...

    Python使用Jinja2模板.docx

    Python使用Jinja2模板.docx

Global site tag (gtag.js) - Google Analytics