泰安招聘网站有哪些,网站建设与管理基础及实训电子版,石家庄发布最新消息,旅游景点网站设计Ansible-Jinja2 一、Ansible Jinja2模板背景介绍二、 JinJa2 模板2.1 JinJa2 是什么2.2 JinJa2逻辑控制 三、如何使用模板四、实例演示 按顺序食用#xff0c;口味更佳 ( 1 ) ⾃动化运维利器Ansible-基础 ( 2 ) ⾃动化运维利器 Ansible-Playbook ( 3 ) ⾃动化运维利器 Ansible… Ansible-Jinja2 一、Ansible Jinja2模板背景介绍二、 JinJa2 模板2.1 JinJa2 是什么2.2 JinJa2逻辑控制 三、如何使用模板四、实例演示 按顺序食用口味更佳 ( 1 ) ⾃动化运维利器Ansible-基础 ( 2 ) ⾃动化运维利器 Ansible-Playbook ( 3 ) ⾃动化运维利器 Ansible-变量 ( 4 ) ⾃动化运维利器AnsiblePlaybook的任务控制 ( 5 ) ⾃动化运维利器 Ansible-Jinja2 ( 6 ) ⾃动化运维利器 Ansible-最佳实战
一、Ansible Jinja2模板背景介绍
目前Nginx的配置⽂件在所有的服务器上都是相同的,但我希望能根据每⼀台服务器的性能去定制服务的启动进程。 同时定制每⼀台Nginx服务的响应头以便于当某台服务出现问题时能快速定位到具体的服务器。 要做这样的定制势必会导致⼀个问题Nginx 在每台物理服务器上的配置⽂件都不⼀样这样的配置⽂件如何管理呢? 再使⽤copy 模块去做管理显然已经不合适。此时使⽤Ansible 提供的另⼀个模板(template) 功能它可以帮助我们完美的解决问题。
二、 JinJa2 模板
Ansible 中的模板(template)的使⽤前提我们必须要学会JinJa2模板。学会了它就相当于我们学会了Ansible 模板。
2.1 JinJa2 是什么
Jinja2是基于Python书写的模板引擎。功能⽐较类似于PHP的smarty模板。
jinja2 ⽂件以 .j2 为后缀 也可以不写后缀jinja2 中存在 三种定界符 注释: {# 注释内容 #}变量引⽤: {{ var }}逻辑表达: {% %}
2.2 JinJa2逻辑控制
条件表达
{% if %}
...
{% elif %}
...
{% else %}
...
{% endif %}Example
{# 如果定义了 idc 变量 则输出 #}
{% if idc is defined %}
{{ idc }}
{% elif %}没有定义
{% endif %}循环控制
{% for %}
...
...
{% endfor %}Example
{# 列举出 dbservers 这个 group 中的所有主机 #}
{% for host in groups[dbservers] %}
{{ host }}
{% endfor %}{#与Python 语法不通模板中的循环内不能break或continue#}
{#你可以在迭代中过滤序列来跳过某些项#}
{#打印dbservers 组中的所有主机但是不打印1.1.1.1 这台主机#}
{% for host in groups[dbservers] if host !1.1.1.1 %}
{{host}}
{% endfor %}三、如何使用模板
⼀个基于Facts的Jinja2 实例
# cat config.j2
{# use variable example #}
wlecome host {{ ansible_hostname }},
os is {{ansible_os_family }}
today is {{ ansible_date_time.date }}
cpucore numbers {{ ansible_processor_vcpus }}{# use condition example #}
{% if ansible_processor_vcpus 1 %}
OS CPU more than one core
{% endif %}{% for m in ansible_mounts if m[mount] ! / %}
mount {{ m[mount] }},
total size is{{m[size_total]}},
free size is {{m[size_available]}}
{% endfor %}在Ansible 中使⽤模板
---
- name: a template examplehosts: allremote_user: roottasks:- name: update jinja2 configtemplate: srcconfig.j2 dest/tmp/config.conf四、实例演示
Jinja2 模板以及如何在Ansible中使⽤模板已经介绍完了。那么如何去实现我们的需求呢?
nginx.conf.j2
user nginx;
{# start process equal cpu cores #}
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {worker_connections 1024;
}
http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main $remote_addr - $remote_user[$time_local] $request $status $body_bytes_sent$http_referer $http_user_agent.... // 此处配置就省略了...{# add_header {{ ansible_hostname }}; #}add_header x-hostname {{ ansible_hostname }};include /etc/nginx/conf.d/*.conf;
}上述是一个nginx的配置文件使用JinJa2模板后ansible就能够通过{{arg}}使用对应节点的变量而不是写死的数据。 改进后的Playbook
- name: template playbook examplehosts: webserversvars:createuser:- tomcat- www- mysqltasks:- name: create useruser: name{{ item }} statepresentwith_items: {{ createuser }}- name: yum nginx webserveryum: namenginx statepresent# use ansible template- name: update nginx main configtemplate:src: nginx.conf.j2dest: /etc/nginx/nginx.conftags: updateconfignotify: reload nginx server- name: add virtualhost configcopy:src: www.qfedu.com.confdest: /etc/nginx/conf.d/tags: updateconfignotify: reload nginx server- name: check nginx syntaxshell: /usr/sbin/nginx -tregister: nginxsyntaxtags: updateconfig- name: check nginx runningstat: path/var/run/nginx.pidregister: nginxrunningtags: updateconfig- name: print nginx syntaxdebug: varnginxsyntax- name: start nginx serverservice: namenginx statestartedwhen:- nginxsyntax.rc 0- nginxrunning.stat.exists falsehandlers:- name: reload nginx serverservice: namenginx statestartedwhen:- nginxsyntax.rc 0- nginxrunning.stat.exists true执⾏还是按照原来的⽅式执⾏即可
使用JinJa2模板就不可以不必每个资产节点都准备一份配置文件ansible能够使用template注入变量为节点定制不同的配置。
按顺序食用口味更佳 ( 1 ) ⾃动化运维利器Ansible-基础 ( 2 ) ⾃动化运维利器 Ansible-Playbook ( 3 ) ⾃动化运维利器 Ansible-变量 ( 4 ) ⾃动化运维利器AnsiblePlaybook的任务控制 ( 5 ) ⾃动化运维利器 Ansible-Jinja2 ( 6 ) ⾃动化运维利器 Ansible-最佳实战