首页 > 其他分享 >ansible template

ansible template

时间:2022-09-28 14:11:05浏览次数:62  
标签:name server nginx ansible template root listen

目录

ansible template

template介绍

Jinja是基于Python的模板引擎。template类是Jinja的另一个重要组件,可以看作一个编译过的模块文件,用来生产目标文本,传递Python的变量给模板去替换模板中的标记。

他与ansible的copy模块功能相似,都是拷贝文件到目标机器,唯一区别是

  • copy:不支持变量
  • template:支持变量,可以把配置文件根据变量渲染后再发送到目标机器

实例

实例1

cat temnginx.yaml 
---
- hosts: test
  remote_user: root
  vars: 
​    nginx_vhosts:
​      - listen: 80
  tasks:
​    - name: config file 
​      template: src=./template/nginx.conf.j2 dest=/data/nginx.conf
 cat template/nginx.conf.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
}
{% endfor %}
生成的结果
cat nginx.conf 
server {
  listen 80
}

实例2

cat temnginx2.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - 81
​      - 82 
​      - 83
  tasks:
​    - name: template config
​      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

cat nginx.conf.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
}
{% endfor %}
生成的结果
server {
  listen 81
}
server {
  listen 82
}
server {
  listen 83
}

实例3

cat temnginx3.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:  
​      - listen: 8080
​        server_name: "web1.baidu.com"
​        root: /var/www/nginx/web1/
​      - listen: 8081
​        server_name: "web1.baidu.com"
​        root: "/var/www/nginx/web2/"
  tasks:
​    - name: template config
​      template: src=./template/nginx.conf2.j2 dest=/data/nginx2.conf

cat nginx.conf2.j2
{% for vhost in nginx_vhosts %}
server{
  listen {{ vhost.listen }}
  server_name {{ vhost.server_name }}
  root {{ vhost.root }}
}
{% endfor %}

实例4

在模板文件中还可以使用if条件判断,决定是否生成相关的配置信息

cat templnginx4.yaml 
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - web1:
​        listen: 8080
​        root: "/var/www/nginx/web1/"
​      - web2: 
​        listen: 8080
​        server_name: "web2.baidu.com"
​        root: "/var/www/nginx/web2/"
​      - web3: 
​        listen: 8080
​        server_name: "web3.baidu.com"
​        root: "/var/www/nginx/web3/"
  tasks:
​    - name: template config to 
​      template: src=./template/nginx.conf3.j2 dest=/data/nginx3.conf

cat template/nginx.conf3.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
  {% if vhost.server_name is defined %}
server_name {{ vhost.server_name }}
  {% endif %}
root {{ vhost.root }}
}
{% endfor %}

输出结果:
cat nginx3.conf 
server {
  listen 8080
  root /var/www/nginx/web1/
}

server {
  listen 8080
  server_name web2.baidu.com
  root /var/www/nginx/web2/
}
server {
  listen 8080
  server_name web3.baidu.com
  root /var/www/nginx/web3/
}

标签:name,server,nginx,ansible,template,root,listen
From: https://www.cnblogs.com/liwenchao1995/p/16737840.html

相关文章

  • ansible 二进制安装mysql
    1、编辑mysql.sh脚本vimmysql.sql【#/bin/bash#脚本安装mysql,上传安装包至/rootcd/root#安装日志mysql_log=/root/mysql.log#mysql安装包名mysql_package=mysql-8......
  • ansible 修改/etc/hosts
    1、编辑文件hosts.tmlvim hosts.tml【----hosts: 192.168.59.103 remote_user:root tasks:  -name:addhosts   lineinfile:name=/etc/hostslin......
  • ansible 安装jdk
    1、上传文件到ansible端上/data/jdk/jdk-8u341-linux-x64.tar.gz(下载地址:)2、编辑jdk.yml文件vimjdk.yml【----hosts:192.168.59.103 remote_user:root tasks......
  • ansible 编译安装nginx
    1、准备安装包nginx-1.22.0.tar.gz(地址:https://nginx.org/download/)2、编写剧本vimnginx.yml【----hosts:192.168.59.103 remote_user:root tasks:  -n......
  • Ansible简介
    Ansible是一种常用的自动运维化工具,基于python开发,分布式,无需客户端,轻量级,配置语言采用YAML。Ansible 的特性:1.模块化:调用特定的模块,完成特殊的任务。2.Paramiko(......
  • ansible 安装docker redis
    1、编辑redis.conf配置文件为了后期修改配置,先写一个配置。后期一般要修改配置的,建议先写一个【#库的数量,默认是16databases32】2、编辑剧本vimredis.yml【 #......
  • 关于WPF自定义控件OnApplyTemplate不执行,手动调用Template.FindName返回空的问题
    我在wpf项目中手写了一个自定义控件,运行得相当的正常,后续调用时,反复遇到问题,前前后后折腾了好几次代码publicExtendCombox(){Loaded+=(e......
  • ansible主机清单inventory
     一、Inventory主机清单1>ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventoryfile中将其分组命名2>默认的inventoryfile为/etc/ansi......
  • 模板方法模式 Template Method
    “组件协作”模式:现代软件专业分工之后的第一个结果是“框架与应用程序的划分”,“组件协作”模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的......
  • 安装项目模板IdentityServer4.Templates
    打开PowseShell执行以下命令dotnetnew-iIdentityServer4.Templates  ......