ansible-playbook安装nginx

首先看下目录结构

ansible-playbook安装nginx_第1张图片
nginx.conf.j2是模板配置文件,里面只改了一个配置worker_processes,单纯为了测试用,文件可以根据自己需求去改

[root@python playbook]# grep -Ev '^[[:space:]].*#|^#|^$' templates/nginx.conf.j2 
user nginx;
worker_processes {{ ansible_processor_vcpus }}; #工作线程数等同cpu数
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

yum-install-nginx.ymlyum-install-nginx-1.yml除了格式不一样没别的不一样,都是执行安装nginx,我这里都列出来,大家挑一个用就行

yum-install-nginx.yml
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: template  config
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart nginx service
    - name: start nginx
      service: name=nginx enabled=yes state=started

  handlers:
    - name: restart nginx service
      service: name=nginx  state=restarted
yum-install-nginx-1.yml
---
- hosts: all
  remote_user: root

  tasks:
    - name: yum install nginx
      yum:
        name: nginx
        state: present
    - name: template config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx
    - name: start nginx
      service:
        name: nginx
        state: started
        enabled: yes
  
  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

接下来我们执行ansible-playbook -C yum-install-nginx-1.yml或者ansible-playbook -C yum-install-nginx.yml检查我们playbook语法格式对不对,我这里执行得是ansible-playbook -C yum-install-nginx-1.yml
ansible-playbook安装nginx_第2张图片
检查结果没问题我们把 -C 去掉,执行我们得playbook
ansible-playbook安装nginx_第3张图片
我的all里面有三台机器,都执行太慢,我就加了一个 --limit 参数,只让一台机器执行
执行完了我们检查一下对应主机nginx是不是起来了
ansible-playbook安装nginx_第4张图片
看看端口,进程数对不对,我们目标主机是4C的,所以起了四个worker process,到这里我们就安装完了。

你可能感兴趣的:(自动化)