首页 > 其他分享 >template(12)

template(12)

时间:2022-11-22 14:45:15浏览次数:38  
标签:12 name j2 nginx ansible conf template

template

template功能:根据模块文件动态生成对应的配置文件

template文件必须存放于templates目录下,且命名为 .j2 结尾
yaml/yml 文件需和templates目录平级,目录结构如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2

示例
利用template 同步nginx配置文件

准备templates/nginx.conf.j2文件
vim temnginx.yml
- hosts: webserver
  remote_user: root
  
  tasks:
    - name: template config to remote hosts
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

ansible-playbook temnginx.yml

template变更替换

范例:

使用pstree查看进程需要安装 yum -y install psmisc

#修改文件nginx.conf.j2  #在这里需要自己安装一个nginx然后进行配置文件的复制,复制为j2文件
mkdir templates
vim templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};

vim temnginx2.yml
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: install nginx
      yum: name=nginx
    - name: template config to remote hosts
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf 
    - name: start service
      service: name=nginx state=started enabled=yes

ansible-playbook temnginx2.yml

template算术运算

范例

vim nginx.conf.j2 
worker_processes {{ ansible_processor_vcpus**2 }};    #这里是对cpu进行相关的运算
worker_processes {{ ansible_processor_vcpus+2 }}; 

范例:

[root@ansible ansible]#vim templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus**3 }};#和上边一样,对cpu进行算数运算

[root@ansible ansible]#cat templnginx.yml
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: install nginx
      yum: name=nginx
    - name: template config to remote hosts
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart nginx
    - name: start service
      service: name=nginx state=started enabled=yes

  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

ansible-playbook  templnginx.yml --limit 10.0.0.8

标签:12,name,j2,nginx,ansible,conf,template
From: https://www.cnblogs.com/yutoujun/p/16915053.html

相关文章

  • template流程控制for和if(12)
    template流程控制for和iftemplate中也可以使用流程控制for循环和if条件判断,实现动态生成文件功能for当我们需要创建如下类型的文件时server{listen80}server{......
  • 20221122 常用MySQL查询
    查询版本selectversion();查询表名和表注释selectTABLE_NAME,TABLE_COMMENTfromINFORMATION_SCHEMA.Tableswheretable_schema='daoancomp';查询表字段和注......
  • Template Method设计模式
    TemplateMethod动机(Motivation)在软件构建过程中,对于某一项任务,它常常有稳定的整体操作结构,但各个子步骤却有很多改变的需求,或者由于固有的原因(比如框架与应用之间的关系)而......
  • 如何修改服务器的用户名和密码Windows2008/2012
    在我们刚拿到服务器的时候密码都是系统随机生成的初始密码,所以这时候我们一般第一时间拿到服务器应该为了安全起见先修改服务器密码以免密码泄露避免发生一些没必要的事情。......
  • 【华为OJ12】字符串反转
    题目描述写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。例如:importjava.util.Scanner;publicclassMain{publicstaticStringreverseStr(String......
  • 前端012-css-后台布局1
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>css后台布局</title><style>body{margin:0}/*去掉边框,*/.page-heade......
  • TM4C123G学习记录(4)--关于ROM前缀函数和HWREG函数
    为了准备电赛临时学一下TM4C123G,简单记录学习内容大家可以在​​这里​​下载我收集的资源,非常全面,花了很大功夫收集来的,还有书籍、例程代码等还可以在TI官网下载相关文档​......
  • TM4C123G学习记录(3)--外部中断
    为了准备电赛临时学一下TM4C123G,简单记录学习内容大家可以在​​这里​​下载我收集的资源,非常全面,花了很大功夫收集来的,还有书籍、例程代码等还可以在TI官网下载相关文档​......
  • TM4C123G学习记录(2)--GPIO
    为了准备电赛临时学一下TM4C123G,简单记录学习内容大家可以在​​这里​​下载我收集的资源,非常全面,花了很大功夫收集来的,还有书籍、例程代码等还可以在TI官网下载相关文档​......
  • TM4C123G学习记录(1)--时钟
    为了准备电赛临时学一下TM4C123G,简单记录学习内容大家可以在​​这里​​下载我收集的资源,非常全面,花了很大功夫收集来的,还有书籍、例程代码等还可以在TI官网下载相关文档​......