自动化运维工具Ansible(10)Jinja2模板

1.Ansible Jinja2模板场景介绍

目前Nginx关于设置设备的文献与所有办事器相反,但是能够按照各个办事器的功用来确定礼品办事的启动流程。同时设想了每个Nginx办事的echo head,使适当某个办事呈现成就时,能够快速定位到详细的办事器。停止如许的定造必然会有所成就。关于Nginx在各个物理办事器上的设置设备,有良多差别的文档。若何处置那类关于设置设备的文件?然后利用复造模块来处置明显的差别。此时利用Ansible供给的另一个模板函数来处置成果。

2.JinJa2模板

Jinja2是一个基于Python转录的模板引擎。与类似PHP的smarty模板比拟。

jinja2 文献以 `.j2` 为后缀, 也可以没有写后缀。jinja2 中生活 三种定界符注释: {# 注释本色 #} 变量援用: {{ var }} 逻辑剖明: {% %}

金甲2逻辑控造

{% if %}...{% elif %}...{% else %}...{% endif %}{# 假设定义了 idc 变量, 则输入 #}{% if idc is defined %}{{ idc }}{% elif %} 不定义{% endif %}

轮回控造

{% for %}......{% endfor %}{# 枚举出 dbservers 那个 group 中的一切主机 #}{% for host in groups['dbservers'] %}{{ host }}{% endfor %}

3.利用模板

一个基于事实的jinja 2

{# 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 example hosts: all remote_user: root tasks: - name: update jinja2 config template: src=config.j2 dest=/tmp/config.conf...

4.示范[root@localhost home]# vim nginx.conf.j2user 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" "$http_x_forwarded_for"'; sendfile on; tcp_nopush on; keepalive_timeout 0; gzip on; gzip_min_length 1k; gzip_buffers 8 64k; gzip_http_version 1.0; gzip_comp_level 5; gzip_types text/plain application/x-javascript text/css application/json application/xml application/x-shockwave-flash application/javascript image/svg+xml image/x-icon; gzip_vary on; {# add_header {{ ansible_hostname }}; #} add_header x-hostname {{ ansible_hostname }}; include /etc/nginx/conf.d/*.conf;}

优化剧本并撑持模板

- name: template playbook example hosts: webservers vars: createuser: - tomcat - www - mysql tasks: - name: create user user: name={{ item }} state=present with_items: "{{ createuser }}" - name: yum nginx webserver yum: name=nginx state=present name: yum nginx webserver yum: name=nginx state=present src: nginx.conf.j2 dest: /etc/nginx/nginx.conf tags: updateconfig notify: reload nginx server - name: add virtualhost config copy: src: www.qfedu.com.conf dest: /etc/nginx/conf.d/ tags: updateconfig notify: reload nginx server - name: check nginx syntax shell: /usr/sbin/nginx -t register: nginxsyntax tags: updateconfig - name: check nginx running stat: path=/var/run/nginx.pid register: nginxrunning tags: updateconfig - name: print nginx syntax debug: var=nginxsyntax - name: start nginx server service: name=nginx state=started when: - nginxsyntax.rc == 0 - nginxrunning.stat.exists == false handlers: - name: reload nginx server service: name=nginx state=started when: - nginxsyntax.rc == 0 - nginxrunning.stat.exists == true

您可以还会对下面的文章感兴趣:

最新评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。