This is the 19th day of my participation in the August More Text Challenge

The template is introduced

Jinja is a Python based templating engine. The template class is another important component of Jinja, which can be seen as a compiled module file that produces object text and passes Python variables to the template to replace tags in the template.

The template:

1. # scp root@192.168175.130.:/etc/httpd/conf/httpd.conf ./templates    // Copy the managed configuration file to the local # vim templates/httpd.conf // Define variables on the managed side where the configuration file is to be modified
Copy the code
  1. Modify the contents of the htto.conf configuration file to set the parameters to variables.
Listen {{http_port}}
ServerName {{server_name}}
MaxClients {{access_num}}
Copy the code

3. Add variable vim /etc/ansible/hosts to /etc/ansible/hosts

[abc]
192.168200.129. http_port=192.168200.129.:80 access_num=100 server_name="www.yun.com:80"# vim apache.yml# ansible-playbook apache.yml   
Copy the code

4. Execute the script and check whether the configuration file has been changed on the host of ABC group

The ansible-playbook yaml file is as follows:

cat httpd_template.yaml 
---
- hosts: test
  remote_user: root
  vars: 
​    - package: httpd
​    - service: httpd
  tasks:
​    - name: install httpd package
​      yum: name={{ package }} state=latest
​    - name: install configure file
​      template: src=./template/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
​      notify:
​        - restart httpd
​    - name: start httpd server
​      service: name={{ service }} enabled=true state=started
  handlers:
​    - name: restart httpd
​      service: name={{ service }} state=restartd
Copy the code

The template for loop

Template can also be used to dynamically generate files by using the process to control for loops and if criteria.

Example 1:

cat temnginx.yaml 
---
- hosts: test
  remote_user: root
  vars: 
​    nginx_vhosts:
​      - listen: 80
  tasks:
​    - name: config file 
​      template: src=./template/nginx.conf.j2 dest=/data/nginx.conf
 cat template/nginx.conf.j2 
{% for vhost inNginx_vhosts %} server {listen {{vhost.listen}}} {% endfor %} cat nginx.conf server {listen 80}Copy the code

Example 2:

cat temnginx2.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - 81
​      - 82 
​      - 83
  tasks:
​    - name: template config
​      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

cat nginx.conf.j2 
{% for vhost inNginx_vhosts %} server {listen {{vhost.listen}}} {% endfor %} Generated result Server {listen 81} server {listen 82} server  { listen 83 }Copy the code

Example 3:

cat temnginx3.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:  
​      - listen: 8080
​        server_name: "web1.baidu.com"
​        root: /var/www/nginx/web1/
​      - listen: 8081
​        server_name: "web1.baidu.com"
​        root: "/var/www/nginx/web2/"
  tasks:
​    - name: template config
​      template: src=./template/nginx.conf2.j2 dest=/data/nginx2.conf

cat nginx.conf2.j2
{% for vhost in nginx_vhosts %}
server{
  listen {{ vhost.listen }}
  server_name {{ vhost.server_name }}
  root {{ vhost.root }}
}
{% endfor %}
Copy the code

You can also use the if condition in the template file to determine whether to generate relevant configuration information. Example 1:

cat templnginx4.yaml 
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - web1:
​        listen: 8080
​        root: "/var/www/nginx/web1/"
​      - web2: 
​        listen: 8080
​        server_name: "web2.baidu.com"
​        root: "/var/www/nginx/web2/"
​      - web3: 
​        listen: 8080
​        server_name: "web3.baidu.com"
​        root: "/var/www/nginx/web3/"
  tasks:
​    - name: template config to 
​      template: src=./template/nginx.conf3.j2 dest=/data/nginx3.conf

cat template/nginx.conf3.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
  {% ifvhost.server_name is defined %} server_name {{ vhost.server_name }} {% endif %} root {{ vhost.root }} } {% endfor %} Output result:  cat nginx3.conf server { listen 8080 root /var/www/nginx/web1/ } server { listen 8080 server_name web2.baidu.com root /var/www/nginx/web2/ } server { listen 8080 server_name web3.baidu.com root /var/www/nginx/web3/ }Copy the code