ansible 与 Jinja2的结合

1.文件架构

[root@master template]# tree
.
├── jinj2_test.yml
├── meta
├── tasks
├── templates
│   └── test3.j2
└── vars

2. test3.j2的内容

[root@master 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的内容

[root@master 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.运行的结果

[root@master 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

猜你喜欢

转载自www.cnblogs.com/hixiaowei/p/10789596.html