加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

ansible 与 Jinja2的结合

发布时间:2020-12-14 01:29:29 所属栏目:Linux 来源:网络整理
导读:1.文件架构 [[email?protected] template] # tree .├── jinj2_test.yml├── meta├── tasks├── templates│?? └── test3.j2└── vars 2.?test3.j2的内容 [[email?protected] template] # cat templates/test3.j2 {% if PORT % }bind_address

1.文件架构

[[email?protected] template]# tree
.
├── jinj2_test.yml
├── meta
├── tasks
├── templates
│?? └── test3.j2
└── vars

2.?test3.j2的内容

[[email?protected] template]# cat templates/test3.j2 
{% if PORT %}
bind_address={{ansible_eth0.ipv4.address}}:{{ PORT }}
{% else %}
bind_address={{ansible_eth0.ipv4.address}}:3306
{% endif %}

my_ip is: {{ansible_eth0.ipv4.address}}

3.?jinj2_test.yml的内容

[[email?protected] template]# cat jinj2_test.yml 
---
- hosts: test
  user: root
  gather_facts: true
  vars:
    PORT: 3136
  tasks:
    - name: copy file to client
      template: src=/roles/template/templates/test3.j2 dest=/root/my.cnf

4.运行的结果

[[email?protected] template]# ansible test -a ‘cat /root/my.cnf‘
10.2.1.51 | CHANGED | rc=0 >>
bind_address=10.2.1.51:3136

my_ip is: 10.2.1.51

10.2.1.230 | CHANGED | rc=0 >>
bind_address=10.2.1.230:3136

my_ip is: 10.2.1.230

?参考:https://blog.csdn.net/qqhappy8/article/details/79217380

https://blog.51cto.com/linuxg/1788574

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读