Ansibleはjinja 2を使用してプロファイルを管理し、jinja 2構文の概要(roles templatesプロファイル)を使用します.
14962 ワード
一、Jinja 2紹介
Jinja 2はpythonベースのテンプレートエンジンで、PHPのsmarty、J 2 eeのFreemarker、velocityに似た機能を持っています.unicodeを完全にサポートし、統合された砂箱実行環境を持ち、広く応用されています.jinja 2 BSDライセンスの使用
Jinja 2の構文はvariables(変数)とstatement(文)からなり、以下のようになる.
1、variables:データを出力できる
` my_variables `
{{ some_dudes_name | capitalize }}
2、statements:条件やループなどを作成できる
上の2番目のvariablesの例から、jinja 2はフィルタ付きUnix型パイプオペレータをサポートしており、多くの内蔵フィルタが使用可能であることがわかります.簡単なifとforの山だけでほとんどの一般的なプロファイルを作成することができますが、jinja 2 documentation(http://jinja.pocoo.org/docs/dev/)にはいろいろな面白いものが含まれています.ansibleでは、テンプレートにペイント時間などの追加のテンプレート変数を使用できることがわかります.
最初の例:参照変数
合計スケジュールymlファイル:
注意:ここ-role:templateと-templateは同じです!
その他のymlファイルは、次のとおりです.
2つ目の例:for文
リモートホストのサーバリストを生成する、このリストに参加するのは192.168からである.13.201 web01.test.comは192.168.13.211 web11.test.comが終了し、手動で追加すると科学的ではありません.ここではjinja 2構文のforループを使用してテンプレートを介して対応するプロファイルを一括生成する必要があります.以下のようにします.
ansibleディレクトリ構造:
各ディレクトリの下のymlファイルの内容:
実行結果:
3つ目の例:if文
説明:ポート番号を定義すると、定義したポート番号がバインドされ、ポート番号が定義されていない場合は、デフォルトのポート番号がバインドされます.
主なymlファイルはtemplatesディレクトリの下のtest 3です.j2
Playbookマスターファイル
実行:
表示
vars変数を削除すると、次の結果が実行されます.
表示:
3、Jinja default()設定
プログラム符号化に精通している友人はよく知られており、default()のデフォルト値の設定はプログラムの丈夫さと簡潔さに役立ちます.幸いJinjaもこの機能をサポートしており、上記の例ではMysqlプロファイルのポート定義を生成し、指定すればPORT=3136、そうでなければPORT=3306、このケースをdefault()を使用して試してみます
編集/etc/ansible/roles/mysql_cnf/templates/test3.j 2の内容は以下の通りであり,この方法はより概要的である.
bind_address=10.0.90.27:{{ PORT | default(3306) }}
二、ansible jiaja 2を使用してapacheマルチホスト構成を生成する
1、ディレクトリを作成し、作成したら次のようになります.
2、tasksスケジューリングファイルを作成します.以下のようにします.
3、apacheのjinja 2テンプレートファイルを作成します.以下の通りです.
4、変数を作成します.
5、総スケジュールymlファイルを作成し、以下のようにする.
6、テスト:
7、テストの実行
8、クライアントに確認する
三、ansible jiaja 2を使用してnginxを生成する1つのテンプレートの多種の異なる構成
説明:2台のNginx Proxyで、1台のNginx Webは一連のテンプレートを通じて対応する配置を生成する
1、ansibleディレクトリ構造:
2、tasksディレクトリの下のファイル内容:
3、nginxテンプレートファイル
4、ansible変数ファイル
ansibleメインプレイブックファイル
5、テストと実行:
6、検査実行結果の表示
Nginx Proxyサーバへのプロファイルの表示
Nginx Webサーバへのプロファイルの表示
ここまでで、終わりです.同じテンプレートで簡単なifと変数設定で異なるタイプのホストのNginx conf構成を完了できるので、Ansibleの強力なテンプレート機能を理解するとともに、テンプレート品質の重要性も見る必要があります.
参照リンク:
http://mp.weixin.qq.com/s?__biz=MzIyMDA1MzgyNw==&mid=2651968051&idx=1&sn=922b6cc8c8e6ff6e1c4b822e78ea31c3&scene=0#wechat_redirectよくansible微信の公衆番号で、共同学習に注目することを歓迎します.
出典:http://blog.51cto.com/linuxg/1788574
Jinja 2はpythonベースのテンプレートエンジンで、PHPのsmarty、J 2 eeのFreemarker、velocityに似た機能を持っています.unicodeを完全にサポートし、統合された砂箱実行環境を持ち、広く応用されています.jinja 2 BSDライセンスの使用
Jinja 2の構文はvariables(変数)とstatement(文)からなり、以下のようになる.
1、variables:データを出力できる
` my_variables `
{{ some_dudes_name | capitalize }}
2、statements:条件やループなどを作成できる
if :
{% if my_conditional %}
...
{% endif %}
for :
{% for item in all_items %}
`item`
……
{% endfor %}
上の2番目のvariablesの例から、jinja 2はフィルタ付きUnix型パイプオペレータをサポートしており、多くの内蔵フィルタが使用可能であることがわかります.簡単なifとforの山だけでほとんどの一般的なプロファイルを作成することができますが、jinja 2 documentation(http://jinja.pocoo.org/docs/dev/)にはいろいろな面白いものが含まれています.ansibleでは、テンプレートにペイント時間などの追加のテンプレート変数を使用できることがわかります.
最初の例:参照変数
#cd roles/template/
.
├── meta
│ └── main.yml
├── tasks
│ ├── template.yml
│ └── main.yml
├── templates
│ ├── order.j2
└── vars
└── main.yml
合計スケジュールymlファイル:
#cat templates.yml
---
- hosts: 10.0.90.27
user: root
gather_facts: false
roles:
- role: template
注意:ここ-role:templateと-templateは同じです!
その他のymlファイルは、次のとおりです.
#cat tasks/main.yml
- include: template.yml
#cat tasks/template.yml
- name: create {{ PROJECT }} directory
file: dest=/data/{{ PROJECT }} state=directory
- name: template transfor java dir
template: src=order.j2 dest=/data/{{ PROJECT }}/order.conf
#cat templates/order.j2
project: {{ PROJECT }}
switch: {{ SWITCH }}
dbport: {{ DBPORT }}
#cat vars/main.yml
PROJECT: "JAVA"
SWITCH: "ON"
DBPORT: "8080"
:
# ansible-playbook templates.yml --syntax-check
playbook: templates.yml
:
# ansible-playbook templates.yml
PLAY [10.0.90.27] **************************************************************
TASK [template : include] ***************************************************
included: /etc/ansible/roles/template/tasks/template.yml for 10.0.90.27
TASK [template : create JAVA directory] *************************************
changed: [10.0.90.27]
TASK [template : template transfor java dir] ********************************
changed: [10.0.90.27]
PLAY RECAP *********************************************************************
10.0.90.27 : ok=3 changed=2 unreachable=0 failed=0
10.0.90.27
#cat /data/JAVA/order.conf
project: JAVA
switch: ON
dbport: 8080
2つ目の例:for文
リモートホストのサーバリストを生成する、このリストに参加するのは192.168からである.13.201 web01.test.comは192.168.13.211 web11.test.comが終了し、手動で追加すると科学的ではありません.ここではjinja 2構文のforループを使用してテンプレートを介して対応するプロファイルを一括生成する必要があります.以下のようにします.
ansibleディレクトリ構造:
#cd /etc/ansible/roles/test_hosts
.
├── meta
│ └── main.yml
├── tasks
│ ├── file1.yml
│ └── main.yml
├── templates
│ └── test1.j2
└── vars
└── main.yml
各ディレクトリの下のymlファイルの内容:
# cat tasks/file1.yml
- name: ansible jinja2 template for hosts config
template: src=test1.j2 dest=/etc/httpd/conf/httpd.conf.test
# cat tasks/main.yml
- include: file1.yml
# cat templates/test1.j2
{% for id in range(201,212) %}
192.168.13.{{ id }} web{{ "%03d" |format(id-200) }}.test.com
{% endfor %}
:
{{ id }} for id
"%02d" python (%d ) 01,02 , 2 , 02
“|” format 。
実行結果:
#cat httpd.conf.test
192.168.13.201 web01.test.com
192.168.13.202 web02.test.com
192.168.13.203 web03.test.com
192.168.13.204 web04.test.com
192.168.13.205 web05.test.com
192.168.13.206 web06.test.com
192.168.13.207 web07.test.com
192.168.13.208 web08.test.com
192.168.13.209 web09.test.com
192.168.13.210 web10.test.com
192.168.13.211 web11.test.com
3つ目の例:if文
説明:ポート番号を定義すると、定義したポート番号がバインドされ、ポート番号が定義されていない場合は、デフォルトのポート番号がバインドされます.
ansible
#cd /etc/ansible/roles/mysql_cnf
#tree
.
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ └── test3.j2
└── vars
主なymlファイルはtemplatesディレクトリの下のtest 3です.j2
# cat templates/test3.j2
{% if PORT %}
bind_address=10.0.90.27:{{ PORT }}
{% else %}
bind_address=10.0.90.27:3306
{% endif %}
Playbookマスターファイル
# cat jinj2_test.yml
---
- hosts: 10.0.90.27
user: root
gather_facts: false
vars:
PORT: 3136
tasks:
- name: copy file to client
template: src=/etc/ansible/roles/mysql_cnf/templates/test3.j2 dest=/root/my.cnf
実行:
# ansible-playbook jinj2_test.yml
PLAY [10.0.90.27] **************************************************************
TASK [copy file to client] *****************************************************
changed: [10.0.90.27]
PLAY RECAP *********************************************************************
10.0.90.27 : ok=1 changed=1 unreachable=0 failed=0
表示
# cat my.cnf
bind_address=10.0.90.27:3136
vars変数を削除すると、次の結果が実行されます.
# cat jinj2_test.yml
---
- hosts: 10.0.90.27
user: root
gather_facts: false
vars:
PORT: false
tasks:
- name: copy file to client
template: src=/etc/ansible/roles/mysql_cnf/templates/test3.j2 dest=/root/my.cnf
表示:
# cat my.cnf
bind_address=10.0.90.27:3306
3、Jinja default()設定
プログラム符号化に精通している友人はよく知られており、default()のデフォルト値の設定はプログラムの丈夫さと簡潔さに役立ちます.幸いJinjaもこの機能をサポートしており、上記の例ではMysqlプロファイルのポート定義を生成し、指定すればPORT=3136、そうでなければPORT=3306、このケースをdefault()を使用して試してみます
編集/etc/ansible/roles/mysql_cnf/templates/test3.j 2の内容は以下の通りであり,この方法はより概要的である.
bind_address=10.0.90.27:{{ PORT | default(3306) }}
二、ansible jiaja 2を使用してapacheマルチホスト構成を生成する
1、ディレクトリを作成し、作成したら次のようになります.
#cd /etc/ansible/roles/apache_conf
# tree ./
./
├── meta
│ └── main.yml
├── tasks
│ ├── file.yml
│ └── main.yml
├── templates
│ └── apache.config.j2
└── vars
└── main.yml
4 directories, 5 files
2、tasksスケジューリングファイルを作成します.以下のようにします.
#cat file.yml
- name: ansible jinja2 template for apache config
template: src=apache.config.j2 dest=/etc/httpd/conf/httpd.conf.template
#cat main.yml
- include: file.yml
3、apacheのjinja 2テンプレートファイルを作成します.以下の通りです.
#cat apache.config.j2
NameVirtualHost *:80
{% for vhost in apache_vhost %}
ServerName {{ vhost.servername }}
DocumentRoot {{ vhost.documentroot }}
{% if vhost.serveradmin is defined %}
ServerAdmin {{ vhost.serveradmin }}
{% endif %}
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
{% endfor %}
4、変数を作成します.
#cat vars/main.yml
apache_vhost:
- {servername: "apache.test1.com", documentroot: "/data/test1/"}
- {servername: "apache.test2.com", documentroot: "/data/test2/"}
5、総スケジュールymlファイルを作成し、以下のようにする.
#cat /etc/ansible/apache_test.yml
---
- hosts: 10.0.90.27
user: root
gather_facts: no
roles:
- { role: apache_conf }
6、テスト:
#ansible-playbook apache_test.yml --syntax-check
playbook: apache_test.yml
7、テストの実行
#ansible-playbook apache_test.yml
PLAY [10.0.90.27] **************************************************************
TASK [apache_conf : include] ***************************************************
included: /etc/ansible/roles/apache_conf/tasks/file.yml for 10.0.90.27
TASK [apache_conf : ansible jinja2 template for apache config] *****************
changed: [10.0.90.27]
PLAY RECAP *********************************************************************
10.0.90.27 : ok=2 changed=1 unreachable=0 failed=0
8、クライアントに確認する
#cat httpd.conf.template
NameVirtualHost *:80
ServerName apache.test1.com
DocumentRoot /data/test1/
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
ServerName apache.test2.com
DocumentRoot /data/test2/
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
三、ansible jiaja 2を使用してnginxを生成する1つのテンプレートの多種の異なる構成
説明:2台のNginx Proxyで、1台のNginx Webは一連のテンプレートを通じて対応する配置を生成する
1、ansibleディレクトリ構造:
# cd roles/nginx_conf/
#tree
.
├── files
├── meta
│ └── main.yml
├── tasks
│ ├── file.yml
│ └── main.yml
├── templates
│ └── nginx.conf.j2
└── vars
└── main.yml
2、tasksディレクトリの下のファイル内容:
#cat tasks/file.yml
- name: nginx.j2 template transfer example
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf.template
#cat tasks/main.yml
- include: file.yml
3、nginxテンプレートファイル
#cat templates/nginx.conf.j2
{% if nginx_use_proxy %}
{% for proxy in nginx_proxies %}
upstream {{ proxy.name }}
#server 127.0.0.1:{{ proxy.port }};
server {{ ansible_eth0.ipv4.address }}:{{ proxy.port }};
}
{% endfor %}
{% endif%}
server {
listen 80;
servername {{ nginx_server_name }};
access_log off;
error_log /etc/nginx/nginx_error.log;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name {{ nginx_server_name }};
ssl_certificate /etc/nginx/ssl/{{ nginx_ssl_cert_name }};
ssl_certificate_key /etc/nginx/ssl/{{ nginx_ssl_cert_key }};
root {{ nginx_web_root }};
index index.html index.html;
{% if nginx_use_auth %}
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/{{ project_name }}.htpasswd;
{% endif %}
{% if nginx_use_proxy %}
{% for proxy in nginx_proxies %}
location {{ proxy.location }} {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://{{ proxy.name }};
break;
}
{% endfor %}
{% endif %}
{% if nginx_server_static %}
location / {
try_files $url $url/ =404;
}
{% endif %}
}
4、ansible変数ファイル
cat vars/main.yml
nginx_server_name: www.testnginx.com
nginx_web_root: /data/html/
nginx_proxies:
- name: suspicious
location: /
port: 1234
- name: suspicious-api
location: /api
port: 4567
ansibleメインプレイブックファイル
#cat nginx_test.yml
##The first roles
- name: Nginx Proxy Server's Config Dynamic Create
hosts: "10.0.90.25:10.0.90.26"
remote_user: root
vars:
nginx_use_proxy: true
nginx_ssl_cert_name: ifa.crt
nginx_ssl_cert_key: ifa.key
nginx_use_auth: true
project_name: suspicious
nginx_server_static: true
gather_facts: true
roles:
- role: nginx_conf
##The second roles
- name: Nginx WebServer's Config Dynamic Create
hosts: 10.0.90.27
remote_user: root
vars:
nginx_use_proxy: false
nginx_ssl_cert_name: ifa.crt
nginx_ssl_cert_key: ifa.crt
nginx_use_auth: false
project_name: suspicious
nginx_server_static: false
gather_facts: false
roles:
- role: nginx_conf
5、テストと実行:
#ansible-playbook nginx_test.yml --syntax-check
playbook: nginx_test.yml
:
# ansible-playbook nginx_test.yml
PLAY [Nginx Proxy Server's Config Dynamic Create] ******************************
TASK [setup] *******************************************************************
ok: [10.0.90.25]
ok: [10.0.90.26]
TASK [nginx_conf : include] ****************************************************
included: /etc/ansible/roles/nginx_conf/tasks/file.yml for 10.0.90.25, 10.0.90.26
TASK [nginx_conf : nginx.j2 template transfer example] *************************
changed: [10.0.90.26]
changed: [10.0.90.25]
PLAY [Nginx WebServer's Config Dynamic Create] *********************************
TASK [nginx_conf : include] ****************************************************
included: /etc/ansible/roles/nginx_conf/tasks/file.yml for 10.0.90.27
TASK [nginx_conf : nginx.j2 template transfer example] *************************
changed: [10.0.90.27]
PLAY RECAP *********************************************************************
10.0.90.25 : ok=3 changed=1 unreachable=0 failed=0
10.0.90.26 : ok=3 changed=1 unreachable=0 failed=0
10.0.90.27 : ok=2 changed=1 unreachable=0 failed=0
6、検査実行結果の表示
Nginx Proxyサーバへのプロファイルの表示
#cat nginx.conf.template
upstream suspicious
#server 127.0.0.1:1234;
server 10.0.90.26:1234;
}
upstream suspicious-api
#server 127.0.0.1:4567;
server 10.0.90.26:4567;
}
server {
listen 80;
servername www.testnginx.com;
access_log off;
error_log /etc/nginx/nginx_error.log;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name www.testnginx.com;
ssl_certificate /etc/nginx/ssl/ifa.crt;
ssl_certificate_key /etc/nginx/ssl/ifa.key;
root /data/html/;
index index.html index.html;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/suspicious.htpasswd;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://suspicious;
break;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://suspicious-api;
break;
}
location / {
try_files $url $url/ =404;
}
}
Nginx Webサーバへのプロファイルの表示
#cat nginx.conf.template
server {
listen 80;
servername www.testnginx.com;
access_log off;
error_log /etc/nginx/nginx_error.log;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name www.testnginx.com;
ssl_certificate /etc/nginx/ssl/ifa.crt;
ssl_certificate_key /etc/nginx/ssl/ifa.crt;
root /data/html/;
index index.html index.html;
}
ここまでで、終わりです.同じテンプレートで簡単なifと変数設定で異なるタイプのホストのNginx conf構成を完了できるので、Ansibleの強力なテンプレート機能を理解するとともに、テンプレート品質の重要性も見る必要があります.
参照リンク:
http://mp.weixin.qq.com/s?__biz=MzIyMDA1MzgyNw==&mid=2651968051&idx=1&sn=922b6cc8c8e6ff6e1c4b822e78ea31c3&scene=0#wechat_redirectよくansible微信の公衆番号で、共同学習に注目することを歓迎します.
出典:http://blog.51cto.com/linuxg/1788574