调用变量
Jinja2是 Python 下一个被广泛应用的模版引擎,Ansible 可以使用jinja2模板调用变量等。
jinja2
## 写一个模板文件(使用facts变量)
[root@Ansible project]# vim jinja.j2
{{ ansible_distribution }} ## 受控端系统
{{ ansible_eth0.ipv4.address }} ## 受控端eth0的IP
{{ ansible_hostname }} ## 受控端主机名
{{ ansible_memtotal_mb // 4 }} ## 受控端总内存除以4取整
## 模板文件放到受控端(根据变量转换成值)
[root@Ansible project]# vim copy_file.yml
- hosts: hosts
tasks:
- name: Copy File
template: ##copy模块不支持转换模板文件
src: ./jinja.j2
dest: /tmp/test.txt
- name: Cat File
shell: cat /tmp/test.txt
register: test_file
- name: Print test_file
debug:
var: test_file.stdout_lines
## 执行一下查看结果
[root@Ansible project]# ansible-playbook copy_file.yml
PLAY [hosts] *****************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************
ok: [nfs1]
ok: [web1]
TASK [Copy File] *************************************************************************************************
changed: [nfs1]
changed: [web1]
TASK [Cat File] **************************************************************************************************
changed: [web1]
changed: [nfs1]
TASK [Print test_file] *******************************************************************************************
ok: [nfs1] => {
"test_file.stdout_lines": [
"CentOS",
"192.168.1.3",
"nfs",
"243"
]
}
ok: [web1] => {
"test_file.stdout_lines": [
"CentOS",
"192.168.1.2",
"web",
"243"
]
}
PLAY RECAP *******************************************************************************************************
nfs1 : ok=4 changed=2 unreachable=0 failed=0
web1 : ok=4 changed=2 unreachable=0 failed=0
判断语句
语句:{% if EXPR %}...{% elif EXPR %}..{% else %}...{% endif %}
Keepalived配置文件(这里就web作为主,nfs作为备)
写一个简单keepalived配置文件的jinja模板
[root@Ansible project]# vim keepalived.conf.j2
global_defs {
router_id {{ ansible_hostname }}
}
vrrp_instance VI_1 {
{% if ansible_hostname=="web" %} ## ansible_hostname变量等于web就引用下面内容
state MASTER
priority 150
{% elif ansible_hostname=="nfs" %} ## ansible_hostname变量等于nfs就引用下面内容
state BACKUP
priority 100
{% endif %} ## 判断语句结束
interface eth0
virtual_router_id 51
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.2
192.168.1.3
}
}
## 配置keepalived加上显示文件内容方便查看结果(这里安装和启动就不写了)
[root@Ansible project]# vim keepalived.yml
- hosts: hosts
tasks:
- name: Configure Keepalived Server
template:
src: ./keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
- name: Cat keeplived Configure
shell: cat /etc/keepalived/keepalived.conf
register: keepalived_config
- name: Print Keepalived Configure
debug:
var: keepalived_config.stdout_lines
## 执行一下
[root@Ansible project]# ansible-playbook keepalived.yml
PLAY [hosts] *****************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************
ok: [web1]
ok: [nfs1]
TASK [Configure Keepalived Server] *******************************************************************************
changed: [nfs1]
changed: [web1]
TASK [Cat keeplived Configure] ***********************************************************************************
changed: [nfs1]
changed: [web1]
TASK [Print Keepalived Configure] ********************************************************************************
ok: [nfs1] => {
"keepalived_config.stdout_lines": [
"global_defs {",
" router_id nfs",
"}",
"vrrp_instance VI_1 {",
" state BACKUP",
" priority 100",
" interface eth0",
" virtual_router_id 51",
" advert_int 1",
" authentication {",
" auth_type PASS",
" auth_pass 1111",
" }",
" virtual_ipaddress {",
" 192.168.1.2",
" 192.168.1.3",
" }",
"}"
]
}
ok: [web1] => {
"keepalived_config.stdout_lines": [
"global_defs {",
" router_id web",
"}",
"vrrp_instance VI_1 {",
" state MASTER",
" priority 150",
" interface eth0",
" virtual_router_id 51",
" advert_int 1",
" authentication {",
" auth_type PASS",
" auth_pass 1111",
" }",
" virtual_ipaddress {",
" 192.168.1.2",
" 192.168.1.3",
" }",
"}"
]
}
PLAY RECAP *******************************************************************************************************
nfs1 : ok=4 changed=2 unreachable=0 failed=0
web1 : ok=4 changed=2 unreachable=0 failed=0
循环语句
语句:{% for i in EXPR %}...{% endfor %}
## 循环定义upstream池
[root@Ansible project]# vim songguoyou.conf.j2
uptream {{ server_name }} {
{% for i in range(1,10) %}
server 192.168.1.{{i}}:{{ http_port }} weigth=2;
{% endfor %}
}
server {
listen {{ http_port }};
server_name {{ server_name }};
location / {
proxy_pass http://{{ server_name }};
proxy_set_header Host $http_host;
}
}
## 写Playbook,里面定义jinja模板引用的变量
[root@Ansible project]# vim nginx.yml
- hosts: web
vars:
server_name: www.songguoyou.com
http_port: 80
tasks:
- name: Configure Nginx Server
template:
src: ./songguoyou.conf.j2
dest: /etc/nginx/conf.d/songguoyou.conf
- name: Cat Nginx Configure
shell: cat /etc/nginx/conf.d/songguoyou.conf
register: nginx_config
- name: Print Nginx Configure
debug:
var: nginx_config.stdout_lines
## 执行一下
[root@Ansible project]# ansible-playbook nginx.yml
PLAY [web] *******************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************
ok: [web1]
TASK [Configure Nginx Server] ************************************************************************************
changed: [web1]
TASK [Cat Nginx Configure] ***************************************************************************************
changed: [web1]
TASK [Print Nginx Configure] *************************************************************************************
ok: [web1] => {
"nginx_config.stdout_lines": [
"uptream www.songguoyou.com {",
" server 192.168.1.1:80 weigth=2;",
" server 192.168.1.2:80 weigth=2;",
" server 192.168.1.3:80 weigth=2;",
" server 192.168.1.4:80 weigth=2;",
" server 192.168.1.5:80 weigth=2;",
" server 192.168.1.6:80 weigth=2;",
" server 192.168.1.7:80 weigth=2;",
" server 192.168.1.8:80 weigth=2;",
" server 192.168.1.9:80 weigth=2;",
"}",
"server {",
" listen 80;",
" server_name www.songguoyou.com;",
" location / {",
" proxy_pass http://www.songguoyou.com;",
" proxy_set_header Host $http_host;",
" }",
"",
"}"
]
}
PLAY RECAP *******************************************************************************************************
web1 : ok=4 changed=2 unreachable=0 failed=0