ansible jinja2

ansible jinja2

什麼是jinjia2模板html

jinjia2是Python的全功能模塊引擎nginx

Jinjia2與Ansible的關係web

Ansible一般會使用jinjia2模板來修改被管理主機的配置文件等...在saltack中一樣會使用到jinjia2vim

若是在100臺主機上安裝nginx,每臺nginx的端口都不同,如何解決?bash

Ansible如何使用Jinjia2負載均衡

使用Ansible的jinjia2模板也就是使用template模塊,該模塊和copy模塊同樣,都是將爲文件複製到遠端主機上去,可是區別在於template模塊能夠獲取到文件中的變量,而copy則是原封不動的把文件內容複製過去。以前咱們在推送rsync的backup腳本時,想把腳本中的變量名改爲主機名,若是使用copy模塊則推送過去的就是{{ ansible_fqdn }},不變,若是使用template,則會變成對應的主機名。dom

Ansible使用Jinjia2注意事項wordpress

Ansible容許jinjia2模板中使用條件判斷和循環,可是不容許在playbook中使用。測試

注意:不是每一個管理員都須要這個特性,可是有些時候jinjia2模塊能大大提升效率。rest

Ansible Jinjia2模板使用

jinja2基本 語法

{{ EXPR }}輸出變量值,會輸出自定義的變量值或facts
1)playbook文件使用template模塊
2)模板文件裏面變量使用{{名稱}},好比{{PORT}}或使用facts

jinjia2模板邏輯判斷

#循環表達式
{% for i in EXPR %}
{% endfor %}

#條件判斷
{% if EXPR %}
{% elif EXPR %}
{% else %}
{% endif %}

#註釋
{# COMMENT #}

測試jinja2

#編輯playbook
[root@m01 ~]# vim jinja2.yml
- hosts: all
  tasks:
    - name: scp motd
      template:
        src: /etc/motd
        dest: /etc/motd

#準備motd文件
[root@m01 ~]# vim /etc/motd
Welcome to {{ ansible_fqdn }}
This system total mem is : {{ ansible_memtotal_mb }} MB
This system free mem is: {{ ansible_memfree_mb }} MB

#執行playbook
[root@m01 ~]# ansible-playbook jinja2.yml

PLAY [web_group] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Copy Template File] ********************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
web02                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

#查看結果
[root@m01 ~]# ansible web_group -a 'cat /etc/motd'
web01 | CHANGED | rc=0 >>
Welcome to web01
This system total mem is : 1982 MB
This system free mem is: 1106 MB

web02 | CHANGED | rc=0 >>
Welcome to web02
This system total mem is : 1982 MB
This system free mem is: 1096 MB

該示例展現瞭如何利用jinjia2模板使用facts變量,達到咱們想要的目的,實現自動化的需求。

Ansible Jinjia2管理nginx

Ansible使用jinjia2的for循環表達式渲染出nginx負載均衡的配置文件

使用playbook推送文件

#編輯playbook
[root@m01 ~]# vim lb.yml
- hosts: lb_group
  vars:
    http_port: 80
    server_name: www.drz.com
  tasks:
    - name: copy
      template:
        src: ./www.drz.com.conf.j2
        dest: /etc/nginx/conf.d/www.drz.com.conf
      notify: reload nginx
  handlers:
    - name: reload nginx
      systemd:
        name: nginx
        state: reloaded
        
#準備配置文件
[root@m01 ~]# vim www.drz.com.conf.j2
upstream {{ server_name }} {
{% for n in range(21) %}
        server 172.16.1.{{ n }}:{{ up_port }};
{% endfor %}
}

server {
        listen 80;
        server_name {{ server_name }};

        location / {
                root /code;
                index index.html;
                proxy_pass http://{{ server_name }};
                proxy_set_header Host $http_host;
        }
}

Ansible jinja2渲染負載均衡配置文件

[root@m01 ~]# cat nginx.conf.j2 
upstream {{ domain_name }} {
{% for ip in range(1,16) %}
    server 172.16.1.{{ ip }}:{{ upstream_port }};
{% endfor %}
}

server {
    listen {{ server_port }};
    server_name {{ domain_name }};

    location / {
        root {{ code_dir }};
        index index.html;
        proxy_pass http://{{ domain_name }};
        proxy_set_header HOST $http_host;
    }
}
[root@m01 ~]# cat  nginx.yml 
- hosts: all
  vars_files: ./wordpress_vars.yml
  tasks:
    - name: configure nginx conf
      template:
        src: ./nginx.conf.j2
        dest: /opt/{{ domain_name }}.conf
[root@m01 ~]# cat wordpress_vars.yml 
domain_name: www.drz.com
upstream_port: 8080
server_port: 80
code_dir: /code/wordpress

Ansible jinja2 管理keepalived配置文件

ansible使用jinja2模板的if判斷表達式渲染出keepalived高可用配置文件,並推送到lb

推送keepalived配置文件

[root@m01 ~]# vim keepalived.yml
- hosts: lb_group
  tasks:
    - name: copy file
      template:
        src: ./keepalived.j2
        dest: /etc/keepalived/keepalived.conf
      notify: restart keepalived

  handlers:
    - name: restart keepalived
      systemd:
        name: keepalived
        state: restarted

keepalived原配

#keepalived master 配置文件
global_defs {
    router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {         
        10.0.0.3
    }
}


#keepalived backup配置文件
global_defs {
    router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP        
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3
    }
}

準備keepalived配置文件

[root@m01 ~]# cat keepalived.conf.j2 
global_defs {
    router_id {{ ansible_fqdn }}
}

vrrp_instance VI_1 {
{% if ansible_fqdn == 'web01' %}
    state MASTER
    priority 150
{% else %}
    state BACKUP
    priority 100
{% endif %}
    interface eth0
    virtual_router_id 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {         
        10.0.0.3
    }
}
[root@m01 ~]# cat keep.yml 
- hosts: webs
  tasks:
    - name: configure keep conf
      template:
        src: ./keepalived.conf.j2
        dest: /opt/keepalive.conf
相關文章
相關標籤/搜索