11_Ansible jinja2, basic syntax, use of {{}}, variables, expressions, logical operations, arithmetic operations, operation lists, str, num, tuple, dic, boolean,

14. Ansible jinja2
14.1. Jinja2 Basic Syntax
14.2. Use of { {}}
14.3. Variables
14.4. Expressions 14.5
. Logical operations
14.6. Arithmetic operations
14.7. Operation lists
14.8.str, num, tuple, dic, boolean
14.9. Will The selection of variables is written to the file, and then the variable is defined in the playbook and the content selected in the file is used

14.Ansible jinja2

The following is transferred from: https://blog.csdn.net/weixin_43384009/article/details/105301811

14.1. Jinja2 Basic Syntax

{ { }}: Used to load expressions, such as variables, operation expressions, comparison expressions, etc.
{% %}: used to load control statements, such as if control structure, for loop control structure.
{# #}: Used to load comments. After the template file is rendered, the comments will not be included in the final file.

14.2. Use of {{}}

14.3. Variables

[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. Expressions

[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. Logical operations

[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. Arithmetic operations

[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. Operation list

[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. Write the variable selection to the file, then define the variable in the playbook and use the content selected in the file

[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]#

Guess you like

Origin blog.csdn.net/toto1297488504/article/details/132235002