由于需要拷贝配置文件模板到受控主机,但是模板文件某些地方需要进行修改,此时就需要利用jinja2对模板定义修改项,再用template拷贝到受控主机。

主机分布如下:

  • ansible: 172.168.20.20
  • web1: 172.16.10.10 OS: CentOS 7
  • web2: 172.16.10.20 OS: CentOS 7
  • web3: 172.16.10.30 OS: CentOS 6

ansible的主机清单


[websrvs]
172.16.10.10 http_port=80
172.16.10.20 http_port=81
172.16.10.30 http_port=82
#专用变量

目录树


/data/ansible/nginx
    service.yml
    templates    #该目录存放模板文件,名字固定
        nginx.conf7.j2
        nginx.conf6.j2

模板文件
nginx.conf7.j2


user  nginx;
worker_processes  {{ ansible_processor_vcpus*2 }};
    #这里用到的是jinja2的运算符,"*",前面是变量受控主机的核心个数
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen {{ http_port }};
        #引用hosts中定义的变量
        server_name localhost;
        root html;
        index index.html index.htm;
    }
    include /etc/nginx/conf.d/*.conf;
}

nginx.conf6.j2

user  admin;
    #修改用户以示区别
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen {{ http_port }};
        #引用hosts中定义的变量
        server_name localhost;
        root html;
        index index.html index.htm;
    }
    include /etc/nginx/conf.d/*.conf;
}

service.yml剧本


---
- hosts: websrvs
  remote_user: root
  
  tasks:
    - name: Install Nginx
      yum: name=nginx
    - name: CentOS 6 Config Copy
      template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
      #复制模板文件到受控主机指定位置,指定名称。src用的相对路径,前提是文件再templates文件夹下
      when: ansible_distribution_major_version == "6"
      #表示只对系统版本为6的主机执行该操作
      notify: Restart Nginx
      #当配置文件发生改变就通知handlers做出相应的操作
    - name: CentOS 7 Config Copy
      template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
      ##表示只对系统版本为7的主机执行该操作
      notify: Restart Nginx
    - name: Service Start
      service: name=nginx state=stared enabled=yes
      
  handlers:
    - name: Restart Nginx
      service: name=nginx state=restarted

运行后的结果
web1: 172.16.10.10 OS: CentOS 7
 监听的端口是80,且进程数为cpu核心的两倍
web2: 172.16.10.20 OS: CentOS 7
 监听的端口是81,且进程数为cpu核心的两倍
web3: 172.16.10.30 OS: CentOS 6
 监听的端口是82,且进程数为cpu核心相同,进程的用户是admin


<center>她</center>


Last modification:October 20th, 2019 at 11:12 am
If you think my article is useful to you, please feel free to appreciate