Ansible Jinja2 template

1.jinja2 rendering NginxProxy profile

  • jinja2
    • Housing and construction design fixed?
  • jinja2 template relationship with Ansible

  • Ansible how to use the template jinja2
    • template module copy files?
    • template copy difference?
      • template will parse the configuration file variable
      • copy does not resolve any of the variables, it will only copy files

Ansible allow jinja2 used in the template to determine the cycle, but the judge jinja loop syntax is not allowed in the playbook.

Note: Not every administrator needs this feature, but sometimes jinja2 template can greatly improve efficiency.

1.jinja basic template syntax
1) To use jinj2 in the configuration file, playbook of tasks must use template module

2) inside the template profile using a variable, such as {{PORT}} or {{facts variable}}

2.jinja logic template
{% for i in EXPR%} ... {% endfor%} as a cyclic expression

{% If EXPR%} ... {% elif EXPR%} ... {% endif%} as a condition determination

{# COMMENT #} denotes a comment

{% for i in range(1,10)%}
        server 172.16.1.{{i}};
{% endfor %}


#判断
{% if ansible_fqdn == "web01" %}
        echo 123
{% elif ansible_fqdn == "web02" %}
        echo 456
{% else %}
        echo 789
{% endif %}

nginxproxy profile

[root@manager jinja2]# cat j_nginx.yml 
- hosts: lbservers
  tasks:

        #安装nginx
    - name: Installed nginx Server
      yum: 
        name: nginx
        state: present

        #配置nginx vhosts
    - name: Configure nginx Server
      template:
        src: ./file/proxy_kod.oldxu.com.conf.j2
        dest: /etc/nginx/conf.d/proxy_kod.oldxu.com.conf
      notify: Restart Nginx Server


        #启动Nginx
    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes 


  handlers:
    - name: Restart Nginx Server
      systemd: 
        name: nginx
        state: restarted
        
        
# nginx组变量   
[root@manager jinja2]# cat group_vars/all 
kod_http_port: 80
kod_server_name: kod.oldxu.com
kod_web_site: /code/kod

 

#nginx proxy配置文件渲染
[root@manager jinja2]# cat file/proxy_kod.oldxu.com.conf.j2 
upstream {{ kod_server_name }} {
    {% for host in groups['webservers'] %}
    server {{host}}:{{kod_http_port}};
    {% endfor %}
}

server {
    listen {{ kod_http_port }};
    server_name  {{ kod_server_name }};

    location / {
        proxy_pass http://{{ kod_server_name }};
        proxy_set_header Host $http_hosts;
    }
}

[root@manager jinja2]# cat ../hosts
[webservers]
172.16.1.7
172.16.1.8

2.Keepalived profile master slave

1. Prepare master backup multiple profiles

[root@manager jinja2]# cat j_keepalived.yml 
- hosts: lbservers
  tasks:
    - name: Installed Keepalived Server
      yum:
        name: keepalived
        state: present

    - name: Configure Keepalived Master
      copy:
        src: ./file/keepalived-master.conf.j2
        dest: /etc/keepalived/keepalived.conf
      when: ( ansible_hostname == "lb01" )
      notify: Restart Keepalived Server

    - name: Configure Keepalived Backup
      copy:
        src: ./file/keepalived-backup.conf.j2
        dest: /etc/keepalived/keepalived.conf
      when: ( ansible_hostname == "lb02" )
      notify: Restart Keepalived Server

    - name: Systemd Keepalived Server
      systemd:
        name: keepalived
        state: started
        enabled: yes

  handlers:
    - name: Restart Keepalived Server
      systemd:
        name: keepalived
        state: restarted

2. Set the same set of variables 5 and 6 host_vars variables, different values

#1.准备一份keepalived配置文件
#2.需要在keepalived配置文件中使用变量方式  ---> jinja

[root@manager jinja2]# cat ./file/keepalived-vars.conf.j2 
global_defs {     
    router_id {{ ansible_hostname }}
}

vrrp_instance VI_1 {
    state  {{ state }}
    priority {{ priority }}

    interface eth0
    virtual_router_id 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}



[root@manager jinja2]# cat host_vars/172.16.1.5
state: MASTER
priority: 200
[root@manager jinja2]# cat host_vars/172.16.1.6
state: BACKUP
priority: 99

[root@manager jinja2]# cat var_keepalived.yml 
- hosts: lbservers
  tasks:

    - name: Installed Keepalived Server
      yum:
        name: keepalived
        state: present


    - name: Configure Keepalived Master
      template:
        src: ./file/keepalived-vars.conf.j2
        dest: /etc/keepalived/keepalived.conf
      notify: Restart Keepalived Server

    - name: Systemd Keepalived Server
      systemd:
        name: keepalived
        state: started
        enabled: yes

  handlers:
    - name: Restart Keepalived Server
      systemd:
        name: keepalived
        state: restarted


#为不同的主机设定相同的变量,  只不过值不一样.

3.jinja2 way to judge

[root@manager jinja2]# cat jinja_keepalived.yml 
- hosts: lbservers
  tasks:

    - name: Installed Keepalived Server
      yum:
        name: keepalived
        state: present


    - name: Configure Keepalived Master
      template:
        src: ./file/keepalived.conf.j2
        dest: /etc/keepalived/keepalived.conf
      notify: Restart Keepalived Server

    - name: Systemd Keepalived Server
      systemd:
        name: keepalived
        state: started
        enabled: yes

  handlers:
    - name: Restart Keepalived Server
      systemd:
        name: keepalived
        state: restarted


[root@manager jinja2]# cat file/keepalived.conf.j2 
global_defs {     
    router_id {{ ansible_hostname }}
}

vrrp_instance VI_1 {
{% if ansible_hostname == "lb01" %}
    state  MASTER
    priority 150
{% elif ansible_hostname == "lb02" %}
    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
    }
}

Guess you like

Origin www.cnblogs.com/baozexu/p/11669956.html