日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ansible PLaybook template 模板详解

發布時間:2025/1/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ansible PLaybook template 模板详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

template 模板

模板是一個文本文件,可以做為生成文件的模版,并且模板文件中還可嵌套jinja語法

jinja2語言

網站:https://jinja.palletsprojects.com/en/2.11.x/

jinja2 語言使用字面量,有下面形式:
字符串:使用單引號或雙引號
數字:整數,浮點數
列表:[item1, item2, …]
元組:(item1, item2, …)
字典:{key1:value1, key2:value2, …}
布爾型:true/false
算術運算:+, -, *, /, //, %, **
比較操作:==, !=, >, >=, <, <=
邏輯運算:and,or,not
流表達式:For,If,When

字面量:

表達式最簡單的形式就是字面量。字面量表示諸如字符串和數值的 Python 對象。如“Hello World”
雙引號或單引號中間的一切都是字符串。無論何時你需要在模板中使用一個字符串(比如函數調用、過濾器或只是包含或繼承一個模板的參數),如42,42.23
數值可以為整數和浮點數。如果有小數點,則為浮點數,否則為整數。在 Python 里, 42 和 42.0 是不一樣的

算術運算:

Jinja 允許用計算值。支持下面的運算符
+:把兩個對象加到一起。通常對象是素質,但是如果兩者是字符串或列表,你可以用這 種方式來銜接它們。無論如何這不是首選的連接字符串的方式!連接字符串見 ~ 運算符。 {{ 1 + 1 }} 等于 2
-:用第一個數減去第二個數。 {{ 3 – 2 }} 等于 1
/:對兩個數做除法。返回值會是一個浮點數。 {{ 1 / 2 }} 等于 {{ 0.5 }}
//:對兩個數做除法,返回整數商。 {{ 20 // 7 }} 等于 2
%:計算整數除法的余數。 {{ 11 % 7 }} 等于 4
:用右邊的數乘左邊的操作數。 {{ 2 2 }} 會返回 4 。也可以用于重 復一個字符串多次。 {{ ‘=’ *80 }} 會打印 80 個等號的橫條
:取左操作數的右操作數次冪。 {{ 23 }} 會返回 8

比較操作符
== 比較兩個對象是否相等
!= 比較兩個對象是否不等

如果左邊大于右邊,返回 true
= 如果左邊大于等于右邊,返回 true
< 如果左邊小于右邊,返回 true
<= 如果左邊小于等于右邊,返回 true

邏輯運算符
對于 if 語句,在 for 過濾或 if 表達式中,它可以用于聯合多個表達式
and 如果左操作數和右操作數同為真,返回 true
or 如果左操作數和右操作數有一個為真,返回 true
not 對一個表達式取反
(expr)表達式組
true / false true 永遠是 true ,而 false 始終是 false

template

template功能:可以根據和參考模塊文件,動態生成相類似的配置文件
template文件必須存放于templates目錄下,且命名為 .j2 結尾
yaml/yml 文件需和templates目錄平級,目錄結構如下示例:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2

范例:利用template 同步nginx配置文件

#準備templates/nginx.conf.j2文件 vim temnginx.yml --- - hosts: websrvsremote_user: roottasks:- name: template config to remote hoststemplate: src=nginx.conf.j2 dest=/etc/nginx/nginx.confansible-playbook temnginx.yml

template變更替換

范例:

#修改文件nginx.conf.j2 mkdir templates vim templates/nginx.conf.j2 worker_processes {{ ansible_processor_vcpus }};vim temnginx2.yml --- - hosts: websrvsremote_user: roottasks:- name: install nginxyum: name=nginx- name: template config to remote hoststemplate: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf - name: start serviceservice: name=nginx state=started enable=yesansible-playbook temnginx2.yml

YAML

template算術運算

范例:

vim nginx.conf.j2 worker_processes {{ ansible_processor_vcpus**2 }}; worker_processes {{ ansible_processor_vcpus+2 }};

范例:

[root@ansible ansible]#vim templates/nginx.conf.j2 worker_processes {{ ansible_processor_vcpus**3 }};[root@ansible ansible]#cat templnginx.yml --- - hosts: websrvsremote_user: roottasks:- name: install nginxyum: name=nginx- name: template config to remote hoststemplate: src=nginx.conf.j2 dest=/etc/nginx/nginx.confnotify: restart nginx- name: start serviceservice: name=nginx state=started enabled=yeshandlers:- name: restart nginxservice: name=nginx state=restartedansible-playbook templnginx.yml --limit 10.0.0.8

參卡鏈接:http://www.yunweipai.com/34663.html

總結

以上是生活随笔為你收集整理的Ansible PLaybook template 模板详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。