14.Ansible jinja2
14.1.jinja2基本语法
14.2.{{}}的使用
14.3.变量
14.4.表达式
14.5.逻辑运算
14.6.算术运算
14.7.操作列表
14.8.str, num, tuple, dic, boolean
14.9.将变量的选取写到文件中,然后在playbook中定义变量并使用文件中选取的内容
14.Ansible jinja2
以下转自:https://blog.csdn.net/weixin_43384009/article/details/105301811
14.1.jinja2基本语法
{{ }}:用来装载表达式,比如变量、运算表达式、比较表达式等。
{% %}: 用来装载控制语句,比如if控制结构,for循环控制结构。
{# #}: 用来装载注释,模板文件被渲染后,注释不会包含在最终的文件中。
14.2.{{}}的使用
14.3.变量
[root@node2 workspace]# cat test.j2
westos python
test jinjia2
test {{ testvar1 }}
[root@node2 workspace]# ansible abc -m template -e "testvar1=haha" -a "src=test.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "15314d70f8f0873cfd7e37cef70bafcd688426a7",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "2f6a8291e0162f577bf3ab516844410b",
"mode": "0644",
"owner": "root",
"size": 37,
"src": "/root/.ansible/tmp/ansible-tmp-1635211952.41-56-170078656065764/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
westos python
test jinjia2
test haha
14.4.表达式
[root@node2 workspace]# cat test1.j2
{{1==1}}
{{2!=2}}
{{2>1}}
{{3<4}}
{{2>=1}}
[root@node2 workspace]# ansible abc -m template -a "src=test1.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "6b7c9b9fb417f9e835aa8a6cb142f19c8d382447",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "8c0c8e0e684394aed401871cdb7acb49",
"mode": "0644",
"owner": "root",
"size": 26,
"src": "/root/.ansible/tmp/ansible-tmp-1635212666.88-200-154178829619445/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
True
False
True
True
True
14.5.逻辑运算
[root@node2 workspace]# cat test2.j2
{{ (2>1) or (3<2) }}
{{ (2>1) or (3<4) }}
{{ not true }}
{{ not false }}
[root@node2 workspace]# ansible abc -m template -a "src=test2.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "df794ac5199e06e48572cebd52f8cb3f1612bc27",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "117dfe113419591e9f565977700c072a",
"mode": "0644",
"owner": "root",
"size": 22,
"src": "/root/.ansible/tmp/ansible-tmp-1635212868.57-338-278799231488357/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
True
True
False
True
[root@node2 workspace]#
14.6.算术运算
[root@node2 workspace]# cat test3.j2
{{ 3 + 10}}
{{ 4 - 1 }}
{{ 2**3 }}
{{ 10/2 }}
[root@node2 workspace]# ansible abc -m template -a "src=test3.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "7b7425bf25a09ea4acba2e427f812b48d263fffe",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "272c55d18eb323f606ea1b2cedbf72e8",
"mode": "0644",
"owner": "root",
"size": 11,
"src": "/root/.ansible/tmp/ansible-tmp-1635213043.42-475-43423369309717/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
13
3
8
5.0
[root@node2 workspace]#
14.7.操作列表
[root@node2 workspace]# cat test4.j2
{{ 1 in [1,2,3,4]}}
{{ 1 not in [1,2,3,4] }}
[root@node2 workspace]# ansible abc -m template -a "src=test4.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "df98bbf81d4ac27e458d1d913a236ac6defff24c",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "ff133012d10577f7ee4a7afd3486da5a",
"mode": "0644",
"owner": "root",
"size": 11,
"src": "/root/.ansible/tmp/ansible-tmp-1635213172.71-612-67218511311884/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
True
False
[root@node2 workspace]#
14.8.str, num, tuple, dic, boolean
[root@node2 workspace]# cat test3.j2
### str
{{ 'teststring' }}
{{ 'teststring' }}
### num
{{ 12 }}
{{ 12.5 }}
### list
{{ ['Aa','Bb','Cc'] }}
{{ ['Aa','Bb','Cc'][1] }}
{{ ['Aa','Bb','Cc'].1 }}
### tuple
{{ ('Aa','Bb','Cc') }}
{{ ('Aa','Bb','Cc').0 }}
{{ ('Aa','Bb','Cc')[0] }}
### dict
{{ {'name':'zjy','age':12} }}
{{ {'name':'zjy','age':12}.name }}
{{ {'name':'zjy','age':12}['name'] }}
### boolean
{{ True }}
{{ true }}
{{ False }}
{{ false }}
[root@node2 workspace]# ansible abc -m template -a "src=test3.j2 dest=/opt/test"
172.17.0.3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "aeeafcde9c0b3a7ab528ef979c9c0d1de4601a32",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "4fea9c400c3ec956e2f82661a7c8e155",
"mode": "0644",
"owner": "root",
"size": 193,
"src": "/root/.ansible/tmp/ansible-tmp-1635213780.94-752-77704245938119/source",
"state": "file",
"uid": 0
}
[root@node2 workspace]# cat /opt/test
### str
teststring
teststring
### num
12
12.5
### list
['Aa', 'Bb', 'Cc']
Bb
Bb
### tuple
('Aa', 'Bb', 'Cc')
Aa
Aa
### dict
{'age': 12, 'name': 'zjy'}
zjy
zjy
### boolean
True
True
False
False
[root@node2 workspace]#
14.9.将变量的选取写到文件中,然后在playbook中定义变量并使用文件中选取的内容
[root@node2 workspace]# pwd
/root/workspace
[root@node2 workspace]# ls
demo-0.0.1-SNAPSHOT.jar demo.log log start.sh temptest1.yml test4.j2
[root@node2 workspace]# cat test4.j2
{{ teststr }}
{{ testnum }}
{{ testlist[1] }}
{{ test1list[1] }}
{{ testdic['name'] }}
[root@node2 workspace]# cat temptest1.yml
---
- hosts: abc
remote_user: root
gather_facts: no
vars:
teststr: 'hahaxixi'
testnum: 18
testlist: ['Aa','Bb','Cc']
test1list:
- aA
- bB
- cC
testdic:
name: zjy
age: 18
tasks:
- template:
src: /root/workspace/test4.j2
dest: /opt/test
[root@node2 workspace]# ansible-playbook temptest1.yml
PLAY [abc] ********************************************************************************************
TASK [template] ***************************************************************************************
ok: [172.17.0.3]
PLAY RECAP ********************************************************************************************
172.17.0.3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@node2 workspace]# cat /opt/test
hahaxixi
18
Bb
bB
zjy
[root@node2 workspace]#